文件的输入输出,之前看过多次,但每次都忘,这次总结一下,以备之后翻看;
代码:
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Paths;
import java.util.Scanner;
public class FirstForTest {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(Paths.get("E:\\Eclipse\\workspace\\ForStudy\\src\\Torres.txt"));
while(in.hasNext()) {
System.out.println(in.nextLine()); //read the file's content and print it on console;
}
in.close();
PrintWriter out = new PrintWriter("E:\\Eclipse\\workspace\\ForStudy\\src\\Lan.txt");
out.println("Hi, Lan; \n I touch you in Java-Eclipse!"); //write the content on specific file.
out.close();
}
}
注意:如果文件名中包含"\", 就要记住在每个反斜杠之前再加一个额外的反斜杠;