简单的IO流总结

来源:互联网 发布:网络安全措施有哪些 编辑:程序博客网 时间:2024/05/01 03:53

IO流分为字符流和字节流。

字符流:

Wirter-->FileWriter-->BufferedWriter(newLine())

Reader-->FileReader-->BufferedReader(readLine())

字节流

InputStream-->FileInputStream-->BufferedInputStream

OutputStream-->FileOutputStreamStream-->BufferedOutputStream

一、字符流读写

1、基本字符读写--FileWriter和FileWriter

@Testpublic void test() throws IOException{FileWriter fw=new FileWriter("d://test读写/temp2.txt");//后有true可以续写/*fw.write("123456");fw.write("哈哈哈");fw.flush();fw.close();*///一次读一个/*FileReader fr=new FileReader("d://test读写/temp.txt"); int num;//int 类型的字节码while((num=fr.read())!=-1){System.out.println((char)(num));}*///一次读一个数组FileReader fr2=new FileReader("d://test读写/temp4.txt"); char arr[]=new char[2];//读的个数 一次读两个int num2;//返回值 读的个数while((num2=fr2.read(arr))!=-1){//读到数组中,返回读多少个System.out.print(new String(arr,0,num2));//写到控制台fw.write(arr,0,num2);//写到文件}fr2.close();fw.close();}
2、增强字符读写--BufferedWriter  BufferedReader
@Testpublic void test2() throws IOException{BufferedWriter bw=new BufferedWriter(new FileWriter("d://test读写/temp4.txt"));/*for(int i=0;i<3;i++){bw.write("abc"+i);bw.newLine();//一次写一行bw.flush();}bw.close();*/BufferedReader br=new BufferedReader(new FileReader("d://test读写/temp3.txt"));String line=null;//一次读一行while((line=br.readLine())!=null){System.out.println(line);bw.write(line);}br.close();bw.close();}
二、字节流读写

1、基本字节读写

@Testpublic void test3() throws IOException  {FileOutputStream fos=new FileOutputStream("d://test读写/temp6.txt");/*fos.write("abcdefg".getBytes());fos.close();*//*FileInputStream fis=new FileInputStream("d://test读写/temp5.txt");int num;//一次一个 不常用while((num=fis.read())!=-1){System.out.print((char)num);fos.write(num);}fis.close();*/FileInputStream fis=new FileInputStream("d://test读写/temp5.txt");byte arr[]=new byte[1024];int num2;while((num2=fis.read(arr))!=-1){System.out.println(new String(arr,0,num2));fos.write(arr,0,num2);}fis.close();fos.close();}
2、增强字节读写--FileOutputStream FileInputStream
@Testpublic void test3() throws IOException  {FileOutputStream fos=new FileOutputStream("d://test读写/temp6.txt");/*fos.write("abcdefg".getBytes());fos.close();*//*FileInputStream fis=new FileInputStream("d://test读写/temp5.txt");int num;//一次一个 不常用while((num=fis.read())!=-1){System.out.print((char)num);fos.write(num);}fis.close();*/FileInputStream fis=new FileInputStream("d://test读写/temp5.txt");byte arr[]=new byte[1024];int num2;while((num2=fis.read(arr))!=-1){System.out.println(new String(arr,0,num2));fos.write(arr,0,num2);}fis.close();fos.close();}
2、增强字节读写--BufferedInputStream BufferedOutputStream
@Testpublic void test4() throws IOException{BufferedInputStream bis=new BufferedInputStream(new FileInputStream("d://test读写/1.jpg"));BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("d://test读写/2.jpg"));byte arr[]=new byte[1024];int num;while((num=bis.read(arr))!=-1){//读到数组返回的个数bos.write(arr,0,num);}bis.close();bos.close();}
    小结:
    字符流写入文件直接fw.write 字节流写入文件要fw.write("abcdef0".getBytes())
    字符流一个一个从文件中读取输出时,要(char)num,字节流一个一个从文件中服务输出时也要(char)num
    字符流一个一个从数组中读输出控制台时要new String(arr,0,num)转成字符串输出,字节流同
    字符流字节流从数组中读后写入文件,不必上述步骤,直接fos.write(arr,0,arr);



0 0