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("文件读取错误!");
}
}