JAvaで末行からファイルを1行ずつ上へ読み込む
1853 ワード
/**************
* Test
*
* @param args
*/
public static void main(String[] args) {
try {
// , ,
FileWriter fw = new FileWriter("C:/test.txt", true); // FileWriter
PrintWriter pw = new PrintWriter(fw); //
String[] str = { "" }; //
for (String index : str) {
pw.println(index); //
}
pw.close(); //
fw.close(); //
// ,
RandomAccessFile raf = new RandomAccessFile("C:/test.txt", "r"); // ,
// java
// IO
long len = raf.length(); // ,
if (len <= 3) { //
System.out.println("the flie is NULL!");
return;
}
long pos = len - 1; //
while (pos > 0) { //
--pos; //
raf.seek(pos); //
if (raf.readByte() == '
') { // ,
System.out.println(raf.readLine());
}
}
raf.seek(pos); //
System.out.println(raf.readLine());
raf.close(); //
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
return;
}