java写文件的基本操作
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter; public class FileIO { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException { // 1.创建文件夹 mkdir
// 如果路径存在,才可以通过该方法创建文件夹。。。mkdirs路径不存在也可以通过这个方法创建,先创建路径再创建文件夹
File myFolderPath = new File("D:\\MyEclipse2013Work\\Test\\test");
try {
if (!myFolderPath.exists()) {
myFolderPath.mkdir();
}
} catch (Exception e) {
System.out.println("新建目录操作出错");
e.printStackTrace();
} // 2. 创建文件 FileWrite 和PrintWrite 的区别需要注意
File myFilePathFile = new File("test.txt");
try {
if (!myFilePathFile.exists()) {
myFilePathFile.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePathFile);
PrintWriter myFile = new PrintWriter(resultFile);
myFile.println("新建文件操作");
myFile.println("新建文件操作");
myFile.println("新建文件操作");
myFile.println("新建文件操作");
myFile.write("sss", 1, 1);
myFile.println("新建文件操作");
resultFile.write("sssssssssssss");
resultFile.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println("新建文件操作出错");
e.printStackTrace();
} // 3. FileWrite 结合BufferedWriter打印文本换行
TestBufferedWriter(); // 4.获得当前工程的路径
File directory = new File("");// 参数为空
String courseFile = directory.getCanonicalPath();
System.out.println(courseFile);
} public static void TestBufferedWriter() {
try { String filePath = "test1.txt";
File myFile = new File(filePath);
if (!myFile.exists()) {
myFile.createNewFile();
}
FileWriter resultFile = new FileWriter(myFile);
BufferedWriter buffer = new BufferedWriter(resultFile);
String fileContent = "This is my name./n we are isoftsotne.com /n chinesebillboy at here。/n";
String contentString = "";
int flag = 0;
boolean isGO = true;
while ((flag = fileContent.indexOf("/n")) != -1 && isGO) {
contentString = fileContent.substring(0, flag);
buffer.write(contentString);
buffer.newLine();
buffer.flush();
if (flag + 2 >= fileContent.length()) {
isGO = false;
} else {
fileContent = fileContent.substring(flag + 2);
}
}
buffer.flush();
resultFile.flush();
buffer.close();
resultFile.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally { }
}
}
java写文件的基本操作的更多相关文章
- java写文件实现换行
Java 写文件实现换行 第一种: 写入的内容中利用\r\n进行换行 File file = new File("D:/text"); try { if(!file.exist ...
- java写文件时,输出不完整的原因以及解决方法
在java的IO体系中,写文件通常会用到下面语句 BufferedWriter bo=new BufferedWriter(new FileWriter("sql语句.txt")) ...
- java 写文件解析
import java.io.File; import java.io.FileOutputStream; import java.io.*; public class FileTest { publ ...
- java写文件时,输出不完整的原因以及解决方法close()或flush()
在java的IO体系中,写文件通常会用到下面语句 BufferedWriter bw=new BufferedWriter(new FileWriter("sql语句.txt")) ...
- java写文件时往末尾追加文件(而不是覆盖原文件),的两种方法总结
代码如下: import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; public ...
- java写文件
randomAccessFile.close(); } e.printStack ...
- java对文件的基本操作
package cn.edu.fhj.day009.FileDemo; import java.io.File; import java.io.IOException; public class Fi ...
- java写文件读写操作(IO流,字符流)
package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...
- java写文件读写操作(IO流,字节流)
package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...
随机推荐
- JDBC开发,数据库的连接
JDBC开发步骤 1.建完工程后,右击工程,new,新建一个文件夹Folder,装jar包, 2.将下载好的驱动包打开,找到jar文件,CTRL+C,选中装jar包的文件夹,CTRL+V ...
- C#装箱与拆箱的研究
在对这个问题展开讨论之前,我们不妨先来问这么几个问题,以系统的了解我们今天要探究的主题. 观者也许曾无数次的使用过诸如System.Console类或.NET类库中那些品种繁多的类.那么,我想问的是它 ...
- shell脚本,awk实现跳过文件里面的空行。
1.用awk '{if(!NF ){next}}1' file11 实现对文件里面的空行进行跳过操作,并输出结果. 2. awk '{if(!NF || /^#/){next}}1' file11 实 ...
- mod_deflate模块
mod_deflate模块 压缩模块,使用mod_deflate模块压缩页面优化传输速度 主要是需要设置 1.针对的内容 2.压缩比是多少 可以忽略排除特定旧版本的浏览器的设置.因为那些都太老了,现在 ...
- Linux运维企业架构项目实战系列
Linux运维企业架构项目实战系列 项目实战1—LNMP的搭建.nginx的ssl加密.权限控制的实现 项目实战2—LVS.nginx实现负载均衡系列2.1 项目实战2.1—实现基于LVS负载均衡集群 ...
- 【Python学习之八】ORM
ORM 什么是ORM呢? ORM全称是:Object-Relational Mapping.即对象-关系映射,就是把关系数据库的一行映射为一个对象,也就是一个类对应一个表.这样,写代码更简单,不用直接 ...
- JS — 实现简单的数字时钟
js实现简单的数字时钟 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- 04.VUE学习之v-text v-html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 使用python3下载网易云音乐歌单歌曲,附源代码
""" 用selenium+PhantomJS配合,不需要进行逆向工程 python 3下的selenium不能默认安装,需要指定版本2.48.0 "" ...
- 初学js之多组图片切换实例
需求是以上效果展示.话不多说,直接代码显示,不涉及代码优化.已实现功能为目的. 先看html部分: <body> <div class="dream" id=&q ...