如何写出文本输出:可以使用printwriter,
File file = new File("****");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(fos);
pw.write("hello i am l");
pw.flush();
如何读入文本:
1,最简单的处理方式就是使用Scanner类,或者可以将短小的文本文件读入到一个字符串中
Path path = Paths.get("***");
try {
byte[] bs = Files.readAllBytes(path);
String string = new String(bs, Charset.defaultCharset());
System.out.println(string);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2,如果想要将这个文件一行一行的读入,可以调用
List<String> list = Files.readAllLines(Paths.get("****"));
3,如果文件太大,可以选择将行惰性处理为一个stream<string>对象