Java 对文件的读取操作

  

package pack;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;

public class InOutFileStream {

	public static void main(String[] args) throws Exception {
		
	//字符流操作 
		
		//读取
		File file = new File ("E:\\open_back16-2-26.sql");
		
		FileReader reader = new FileReader(file);
		
		int r = 0;
		char []rchar = new char[1024];
		
		String text = "";
		
		 while ((r = reader.read(rchar)) != -1) {
			 text +=new String(rchar,0,r);
		 }
		 reader.close();
		
		System.out.println(text);
		
		File outfile = new File ("E:\\out.txt");
		if (!outfile.exists()) {
			outfile.createNewFile();
		}
		
		//写入
		FileWriter fileWriter = new FileWriter(outfile);
		
		fileWriter.write(text);
		fileWriter.flush();
		fileWriter.close();
		
		
		//字节流操作
		
		//读取文件
		FileInputStream fis = new FileInputStream(new File ("E:\\back.sql"));
		
		File outFile = new File("E:\\out1.txt");
		if (!outFile.exists()) {
			outFile.createNewFile();
		}
		
		
		//将读入的back.sql 写入到 e:out1.txt 文件中
		FileOutputStream fos = new FileOutputStream(outFile);
		
		int rr = 0;
		
		byte[]fchar = new byte[1024];
		
		while ((rr = fis.read(fchar)) != -1) {
			
			fos.write(fchar,0,rr);
		}
		fos.flush();
		fos.close();
		fis.close();
	}
}

  

posted @ 2016-06-21 18:02  紅_塵  阅读(209)  评论(0编辑  收藏  举报