Public static void main (String [] args ){
Try {
// Write the file first and append it to the end of the file. If the file does not exist, the file is automatically created.
FileWriter fw = new FileWriter ("C:/test.txt", true); // use FileWriter to open the file
PrintWriter pw = new PrintWriter (fw); // load the file with a write pointer
String [] str = {""}; // String to be written
For (String index: str ){
Pw. println (index); // append each time to the end of the file
}
Pw. close (); // close
Fw. close (); // close
// Locate the last row of the file below and read one row at the top
RandomAccessFile raf = new RandomAccessFile ("C:/test.txt", "r"); // This class can locate the file,
// Java
// The unique IO class that can be located
Long len = raf. length (); // get the object length to locate the end
If (len <= 3) {// checks whether the file is empty.
System. out. println ("the flie is NULL! ");
Return;
}
Long pos = len-1; // locate the end Of the file
While (pos> 0) {// determines whether the object has reached the header
-- Pos; // forward pointer of one character and one character
Raf. seek (pos); // locate the position indicated by the file pointer
If (raf. readByte () = '\ n') {// if it is a line break, you can read this line.
System. out. println (raf. readLine ());
}
}
Raf. seek (pos); // Finally, read the first row
System. out. println (raf. readLine ());
Raf. close (); // close
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
System. exit (0 );
Return;
}