1、写入到指定路径下文件
public static void main(String[] args) throws FileNotFoundException {
File file = new File("E:\\a.txt");
PrintStream printStream=new PrintStream(file);
PrintStream out=System.out;
System.setOut(printStream);
String[] strings={"a.jpg","b.jpg","c.mp4","d.mp4"};
for (int i = 0; i <strings.length; i++) {
System.out.println(strings[i]);
}
System.setOut(out);
System.out.println("写入成功!");
}
2、读取指定路径下文件的内容
public static void main(String[] args) throws IOException {
File file = new File("E:\\a.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String string = ""; //不能初始化为null;
String allcase = new String();
while ((string = br.readLine())!= null){
allcase = allcase +string+"\n";//空格问题自己解决一哈
}
br.close();
System.out.print(allcase);
}