java io 文件读写操作

写:

import java.io.*;

 

String filePath= "F:\\test.txt";

FileWriter fwriter = null;

 

fwriter = new FileWriter(filePath);

fwriter.write(“1”); //这里要放入string类型

fwriter.flush();

fwriter.close();

 

读:

try {

File file = new File(filePath);

if(file.isFile() && file.exists()) {

InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");

BufferedReader br = new BufferedReader(isr);

String lineTxt = null;

while ((lineTxt = br.readLine()) != null) {

System.out.println(lineTxt); //读出的内容 这里是string类型的

}

br.close();

} else {

System.out.println("文件不存在!");

}

} catch (Exception e) {

System.out.println("文件读取错误!");

}

}

 

posted @ 2018-11-29 08:42  诸葛小朙  阅读(670)  评论(0编辑  收藏  举报