IO streams, copy the file byte stream, a character stream buffer copy files +

If the flow divided by JAVAIO: two kinds of input and output streams

          Input stream the base class: InputStream Reader

          Base class output stream: OutputStream Writer

    If the data unit is divided by: the byte stream and character stream

          Base classes of the input and output byte stream: InputStream OutputStream

          Base class for character stream input and output: Reader Writer

 

Copy the contents of the file byte stream

public static void main(String[] args) {
		//字节流复制文件内容
		InputStream io=null;
		OutputStream os=null;
		try {
			io=new FileInputStream("D:/a.txt");
			os=new FileOutputStream("D:/c.txt");
			int a=0;
			byte[] b=new byte[1024];
			while((a=io.read(b))!=-1){
				os.write(b,0,a);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				os.close();
				io.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

  Note: java path symbol is: "/" "\\" and our system path symbol is: "\" I remember off the flow when reading and writing files is completed, otherwise they will not read the contents of the file

 

+ Character stream buffer to copy the contents of the file

public static void main(String[] args) {
		//字符流+缓冲复制文件文件内容
		Reader read=null;
		BufferedReader br=null;
		Writer write=null;
		BufferedWriter bw=null;
		
		try {
			read=new FileReader("D:/a.txt");
			br=new BufferedReader(read);
			write=new FileWriter("D:/d.txt");
			bw=new BufferedWriter(write);
			String s="";
			while((s=br.readLine())!=null){
				bw.write(s);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				bw.close();
				write.close();
				br.close();
				read.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

  Note: Remember to turn off when the order flow: After the first opening of relations, after the opening of the first turn

Guess you like

Origin www.cnblogs.com/LittleBoys/p/12090904.html