大家知道java对于文件处理提供的接口非常的多,功能也非常的强大,下面我对java文件处理类的一些总结,与大家分享,也请大家多多批评
1 先说字节流;java在读入写出文件的时候以字节流的形式进行处理,也就是把文件中的数据以字节的方式序列化。下面就以对文件的操作做解释
1 虚基类:inputStream 实现的类: FileInputStream 功能:从文件读入数据到内存中,实例化:
InputStream inputStream = new FileInputStream(new File("fileName"))
你可以在用缓冲流再对inputStream进行包装:例如
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(new File("in.txt")))
为什么要用缓冲流对其再进行包装是因为操作系统在对物理磁盘进行读写的时候很耗费性能,而建立缓冲流则减少对硬盘的大量读写,让程序更加的高效。以下对缓冲流不做说明,inputStream当从文件中读入数据的时候可以建立一个缓冲数组并且把inputStream字节流填充
byte[] b = new byte[1024];
int len = inputStream.read(b);
//返回的len就是字节流的长度
读入字节流后可以对他进行处理例如在输出的另一个文件中去,如果输出到另一个文件中我们将用到字节流的另一个虚基类 OutputStream 实现类:FileOutputStream 功能就是把字节流在输送到另一个文件中去,你也可以用相应的缓冲流进行包装(BufferedOutputStream) 接着上例,要把一段字节流输出到文件中去,你先要建立文件输出流
OutputStream outputStream = new FileOutputStream(new File("out.txt"));
//当len为-1的时候说明到文件尾
while(len != -1) {
outputStream.write(b,0,len);
len = inputStream.read(b, 0 ,len);
}
//最后别忘了关掉输入输出流
inputStream.close();
outputStream.close();
//如果输出流的时候您用到了BufferedOutputStream时候别忘了先刷新一下在关闭
//如果文件中含有中文汉字的时候你可以选择这种操作方式
2、字符流,java也提供了文件读入写出的时候以字符为单位进行操作,字符输入流虚基类Reader 实现类 FileReader 输出流虚基类 Writer 实现类: FileWriter 你也可以选择缓冲流对齐进行包装,reader的缓冲流 BufferedReader Writer对应的缓冲流 BufferedWriter 从文件读入字符流到内存
Reader reader = new FileReader(new File("in.txt"));
//把缓冲流赋给字符数组 len是字符流的长度
char[] c = new char[1024];
int len = reader.read(c);
如果再把上面的字符流输出到文件中 便可以用到FileWriter
Writer writer = new FileWriter(new File("out.txt"));
while(len != -1) {
writer.write(c,0,len);
len = reader.read(c,0,len);
}
//最后别忘了关闭输入输出流,如果用到缓冲流别玩了先flash一下在关闭
reader.close();
writer.close();
字符流和字节流的结果差别就是当数据从一个文件(in.txt)读入又输出到另一个文件(out.txt)的时候如果文件含有中文字节流则不会出现异常,而字符流输出的out.txt中文则会乱码,但是字符流的操作要比字节流快得过。
如果你想操作快一些还不想让中文乱码的时候你可以用到字节流向字符流转化的接口
3 字节向字符转化的类 输入类 InputStreamReader 输出类:OutputStreamReader
java 代码
//注意字符集编码 gb2312 因为文本文件的记事本编码为gb2312,如果转码的时候不是//gb2312的时候输出的文件的时候也会乱码
InputStreamReader b2c = new InputStreamReader(new FileInputStream(new File("fileout.txt")), "gb2312");
OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(new File("123454.txt")));
//BufferedOutputStream bufferedOut1 = new BufferedOutputStream(new FileOutputStream(new File("1111112.txt")));
int ci1 = 0;
while ((ci1 = b2c.read()) != -1) {
System.out.print((char)ci1);
ow.write(ci1);
}
ow.flush();
b2c.close();
ow.close();
上面是自己的一点心得,下面附上没事的时候测试的代码,大家可以根据自己的实际情况修改相应的参数进行测试
package question;
import java.awt.PrintJob;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Queue;
public class Q_01 {
public static void main (String[] args) throws Exception {
File file = new File("testin.txt");
String content = "您好! java世界";
//利用outputStream写文件
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
byte[] b = content.getBytes();
int len = b.length;
bufferedOutputStream.write(b, 0, len);
bufferedOutputStream.flush();
bufferedOutputStream.close();
//利用writer写文件
File file1 = new File("testin1.txt");
Writer writer = new FileWriter(file1);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
char[] c = content.toCharArray();
len = c.length;
bufferedWriter.write(c, 0, len);
bufferedWriter.flush();
bufferedWriter.close();
//先从文件读再写writer
File fileread = new File("fileout.txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileread));
BufferedWriter bwriter = new BufferedWriter(new FileWriter(new File("writerout.txt")));
int k;
while ((k = bufferedReader.read()) != -1) {
bwriter.write(k);
}
bwriter.flush();
bwriter.close();
bufferedReader.close();
//先从文件写outputStream
File fileout = new File("fileout.txt");
BufferedInputStream bufferedIn = new BufferedInputStream(new FileInputStream(new File("fileout.txt")));
BufferedOutputStream bufferedOut = new BufferedOutputStream(new FileOutputStream(new File("111111.txt")));
int ci = 0;
byte[] bb = new byte[1024];
int ll = bufferedIn.read(bb);
while (ll != -1) {
bufferedOut.write(bb, 0, ll);
ll = bufferedIn.read(bb, 0, ll);
}
String con = new String(bb);
con = new String(con.getBytes("gb2312"), "utf-8");
System.out.println(con);
System.out.println();
bufferedOut.flush();
bufferedIn.close();
bufferedOut.close();
//架设字节码到字符码的桥梁
InputStreamReader b2c = new InputStreamReader(new FileInputStream(new File("fileout.txt")), "gb2312");
OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(new File("123454.txt")));
//BufferedOutputStream bufferedOut1 = new BufferedOutputStream(new FileOutputStream(new File("1111112.txt")));
int ci1 = 0;
while ((ci1 = b2c.read()) != -1) {
System.out.print((char)ci1);
ow.write(ci1);
}
ow.flush();
b2c.close();
ow.close();
}
}