java文件流操作

原创
2017/03/01 21:57
阅读数 88
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ReadFileTest {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		try {
			//声明文件输入流,指明路径
//			FileInputStream file = new FileInputStream("F:\\study\\数据库\\MYSQL\\SQL6\\MySQL-06.sql");
//			FileReader file = new FileReader("F:\\study\\数据库\\MYSQL\\SQL6\\in.txt");
			BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\study\\数据库\\MYSQL\\SQL6\\MySQL-06.sql"), "UTF-8"));
			StringBuilder s = new StringBuilder();
			
			//声明一个整型变量用于存放读取的数据
			int data=0;
			 
			while((data=file.read())!=-1){
				s.append((char)data);
			}
			System.out.println(s);
			File f = new File("C:\\Users\\jag\\Desktop\\sql.sql");
			if(!f.exists()){
				f.createNewFile();
			}
			FileWriter fw = new FileWriter(f);
			BufferedWriter out = new BufferedWriter(fw);
			out.write(s.toString());

			out.flush(); //此处记得一定要flush 刷新缓冲区,就这段代码来说,如果不flush会发现根本没有数据写入到文件。
			file.close();
			fw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部