java io 体系结构
1 字节流(InputStream,OutputStream)
直接和文件交互,没有缓存区。所以即便没有close(),数据也已写入文件中。
操作的是字节(byte[]),可对任何文件进行读写操作
private static void write() throws IOException{
File file = new File("D://123.java");
FileOutputStream fos = new FileOutputStream(file);
String abc ="我是中国人";
fos.write(abc.getBytes());
// 字节流不用缓存,所以哪怕没有close,数据也写入到了文件中
fos.close();
}
private static void read() throws IOException{
File file = new File("D://123.txt");
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[(int)file.length()];
fis.read(b);
// 字节流由于读取的是byte,转换成字符的时候容易出现乱码,
System.out.println(new String(b,"utf-8"));
// fis.close();
}
private static void copy() throws IOException{
File file = new File("d:\\Test1.java");
FileInputStream fis = new FileInputStream(file);
// 复制
FileOutputStream fos = new FileOutputStream("d:\\123.java");
//自定义缓冲对象
byte[] b = new byte[1024];
int n=0;
while((n=fis.read(b))!=-1){
fos.write(b, 0, b.length);
}
fis.close();
fos.close();
}
2 字符流(Reader Writer)
有缓存,字符都是在缓存中存储的,传输和文件存储的其实还是字节。
一个字符char(unicode)包含2个字节byte。操作的是char[]
字符流只能处理字符或者字符串,而字节流可以处理任何二进制文件
Writer.write(“123”)后,必须writer.close()或者writer.flush()强制将缓存中的字符刷新到字节中,才能完成文件写入。
Reader.read(char[] c),从字节流读取字符,不用reader.close()也能获取c的内容。
private static void writer() throws IOException{
File file = new File("d:" + File.separator + "123.txt");
Writer writer = new FileWriter(file);
writer.write("我是你爷爷");
// 必须得close,这样缓存的数据才会进入到文件中;否则文件内容被置空
writer.close();
}
private static void reader() throws IOException {
File file = new File("d:" + File.separator + "123.txt");
Reader input = new FileReader(file);
char[] c = new char[1024];
int temp = 0;
int len = 0;
while ((temp = input.read()) != -1) {
c[len] = (char) temp;
len++;
}
input.close();
System.out.println(new String(c, 0, len));
}
/**
* 字符流不能支持jpg格式的write
* @throws IOException
*/
private static void readPic() throws IOException {
FileReader fis = new FileReader("C:\\Users\\ycig\\Pictures\\Saved Pictures\\IMG_1483.JPG");
FileWriter fos = new FileWriter("C:\\Users\\ycig\\Pictures\\Saved Pictures\\copy2.jpg");
int n;
while ((n = fis.read()) != -1) {
fos.write(n);
System.out.println((char)(n));
}
fis.close();
fos.close();
}
3 InputStreamReader OutputStreamWriter
InputStreamReader
传入字节流,读取到缓冲区变成成字符流,需要charset指定字符集,父类是reader,子类有FileReader。所以FileReader.read()调用的就是InputStreamReader中的read()方法
构造方法需要传入InputStream字节流
int read(char[] c)将inputstream字节流按照charset字符集读取到缓冲区字符c中。
// Reader
public int read(char cbuf[]) throws IOException {
return read(cbuf, 0, cbuf.length);
}
abstract public int read(char cbuf[], int off, int len) throws IOException;
// InputStreamReader
public int read(char cbuf[], int offset, int length) throws IOException {
return sd.read(cbuf, offset, length);
}
一个文件只能同时有一个流进行read操作,下面这段代码执行结构只有inputStreamReader会输出文件内容,因为InputStreamReader比FileInputStream先读取文件,所以占用了通道。同理,如果isr.read()在fis.read()后面,则只有fileInputStream会输出内容。
private static void read() throws IOException{
File file = new File("D://123.txt");
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[(int)file.length()];
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
char[] c = new char[1024];
int len = isr.read(c);
fis.read(b);
// 字节流由于读取的是byte,转换成字符的时候容易出现乱码,
System.out.println("====fileInputStream====");
System.out.println(new String(b,"utf-8"));
System.out.println("=====inputStreamReader====");
System.out.println(c);
// fis.close();
}
OutputStreamWriter
获取OutputStream,将字符串或者char写入输出流,即缓冲区字符流转换成字节流写入到OutputStream中。write方法是父类是Writer,子类有FileWriter。
构造方法传递OutputStream
write()的方法有
// Writer
abstract public void write(char cbuf[], int off, int len) throws IOException;
// OutputStreamWriter
public void write(String str, int off, int len) throws IOException {
se.write(str, off, len);
}
// StreamEncoder
private void writeBytes() throws IOException {
this.bb.flip();
int var1 = this.bb.limit();
int var2 = this.bb.position();
assert var2 <= var1;
int var3 = var2 <= var1 ? var1 - var2 : 0;
if (var3 > 0) {
if (this.ch != null) {
assert this.ch.write(this.bb) == var3 : var3;
} else {
// 调用outputStream.write()
this.out.write(this.bb.array(), this.bb.arrayOffset() + var2, var3);
}
}
this.bb.clear();
}
下面这段代码,两个字符串都能写入文件,最后结果是 我是中国人abc
private static void write() throws IOException{
File file = new File("D://123.java");
FileOutputStream fos = new FileOutputStream(file);
String abc ="我是中国人";
OutputStreamWriter osw = new OutputStreamWriter(fos);
fos.write(abc.getBytes());
osw.write("abc");
osw.flush();
// 字节流不用缓存,所以哪怕没有close,数据也写入到了文件中
fos.close();
}