First, we look at Python is very simple:
1. Read the file:
1 with open ("/path/file","R") as fr:2 for in fr.readlines ():3 do_somethings (line)
2. Write files:
1 with open ("/path/file","w/a") as fr: 2 fr.write ("ssssssss")
Second, the above knowledge a primer, not the focus, or to learn the Java file read and write operations:
The most common is to read and write by line, of course, there will be a bit of other reading and writing methods:
1, read by line:
1File File =NewFile ("Absolute path");2BufferedReader BufferedReader =NewBufferedReader (Newfilereader (file));3String str =NULL;4 while(str = bufferedreader.readline ())! =NULL) 5{ 6 System.out.println (str); 7 }8Bufferedreader.close ();
2, write by line:
1 New FileWriter ("Absolute path",true); 2 // true indicates append, otherwise overwrite write, overwrite write does not need true 3 filew.write ("\ntest\n"); 4 filew.close ();
1 list<string> b = files.readalllines (Paths.get ("/users/a003797/desktop/a1.txt")); 2 for (String item:b) {3 System.out.println (item) ; 4 }
3. Other reading methods:
1 // Read all bytes: 2 byte [] A = Files.readallbytes (Paths.get ("/users/a003797/desktop/a1.txt"));
Third, input processing
1 ImportJava.io.*;2 3 Public classtest{4 Public Static voidMain (String args[])throwsIOException {5BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); 6 while(true){7String string =br.readline ();8 System.out.println (string);9 }Ten } One}
Java read-write file and input processing by line