在Java中怎样逐行地写文件?
2015-12-26 11:25
543 查看
下边是写东西到一个文件里的Java代码。
执行后每一次,一个新的文件被创建,而且之前一个也将会被新的文件替代。这和给文件追加内容是不同的。
这个样例使用的是FileOutputStream,你也能够使用FileWriter 或PrintWriter。假设是针对文本文件的操作是全然绰绰有余的。
使用FileWriter:
FileWriter
is a convenience class for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer
size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.
FileWriter针对写字符文件是一个非常方便的类。这个类的构造方法如果默认的字符编码和默认的字节缓冲区都是能够接受的。
如果要指定编码和字节缓冲区的长度,须要构造OutputStreamWriter。
PrintWriter
prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It
does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.
PrintWriter打印格式化对象的表示到一个文本输出流。这个类实现了全部在PrintStream中的打印方法。
它不包括用于写入原始字节,由于一个程序应该使用未编码的字节流。
主要差别在于,PrintWriter提供了一些额外的方法来格式化。比如println和printf。
此外,万一遇到不论什么的I/O故障FileWriter会抛出IOException。PrintWriter的方法不抛出IOException异常,而是他们设一个布尔标志值,能够用这个值来检測是否出错(checkError())。
PrintWriter在数据的每一个字节被写入后自己主动调用flush
。而FileWriter,调用者必须採取手动调用flush.
执行后每一次,一个新的文件被创建,而且之前一个也将会被新的文件替代。这和给文件追加内容是不同的。
public static void writeFile1() throws IOException { File fout = new File("out.txt"); FileOutputStream fos = new FileOutputStream(fout); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos)); for (int i = 0; i < 10; i++) { bw.write("something"); bw.newLine(); } bw.close(); }
这个样例使用的是FileOutputStream,你也能够使用FileWriter 或PrintWriter。假设是针对文本文件的操作是全然绰绰有余的。
使用FileWriter:
public static void writeFile2() throws IOException { FileWriter fw = new FileWriter("out.txt"); for (int i = 0; i < 10; i++) { fw.write("something"); } fw.close(); }使用PrintWriter:
public static void writeFile3() throws IOException { PrintWriter pw = new PrintWriter(new FileWriter("out.txt")); for (int i = 0; i < 10; i++) { pw.write("something"); } pw.close(); }使用OutputStreamWriter:
public static void writeFile4() throws IOException { File fout = new File("out.txt"); FileOutputStream fos = new FileOutputStream(fout); OutputStreamWriter osw = new OutputStreamWriter(fos); for (int i = 0; i < 10; i++) { osw.write("something"); } osw.close(); }摘自Java文档:
FileWriter
is a convenience class for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer
size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.
FileWriter针对写字符文件是一个非常方便的类。这个类的构造方法如果默认的字符编码和默认的字节缓冲区都是能够接受的。
如果要指定编码和字节缓冲区的长度,须要构造OutputStreamWriter。
PrintWriter
prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It
does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.
PrintWriter打印格式化对象的表示到一个文本输出流。这个类实现了全部在PrintStream中的打印方法。
它不包括用于写入原始字节,由于一个程序应该使用未编码的字节流。
主要差别在于,PrintWriter提供了一些额外的方法来格式化。比如println和printf。
此外,万一遇到不论什么的I/O故障FileWriter会抛出IOException。PrintWriter的方法不抛出IOException异常,而是他们设一个布尔标志值,能够用这个值来检測是否出错(checkError())。
PrintWriter在数据的每一个字节被写入后自己主动调用flush
。而FileWriter,调用者必须採取手动调用flush.
原文:How to Write a File Line by Line in Java?
相关文章推荐
- spring线程池ThreadPoolExecutor配置并且得到任务执行的结果
- JavaIO流分析总结
- Eclipse中配置ServiceMix的远程调试模式
- 个人学习-java-数据类型
- JAVA基础01
- Myeclipse报PermGen space异常的问题
- 心形图
- Java&Xml教程(七)使用JDOM修改XML文件内容
- Java&Xml教程(七)使用JDOM修改XML文件内容
- Java&Xml教程(七)使用JDOM修改XML文件内容
- Java_interface_接口
- MyEclipse护眼模式、字体大小的调整
- ArrayList Vector LinkedList HasMap等区别与用法
- eclipse里报:An internal error occurred during: "Building workspace". Java heap space)
- Spring单实例、多线程安全、事务解析
- Spring事务处理时自我调用的解决方案及一些实现方式的风险
- Andriod基本框架
- Java应用程序打包成jar文件后,运行jar包后不能显示图片的问题
- spring项目加入jta分布式事务的实现方式: Atomikos
- SpringMVC框架总体介绍(一)