Method one: With Pushbackreader, a buffer of length 1 is used by default, and the size of the buffer needs to be indicated when creating an object.
public static void Function1 (File f) throws Ioexception{try (Pushbackreader PR = new Pushbackreader (new FileReader (f), 64)) {char[] cbuf = new CHAR[32]; String lastcontent = ""; String content = ""; int hasread = 0;while ((Hasread = Pr.read (cbuf)) > 0) {content = new String (cbuf, 0, hasread); int tar Get = (lastcontent + content). IndexOf ("New Pushbackreader"), if (Target > 0) {pr.unread ((lastcontent + content). ToCharArray ()); if (target > +) {cbuf = new Char[target];p r.read (cbuf, 0, Target); System.out.println (New String (cbuf, 0, Target); break;}} Else{system.out.print (lastcontent); lastcontent = Content;}}}}
Method Two: Using BufferedReader, this class has a ReadLine method, which can read the text in one line, this method is relatively simple.
public static void Function2 (File f) throws Ioexception{try (BufferedReader br = new BufferedReader (new FileReader (f))) { String line = ""; int target = 0;while (line = Br.readline ())!=null) {if (target = Line.indexof ("New BufferedReader") > 0) {System.out.println (line.substring (0, target)); Else{system.out.println (line);}}}
Read the contents of a string in the file, two methods