Java写入文件的几种方法
FileWritter写入文件
FileWritter
字符流写入字符到文件。默认情况下,它会使用新的内容取代所有现有的内容,然而,当指定一个true (布尔)值作为FileWritter构造函数的第二个参数,它会保留现有的内容,并追加新内容在文件的末尾。
1. 替换所有现有的内容与新的内容。
new FileWriter(file);
2. 保留现有的内容和附加在该文件的末尾的新内容。
new FileWriter(file,true);
追加文件示例
一个文本文件,命名为“javaio-appendfile.txt”,并包含以下内容。
ABC Hello
追加新内容 new FileWriter(file,true)
package org.hhc.fileoperation;
import java.io.File;
import java.io.FileWriter;
public class AppendToFileExample {
public static void main(String[] args) {
try {
String data = "This content : will append to the end of the file\n";
File file = new File("javaio-appendfile.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file, true);
fileWriter.write(data);
fileWriter.write(data, 13, 15);
// FileWriter fileWriter2 = new FileWriter(file.getName(), true);
// fileWriter2.write(data);
fileWriter.close();
// fileWriter2.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
结果
现在,文本文件“javaio-appendfile.txt”内容更新如下:
ABC HelloThis content : will append to the end of the file
: will append t
总结:
每写一个数据,硬盘就有一个写动作,性能极差;
字符流操作
BufferedWriter写入文件
缓冲字符(BufferedWriter )是一个字符流类来处理字符数据。不同于字节流(数据转换成字节),你可以直接写字符串,数组或字符数据保存到文件。
package org.hhc.fileoperation;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterExample {
public static void main(String[] args) {
try {
String content = "This is the content to write into file";
File file = new File("filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.newLine();
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
总结:
为带默认缓冲的字符输出输入流,因为缓冲区所以效率比没缓冲区的高
字符流操作
1. java.io.BufferedReader和java.io.BufferedWriter类各拥有8192字符的缓冲区。当BufferedReader在读取文本文件时,会先尽量从文件中读入字符数据并置入缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取。如果缓冲区数据不足,才会再从文件中读取,使用BufferedWriter时,写入的数据并不会先输出到目的地,而是先存储至缓冲区中。如果缓冲区中的数据满了,才会一次对目的地进行写出。
2. 从标准输入流System.in中直接读取使用者输入时,使用者每输入一个字符,System.in就读取一个字符。为了能一次读取一行使用者的输入,使用了BufferedReader来对使用者输入的字符进行缓冲。readLine()方法会在读取到使用者的换行字符时,再一次将整行字符串传入。
3. System.in是一个位流,为了转换为字符流,可使用InputStreamReader为其进行字符转换,然后再使用BufferedReader为其增加缓冲功能。例如:
这里一个 "修饰类 "的概念 FileWriter 是被修饰者 BufferedWriter 是修饰者 一般用法为 BufferedWriter bw = new BufferedWriter(new FileWriter( "filename ")); 上面这个加了一个缓冲,缓冲写满后在将数据写入硬盘 这样做极大的提高了性能
如果单独使用 FileWriter 也可以 你每写一个数据,硬盘就有一个写动作,性能极差 |
FileOutputStream写入文件
文件输出流是一种用于处理原始二进制数据的字节流类。为了将数据写入到文件中,必须将数据转换为字节,并保存到文件。请参阅下面的完整的例子。
package org.hhc.fileoperation;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamExample {
public static void main(String[] args) {
try {
String content = "This is the content to write into file";
File file = new File("filenameByte.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fop = new FileOutputStream(file, true);
fop.write(content.getBytes());
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
//更新的JDK7例如,
使用新的“尝试资源关闭”的方法来轻松处理文件。
package com.yiibai.io;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFileExample {
public static void main(String[] args) {
File file = new File("c:/newfile.txt");
String content = "This is the text content";
try (FileOutputStream fop = new FileOutputStream(file)) {
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
参考
http://www.jb51.net/article/47062.htm
http://blog.sina.com.cn/s/blog_66e177dd0100hf92.html
http://blog.csdn.net/jiangxinyu/article/details/7885518/
http://blog.csdn.net/liuhenghui5201/article/details/8276278
http://blog.csdn.net/yuzhuuse/article/details/7275785
http://xyiyy.iteye.com/blog/361476
http://blog.csdn.net/liuhenghui5201/article/details/8279557