BufferedReader read the first line of the file first character loss problem

Source: Internet
Author: User
Tags readline

You need to read every attribute in the file when you parse the PACP file. But each read the first character of each line of content is swallowed, looking for a long time do not know why, and then see the reading of the use of the Read () method, and the return value is type int, The default should be 0 when the variable is initialized, probably just swallowed the first one. (Still not quite clear) read reads only one byte and does not automatically read the next one only reads the first byte and the pointer is not the same thing, but if you use the ReadLine () function, this will not happen.

Of course, this is only the case when using InputStream, if the direct use of filereader this problem will not appear, using read will also read the first character, may be the buffer in the mischief, and the possibility is very large, If you do not use the buffer directly with FileReader can also be fully read, but if the use of buffer, and the use of int return the value of read, then will inevitably have to swallow the first character of the consequences, the specific code posted, as follows:

 1 package Com.zaj;
 2 3 Import Java.io.BufferedReader;
 4 Import Java.io.BufferedWriter;
 5 Import Java.io.FileReader;
 6 Import Java.io.FileWriter;
 7 Import java.io.IOException;         8 9 public class ReadLine {one public static void ReadLine (string file, String writerfile) {13            
try {FileReader fr = new FileReader (file); BufferedReader br = new BufferedReader (FR); FileWriter FW = new FileWriter (writerfile);
Write file operations, the resulting file corresponding to the contents of the document written, Writerfile. 
//system.out.println (Fw.getencoding ())//utf-8 default BufferedWriter bw = new BufferedWriter (FW);
Readoneline String;            
int l; while ((L = br.read ())!=-1) {//This will result in swallowing the first character. Readoneline = new String (Br.readline (
). GetBytes (), "utf-8");
//system.out.println (Readoneline);          /*string l;  
while ((L = br.readline ())!= null) {//if readLine () is intact readoneline = new String (
Br.readline (). GetBytes (), "utf-8");
//system.out.println (Readoneline);
* * Bw.write (readoneline);
Bw.newline ();
System.out.println (Readoneline);
Bw.flush ();
Br.close ();
Panax Notoginseng bw.close ();
Br.close ();
Fw.close ();
Fr.close (); A/D catch (IOException e) {e.printstacktrace (); 
ID Main (string[] args) {readline ("D:\\tddownload\\test\\packwifi.pcap", "d:\\tddownload\\test\\packwifi.txt"); 48} 49}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.