实例代码三(文件读取,字节流和字符流)

就是复制文件到另一个文件,字节流和字符流的读取

读取字符
import java.io.*;
public class ZifuDemo {
	public static void main(String[] args) {
		FileReader in=null;
		FileWriter out=null;
		int b;
		char a;
		try {
			in=new FileReader("C:/keshe/oldone.txt");
			out=new FileWriter("C:/keshe/newone2.txt");
			while((b=in.read())!=-1) {
				a=(char)b;
				out.write(a);}
			in.close();
			out.close();
		}catch(FileNotFoundException e){
			System.out.println("can not find file!");
		}catch(IOException e1) {
			System.out.println("File read error!");}}}
读取字节
import java.io.*;
public class ZijieDemo {
	public static void main(String[] args) {
		FileInputStream in=null;
		int b=0,count=0;
		char a;
		FileOutputStream out=null;
		try {
			out=new FileOutputStream("C:/keshe/newone1.txt");
			in=new FileInputStream("C:/keshe/oldone.txt");	
		}catch(FileNotFoundException e) {
			System.out.println("can not find file!");
			System.exit(-1);}
		try {
			while((b=in.read())!=-1) {
				a=(char)b;
				out.write(a);}
			in.close();
			out.close();
		}catch(IOException e1){
			System.out.println("File read error!");
			System.exit(-1);}}}


猜你喜欢

转载自blog.csdn.net/weixin_40373090/article/details/80663891