First, there are several points to note: 1.long len = Rf.length (), the length of the file obtained from 0 to len-1 in seek, if Len is used, then return-1 with the Read method, which represents the end of the file. So the index is 0 to length-1.
2. After using the Randomaccessfile read method, the pointer moves one character back, and the readline moves to the beginning of the next line, which means that read is the first character in the next line.
3. text file, whether the end is a blank line is a difference, look at the picture, the former is a blank line, the latter is the last line is not a blank line. This is not what Baidu said this is the text file display effect and said that these two files are the same, in fact, this is wrong. The difference is that the last character of the former is ": 6\r\n", and the last character of the latter is only ": 6". That is, only the rows and rows are \ r \ n, and the first line is not \ r \ n, and the last line is not a blank line.
Now do the exercise:
1. Read each line of text with randomaccessfile positive sequence
public class Everyline {public
static void Main (string[] args) throws IOException, IOException {
//TODO Auto-gen erated method Stub
Randomaccessfile rf = new Randomaccessfile ("E:\\databike.txt", "R");
String Line=null;
while ((Line=rf.readline ())!=null)
{
//system.out.println (line);
System.out.println (New String (Line.getbytes ("iso-8859-1"), "GBK");}
}
This cannot be directly System.out.println (line) because the string encoded in the Randomaccessfile ReadLine method is iso-8859-1, and the latter is the encoding of your IDE, My eclipse's default encoding is GBK, so I want to set it to GBK. It's easy to read each line, just use the method directly.
2. Read each line of text in reverse randomaccessfile. This first version of the while Loop Rf.seek (NextEnd); cannot be Rf.seek (nextend-1);
public class Fromendread {public static void main (String args[]) {Randomaccessfile RF = null;
try {rf = new Randomaccessfile ("E:\\databike.txt", "R"); Long len = Rf.length ();
File length System.out.println ("File start pointer is" +0); Long NextEnd = len-1;
The pointer is from 0 to length-1 String line; Rf.seek (NextEnd);
Seek to the last character int c =-1;
while (NextEnd >= 0) {c = Rf.read (); if (c = = "\ n")//only the rows and rows have \ r \ n, which means that the end of the line on each row is read \ n, and after the read, the//pointer points to the beginning of the line
= New String (Rf.readline (). GetBytes ("Iso-8859-1"), "GBK");
Randomaccessfile's ReadLine method reads the text as iso-8859-1 and needs to be converted to Windows default encoding GBK System.out.println (line);
nextend--; Rf.seek (NextEnd);//This sentence must be in this position, if after nextend--, then lead into the 0 loop after the seek-1 index, reported abnormal//if in read () before, then lead to enter 0 loop, because the read pointer to 1, the first line reads less than one character if (NextEnd = = 0) {//When the file pointer is back to the beginning of the file, the first row of the output System.out.println (new StriNg (Rf.readline (). GetBytes ("Iso-8859-1"), "GBK");
} nextend--;
} catch (FileNotFoundException e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
Finally {try {if (RF!= null) rf.close ();
catch (IOException e) {e.printstacktrace (); }
}
}
}
There is a second version, is a small change in the loop, the effect is the same. The reason for this change is that the second version seems more consistent with my way of thinking. This second version of the while loop is Rf.seek (nextend); it can be Rf.seek (nextend-1); the difference is that the latter has a less frequent cycle.
public class Fromendreadnew {public static void main (String args[]) {Randomaccessfile RF = null;
try {rf = new Randomaccessfile ("E:\\databike.txt", "R"); Long len = Rf.length ();
File length System.out.println ("File start pointer is" +0); Long NextEnd = len-1;
The pointer is from 0 to length-1 String line; Rf.seek (NextEnd);
Seek to the last character int c =-1;
while (NextEnd >= 0) {c = Rf.read (); if (c = = "\ n")//only the rows and rows have \ r \ n, which means that the end of the line on each row is read \ n, and after the read, the//pointer points to the beginning of the line
= New String (Rf.readline (). GetBytes ("Iso-8859-1"), "GBK");
System.out.println (line);
} if (NextEnd = = 0) {//When the file pointer is back to the beginning of the file, the output first row rf.seek (0);//No need to prepare for next time, but because the read () method pointer from 0 to 1, it needs to be reset to 0
System.out.println (New String (Rf.readline (). GetBytes ("Iso-8859-1"), "GBK"));
else {Rf.seek (nextend-1);//Prepare for Next loop} nextend--;
} catch (FileNotFoundException e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
Finally {try {if (RF!= null) rf.close ();
catch (IOException e) {e.printstacktrace (); }
}
}
}
However, both programs require that the last line not be a blank line, and if so, delete it manually and save it. The idea is that every line except the first line, all ahead, \ r \ n So the loop goes from the back forward, and once read \ n, it starts reading (because after read to the top of the row, the pointer moves back, just to the beginning of the next line, and then the row, and then all the data for that row is read).
3. Read the first line
public class Firstline {public
static void Main (string[] args) throws IOException {
//TODO auto-generated method Stub
Randomaccessfile rf = new Randomaccessfile ("E:\\databike.txt", "R");
String s=rf.readline ();
System.out.println (s);
}
4. Read the last line (end behavior blank row or not blank line)
Last line is not empty:
public class LastLine {public
static void Main (string[] args) throws IOException {
//TODO auto-generated method Stub
Randomaccessfile rf = new Randomaccessfile ("E:\\databike.txt", "R");
Long len = Rf.length (); File length
long nextend=len-1;
int c =-1;
while (NextEnd > 0)
{
rf.seek (nextend);
c = Rf.read ();
if (c = = ' \ n ')
{
String line = new String (Rf.readline (). GetBytes ("Iso-8859-1"), "GBK");
Randomaccessfile's ReadLine method reads the text as iso-8859-1 and needs to be converted to Windows default encoding GBK
//line=rf.readline ();
System.out.println (line);
break;
nextend--;//is a cyclic control condition that does not relate to pointer movement}}
End Behavior Empty line: Add a flag, if the first time encountered first set back
public class Lastlineempty {public
static void Main (string[] args) throws IOException {
Randomaccessfile rf = new Randomaccessfile ("E:\\databike.txt", "R");
Long len = Rf.length (); File length
long nextend=len-1;
int c =-1;
Boolean b=false;
while (NextEnd > 0)
{
rf.seek (nextend);
c = Rf.read ();
if (c = = ' \ n ')
{
if (b==false)
{
b=true
}
else
{
String line = new String (Rf.readline (). GetBytes ("Iso-8859-1"), "GBK");
System.out.println (line);
break;
}
}
nextend--;
}
Rf.close ();
}
5. Read the first few lines of the positive sequence. Use the counter to judge.
public class Frontany {public
static void Main (string[] args) throws IOException {
//TODO auto-generated method Stub
Randomaccessfile rf = new Randomaccessfile ("E:\\databike.txt", "R");
String Line=null;
int count=1;
int want=5;
while ((Line=rf.readline ())!=null)
{
if (count==want)
System.out.println (New String (Line.getbytes () Iso-8859-1 ")," GBK ");
count++
}}
}
6. Read the first few lines in reverse. As long as the requirement is not the last line, it doesn't matter if the last line is a blank line, and it can have results. But if you want the last line and the last line is empty, then you need to add a judgment condition to the IF.
public class Backany {public
static void Main (string[] args) throws IOException {
//TODO auto-generated method s Tub
//Assuming that the last line is not a blank line
randomaccessfile RF = new Randomaccessfile ("E:\\databike.txt", "R");
Long len = Rf.length (); File length
long nextend=len-1;
int c =-1;
int count=1;
int want=5;
while (NextEnd > 0)
{
rf.seek (nextend);
c = Rf.read ();
if ((c = ' \ n ') &count==want)
{
String line = new String (Rf.readline (). GetBytes ("Iso-8859-1"), "GBK ");
System.out.println (line);
break;
else if (c = = ' \ n ')
{
count++;
}
nextend--
}}
}
It is really convenient to read a file with Randomaccessfile because the Seek method can be arbitrarily