一、我们来看python的很简单:
1、读文件:
with open("/path/file","r") as fr:
for line in fr.readlines():
do_somethings(line)
2、写文件:
with open("/path/file","w/a") as fr:
fr.write("ssssssss")
二、上文知识一个引子,不是重点,还是来学习java的文件读写操作吧:
最常用的还是按行读写,当然后面也会带一点其他读写方法:
1、按行读取:
File file = new File("绝对路径");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
3 String str = null;
while((str = bufferedReader.readLine()) != null)
5 {
System.out.println(str);
}
bufferedReader.close();
2、按行写入:
FileWriter filew = new FileWriter("绝对路径",true);
//true表示追加,否则是覆盖写,覆盖写不需要true
filew.write("\ntest\n");
filew.close();
List<String> b = Files.readAllLines(Paths.get("/Users/a003797/Desktop/a1.txt"));
for(String item:b) {
System.out.println(item) ;
}
3、其他读取方式:
//读取全部字节:
byte[] a = Files.readAllBytes(Paths.get("/Users/a003797/Desktop/a1.txt"));
三、输入处理
import java.io.*; public class test{
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
String string = br.readLine();
System.out.println(string);
}
}
}