IO of the byte stream and character stream

IO of the byte stream and character stream
byte stream and character stream have mythical difference does it make?
Any data byte streaming image video text, etc.
The character stream can only handle plain text (plain text is also recommended when using a character stream)


The computer is the same Oh, so do not flow processing non-character file with the characters.
Because this is only the first IO of the opening so briefly

1. thinking

Theme hold a four-point
// Select Source 1
// 2 Select stream
// 3 Operation
// 4 release resources

2. Code

Byte stream

package IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
//1  选择源 
//2 选择流
//3 操作 
// 4 释放资源
public class 标准版文件字节流 {
public static void main(String[] args) {
	标准版文件字节流 f= new 标准版文件字节流();
	f.out();
}
public void read() {//字节文件读取流
	//选择源
		File file = new File("king.txt");
		//选择流
		InputStream in= null;
		try {
		in = new FileInputStream(file);
			
		//操作(分段读取)
			byte [] flush =new byte [128];//缓冲容器
			int len = -1;//接收容器
			while((len = in.read(flush))!=-1) {
				 String str=new String(flush,0,len);//字节数组-->字符串  解码
				 System.out.println(str);
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {//释放资源
			
			if(null!=in) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		
	}
public void out() {
	//选择源
	File file  =new File("king.txt");
	//选择流
	OutputStream output =null;
	try {
		output = new FileOutputStream(file ,true);
		//操作
		String s ="我又来了";
		byte [] n=s.getBytes();//编码
		output.write(n, 0, n.length);
		output.flush();//刷新
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {//关闭流
		if(null!=output) {
			try {
				output.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
}

Copy the file byte stream

package IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class 文件的拷贝 {
public static void main(String[] args) {
	文件的拷贝 f = new  文件的拷贝();
	f.copy("1.jpg", "测试的文件.jpg");
}
public void copy(String srcpath,String destpath) {
	//选择源
	//File file1  =new File("1.jpg");
	//File file2  =new File("copy.jpg");
	File file1  =new File(srcpath);
	File file2  =new File(destpath);
	//选择流
	InputStream in =null;
	OutputStream out = null;
	try {
		in=new FileInputStream(file1);
		out =new FileOutputStream(file2);
		//操作
		byte [] flush = new byte [1024];
		int len =-1;
		while((len=in.read(flush))!=-1) {
			out.write(flush, 0, flush.length);
		}
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	//释放资源
	try {
		if(null!=out) {
			out.close();
		}
	} catch (Exception e) {
		// TODO: handle exception
	}
	try {
		if(null!=in) {
			in.close();
		}
	} catch (Exception e) {
		// TODO: handle exception
	}
}
}

Character stream

package IO;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class 文件字符输入输出流 {
public static void main(String[] args) {
	文件字符输入输出流 f= new 文件字符输入输出流();
	f.out();
}
public void in() {
	//选择源
		File file  = new File("king.txt");
		//选择流
		Reader read=null;
		 try {
			read=new FileReader(file);
			char [] flush = new char [128];
			int len =-1;
			while((len=read.read(flush))!=-1) {
				System.out.println(flush);
			}
			//操作
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {//释放资源
			if(null!=read) {
				try {
					read.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		
	}
 public void out() {
	 File file =new File("文件字符流测试.txt")	;
	 Writer write =null;
	 try {
		write=new FileWriter(file);
		String test ="我实在不想玩了";
		char [] flush =test.toCharArray();//字符串->数组
		write.write(flush, 0,flush.length);
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		if(null!=write) {
			try {
				write.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
 }
}

Published 27 original articles · won praise 5 · Views 655

Guess you like

Origin blog.csdn.net/qq_44620773/article/details/103962764