Java文件读写操作
Java IO系统里读写文件使用Reader和Writer两个抽象类,Reader中read()和close()方法都是抽象方法。Writer中 write(),flush()和close()方法为抽象方法。子类应该分别实现他们。
Java IO已经为我们提供了三个方便的Reader的实现类,FileReader,InputStreamReader和BufferedReader。其中最重要的类是InputStreamReader, 它是字节转换为字符的桥梁。你可以在构造器重指定编码的方式,如果不指定的话将采用底层操作系统的默认编码方式,例如GBK等。
FileReader读txt文件例子
FileReader fr =
new FileReader("D:\\test.txt");
int ch = 0;
while((ch = fr.read())!=-1 ){
System.out.print( ( char)ch );
}
int ch = 0;
while((ch = fr.read())!=-1 ){
System.out.print( ( char)ch );
}
InputStreamReader读txt文件例子
InputStream is =
new FileInputStream(
new File("D:\\test.txt"));
InputStreamReader reader = new InputStreamReader(is);
int ch = 0;
while((ch = reader.read())!=-1 ){
System.out.print(( char)ch);
}
这和FileReader并没有什么区别,事实上在FileReader中的方法都是从InputStreamReader中继承过来的。 read()方法是比较好费时间的,如果为了提高效率,我们可以使用BufferedReader对Reader进行包装,这样可以提高读取得速度,我们可以一行一行的读取文本,使用 readLine()方法。
InputStreamReader reader = new InputStreamReader(is);
int ch = 0;
while((ch = reader.read())!=-1 ){
System.out.print(( char)ch);
}
BufferedReader br =
new BufferedReader(
new FileReader("D:\\test.txt"));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
当你明白了如何用Reader来读取文本文件的时候那么用Writer写文件同样非常简单。有一点需要注意,当你写文件的时候,为了提高效率,写入的数据会先放入缓冲区,然后写入文件。因此有时候你需要主动调用flush()方法。
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
有读就有写,写文本文件可以使用PrintWriter,FileWriter,BufferedWriter。
FileWriter fw =
new FileWriter("D:\\test01.txt");
String s = "hello world!\n";
fw.write(s,0,s.length());
fw.flush();
OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream("D:\\test02.txt"));
osw.write(s,0,s.length());
osw.flush();
PrintWriter pw = new PrintWriter( new OutputStreamWriter( new FileOutputStream("D:\\test03.txt")), true);
pw.println(s);
fw.close();
osw.close();
pw.close();
String s = "hello world!\n";
fw.write(s,0,s.length());
fw.flush();
OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream("D:\\test02.txt"));
osw.write(s,0,s.length());
osw.flush();
PrintWriter pw = new PrintWriter( new OutputStreamWriter( new FileOutputStream("D:\\test03.txt")), true);
pw.println(s);
fw.close();
osw.close();
pw.close();
如果想在文件末尾追加内容,声明加个true就加一个"true"就可以了: FileWriter fw = new FileWriter("log.txt",true);
下面是一个BufferedWriter的例子:
File file =
new File("D:\\test.txt");
File dest = new File("D:\\test01.txt");
try {
BufferedReader reader = new BufferedReader( new FileReader(file));
BufferedWriter writer = new BufferedWriter( new FileWriter(dest));
String line = reader.readLine();
while(line!= null){
writer.write(line);
line = reader.readLine();
}
writer.flush();
reader.close();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File dest = new File("D:\\test01.txt");
try {
BufferedReader reader = new BufferedReader( new FileReader(file));
BufferedWriter writer = new BufferedWriter( new FileWriter(dest));
String line = reader.readLine();
while(line!= null){
writer.write(line);
line = reader.readLine();
}
writer.flush();
reader.close();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
最后写的程序用于简单的文件读写操作。FileHelper类中的readFile方法用于读取文件内容,writeFile方法用于向文件中写入内容。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class FileHelper {
public static String readFile(String filename) throws Exception {
BufferedReader reader = new BufferedReader( new FileReader(filename));
String ans = "", line = null;
while((line = reader.readLine()) != null){
ans += line + "\r\n";
}
reader.close();
return ans;
}
public static void writeFile(String content, String filename) throws Exception {
BufferedWriter writer = new BufferedWriter( new FileWriter(filename));
writer.write(content);
writer.flush();
writer.close();
}
public static void main(String[] args) throws Exception {
String ans = readFile("D:\\input.txt");
writeFile(ans, "D:\\output.txt");
}
}
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class FileHelper {
public static String readFile(String filename) throws Exception {
BufferedReader reader = new BufferedReader( new FileReader(filename));
String ans = "", line = null;
while((line = reader.readLine()) != null){
ans += line + "\r\n";
}
reader.close();
return ans;
}
public static void writeFile(String content, String filename) throws Exception {
BufferedWriter writer = new BufferedWriter( new FileWriter(filename));
writer.write(content);
writer.flush();
writer.close();
}
public static void main(String[] args) throws Exception {
String ans = readFile("D:\\input.txt");
writeFile(ans, "D:\\output.txt");
}
}