文件输入输出流的学习

1.单个字节形式的字节输入输出(效率较低)

    (1)单个字节的输入流

package qwe;
import java.io.*;
public class inputStream1 {
public static void main(String[] args) {
FileInputStream in=null;
try {
in=new FileInputStream("a.txt");
int n; //用以接收输入流从文件读取的单个字节, read()函数返回的是字节对应的ascll码,当读不到数据时返回-1
while((n=in.read())!=-1) {
System.out.print((char)n);//将ascall码转化为字节并且输出
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(in!=null) {
try {
in.close();//流的关闭
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

(2)单个字节的输出流

package qwe;

import java.io.FileOutputStream;
import java.io.IOException;
public class outputStream {
public static void main(String[] args) throws IOException {
FileOutputStream ou=new FileOutputStream("b.txt");
for(int i=0;i<10;i++) {     
ou.write(97);
}
}
}

(3)基于单个字节输入输出流的文件复制

FileInputStream in=null;
FileOutputStream ou=null;
try {
in=new FileInputStream("a.txt");  //这是被复制的文件名
ou=new FileOutputStream("b.txt");    //这是复制出的文件的文件名,此处是相对路径,如果需要绝对路径,形式为:"C://Users//user//Desktop//abc.txt"  即可
int n;
while((n=in.read())!=-1) {
ou.write(n);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try{
if(in!=null) {
in.close();
}
if(ou!=null) {
ou.close();
}
}catch (IOException e) {

}

}

注意: 1.ascll码里面并没有中文对应的字节,所以单字节形式的输入输出流无法处理中文等特殊字符的乱码问题

2.GBK每个汉字两个字节,而UTF-8每个汉字三个字节。

3.如果出现乱码则可以用String(byte[] ,"合适的编码方式")或则其他可以自己设置编码的函数

2.字节数组形式的文件输入输出流

(1)多字节输入流

package qwe;
import java.io.*;
import java.lang.reflect.Array;
import java.util.Arrays;
public class inputStream1 {
public static void main(String[] args) {
FileInputStream in=null;
try {
in=new FileInputStream("a.txt");

int n;
byte b[]=new byte[5];   //此处一般写1024 ,但是本人认为即便是1024也不能完全避免中文乱码,因为当文件中文字符多到大于1024字节后,下一次读取也会因为字节分割的问题造成乱码的出现                          
while((n=in.read(b))!=-1) {                         //以字节数组的形式读的话,每次读取的5个字节会放入缓冲区,所以不满5个字节的时候,更新完新前几个字节后,剩下的内容还是上一个5字节的内容,此时的返回值为所读文件的字节个数。
System.out.println(new String(b,0,n));          
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(in!=null) {
try {
in.close();//流的关闭
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

(2)多字节输出流(部分代码)

public class outputStream {
public static void main(String[] args) throws IOException {
FileOutputStream ou=new FileOutputStream("b.txt");
for(int i=0;i<10;i++) {
ou.write("he真实的\r\n".getBytes());         //在windows系统下用/r/n来表示的换行。
}
}

(3)多字节的文件复制

public void test() {
FileInputStream in=null;
FileOutputStream ou=null;
try {
in=new FileInputStream("a.txt");
ou=new FileOutputStream("b.txt");
byte a[]=new byte[1024];
int n;
while((n=in.read(a))!=-1) {
ou.write(a,0,n);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try{
if(in!=null) {
in.close();
}
if(ou!=null) {
ou.close();
}
}catch (IOException e) {
}
}
}
}

注意:

      1.FileInputStream每次都是从硬盘读入,而BufferedInputStream大部分是从缓冲区读入。读取内存速度比读取硬盘速度快得多,因此BufferedInputStream效率高。
  BufferedInputStream的默认缓冲区大小是8192字节。当每次读取数据量接近或远超这个值时,两者效率就没有明显差别了。

猜你喜欢

转载自www.cnblogs.com/icxl/p/10469698.html