2015-11-02 55 views
0

所以我有這樣如何閱讀客戶提供的日誌文件?

blah blah blah blah blah blah blah 
blah blah blah blah blah blah blah 
<someTag>someDetail</someTag> 
blah blah blah blah blah blah blah 
blah blah blah blah blah blah blah 
<someTag>someDetail</someTag> 
blah blah blah blah blah blah blah 
blah blah blah blah blah blah blah 

這裏的日誌文件是我的代碼讀取該文件

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 

public class DummyReadFile { 

    public static void main(String[] args) { 

     BufferedReader br = null; 

     try { 

      String sCurrentLine; 
      br = new BufferedReader(new FileReader("C:/Hussi/logFile.log")); 

      while ((sCurrentLine = br.readLine()) != null) { 
       //System.out.println(sCurrentLine); 
       if(sCurrentLine.contains("<")) 
       { 
        System.out.println("==>>>"+sCurrentLine); 
       } 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (br != null)br.close(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } 

} 

,但在我的輸出我得到

b l a h b l a h b l a h 
< s o m e T a g >s o m e D e t a i l </s o m e T a g > 
b l a h b l a h b l a h 
< s o m e T a g >s o m e D e t a i l </s o m e T a g > 
b l a h b l a h b l a h 
< s o m e T a g >s o m e D e t a i l </s o m e T a g > 

之間的空間字符實際上是一些方塊

我該怎麼做才能讀取f ile正確

我必須提及,文件格式,這樣的東西?

this is the console

+0

sCurrentLine.replaceAll(」」, 「」); –

+1

如何使用XML解析器獲取標籤? – davejagoda

回答

1

看來你的文件是UTF-16的文本編碼格式

我已經改變兩行現在的計劃工作好。

FileInputStream fin = new FileInputStream("C:/temp/test3.txt"); 
br = new BufferedReader(new InputStreamReader(fin, "UTF-16LE")); 

改性工作程序

import java.io.BufferedReader; 
import java.io.FileInputStream; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class DummyReadFile { 

    public static void main(String[] args) { 

     BufferedReader br = null; 

     try { 

      String sCurrentLine; 
      FileInputStream fin = new FileInputStream("C:/temp/test3.txt"); 
      br = new BufferedReader(new InputStreamReader(fin, "UTF-16LE")); 
      //br = new BufferedReader(new FileReader("C:/temp/test3.txt")); 

      while ((sCurrentLine = br.readLine()) != null) { 
       System.out.println(sCurrentLine); 
       /*if(sCurrentLine.contains("<")) 
       { 
        System.out.println("==>>>"+sCurrentLine); 
       }*/ 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (br != null)br.close(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } 

}