I/O Streams -1Byte Streams,Character Streams

父类 子类
Byte Streams InputStream
OutputStream
FileInputStream
FileOutputStream
Character Streams Reader
Writer
FileReader
FileWriter

一般来说呢,不使用byte Streams,其他的流呢都是建立在byte Streams的基础上的。

Byte Streams(FileInputStream/FileOutputStream)

package tutorials;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyBytes {
    public static void main(String[] args) throws IOException{
        FileInputStream in = null;
        FileOutputStream out = null;
        
        try{
            in = new FileInputStream("D:/Learn/tutorials/src/xanadu.txt");
            out = new FileOutputStream("outagain.txt");
            int c;
            
            while((c=in.read()) != -1){
                //read方法返回的是int类型,这个int值是ASCII ,
                //如果读到文件的末尾,那么返回的是-1
                System.out.println(c);
                out.write(c);
            }
        } finally{
            if(in != null){
                in.close();
            }
            if(out != null){
                out.close();
            }
        }
    }
}

Character Streams(FileReader /FileWriter )

package tutorials;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyCharacters {
    public static void main(String[] args) throws IOException{
        FileReader inputStream = null;
        FileWriter outputStream = null;
        
        try{
            inputStream = new FileReader("hah.txt");
            outputStream = new FileWriter("nihao.txt");
            int c;
            
            if((c=inputStream.read()) != -1){
                outputStream.write(c);
            }
        }finally{
            if(inputStream != null){
                inputStream.close();
            }
            if(outputStream != null){
                outputStream.close();
            }
        }
    }
}

上面两段程序唯一不同的地方就是将FileInputStream换成FileReader,FileOutputStream换成FileWriter
还有个区别就是程序中有个int c ,如果使用FileReader保存的是character value的后16位,FileInputStream保存的是character value的后8位。

line-oriented I/O

package tutorials;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class CopyLines {
    public static void main(String[] args) throws IOException{
        BufferedReader inputStream = null;
        PrintWriter outputStream = null;
        
        try{
            inputStream = new BufferedReader(new FileReader("D:/Learn/tutorials/src/hah.txt"));
            outputStream = new PrintWriter(new FileWriter("nihao.txt"));
            //BufferedReader里面放的是Reader,PrintWriter里面放的是Writer,Reader和Writer都是Character Streams
            
            String l;
            while((l = inputStream.readLine()) != null){
                //BufferedReader.readLine方法,如果我们有值返回就返回String,如果没有值了就返回null
                //readline会一行一行的读取内容,识别的机制是读取到换行符(/n)或者回车键(/r)或者(/r/n),能够适应各种操作系统
                outputStream.println(l);
                //printWriter.println方法,这个方法就是把把String中的内容写到PrintWriter中,然后加上换行符号
            }
        }finally{
            if(inputStream != null){
                inputStream.close();
            }
            if(outputStream != null){
                outputStream.close();
            }
        }
    }
}

你可能感兴趣的:(I/O Streams -1Byte Streams,Character Streams)