2016-04-08 58 views
1

我目前正在創建一個工具,可以提取和搜索存儲在大學項目的智能手錶上的數據。掃描儀無法讀取文件中的外國字符

我已經能夠從包含智能手錶連接到的手機的藍牙MAC地址的智能手錶中提取特別名爲「Node.db」的文件。我正在嘗試創建一個掃描器,而不是掃描此「node.db」文件並打印出MAC地址。

這是我目前擁有的代碼:

// Identify the location of the node.txt file  
File file = new File("C:\\WatchData\\node.txt"); 
// Notify the user that Bluetooth extraction has initalized 
Txt_Results.append("Pulling bluetooth data..."); 
     Scanner in = null; 
     try { 
      in = new Scanner(file); 
      while(in.hasNext()) 
      { // Scan till the end of the file 
       String line=in.nextLine(); 
       // Scan the file for this string 
       if(line.contains("settings.bluetooth")) 
       // Print the MAC Address string out for the user 
        System.out.println(line); 
      } 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }  

先前的功能轉換爲.txt文件。 代碼搜索每一行並查找「settings.bluetooth」,如果找到,應打印出包含MAC地址的這一行。但是,我相信node.db文件的格式正在阻止掃描器找到此字符串。我相信文件中的一些數據是經過編碼的。數據如何呈現的例子如下所示。我相信這是黑色字符它不承認:

Picture of file lines

當我在文件中運行的代碼,程序會簡單地掛起並且不提供錯誤信息。我已經離開程序運行了20多分鐘,但仍然沒有成功。

我試圖從文件打印出精確的線條如下圖所示:

Line showing MAC Address of paired device

我已經測試過的文本文件中的代碼沒有這些編碼的字符,可以得出結論,代碼不工作。所以我的問題是:

有沒有一種方法,我可以讓掃描儀跳過它不會在文件中識別字符,以便它可以繼續掃描文件?

在此先感謝。

+0

你有什麼錯誤嗎? – Hackerdarshi

+0

@Hackerdarshi你好。該程序只是掛起,沒有顯示錯誤信息。我已經更新了該問題以包含此信息。 – JPM

+0

您可以在您的問題中包含一些行(包括您希望它打印的行;不是所有行)...... – Hackerdarshi

回答

1

既然你沒有在這裏提供文件,所以我不能編寫代碼來測試你的文件。它看起來像你的文件有不同於Java用來解碼它的編碼。

所以,你需要爲你的輸入流嘗試不同的編碼設置。

通常情況下,您指定的編碼:

String encoding = "UTF-8"; // try "UTF-8" first and also change to other encodings to see the results 
Reader reader = new InputStreamReader(new FileInputStream("your_file_name"), encoding); 

Refer to this post for more information。這篇文章還討論瞭如何編寫代碼來檢測文件的編碼。

順便說一句,在您的文件中顯示的具有黑色背景的解碼字符是ASCII中的一些控制字符。

我也建議你嘗試改變你的文本查看器應用程序的解碼方法,看看你是否真的可以在特定的編碼方法中正確顯示文本。

UPDATE

它看起來像Scanner同時使用其他IO類實際上正常工作無法正常工作。

StringBuilder sb = new StringBuilder(); 

try (BufferedReader reader = new BufferedReader(new FileReader("node.txt"))) { 

    String line; 
    while ((line = reader.readLine()) != null) { 
     sb.append(line); 
    } 

} catch (Exception e) { 
    // TODO: handle exception 
} 


int index = sb.indexOf("settings.bluetooth"); 
if (index != -1) 
    System.out.println(sb.substring(index, index + 18)); 

UPDATE

它看起來像只有當你從文件創建Scanner從文件中讀取數據時將導致異常的Scanner的內心方法之一。但是使用如下的輸入流將始終有效,甚至將其封裝在Scanner中。

try (Scanner s = new Scanner(new FileInputStream("node.txt"))) { 
    while(s.hasNext()) { 
     System.out.println(s.next()); 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

UPDATE

這種解決方案只是消除了從文件中的所有非法字符。

public static void main(String args[]) { 
    String encoding = "UTF-8"; // try "UTF-8" first and also change to other encodings to see the results 

    StringBuilder sb = new StringBuilder(); 
    try(Reader reader = new InputStreamReader(new FileInputStream("node.txt"), encoding)) { 
     int c = -1; 
     while ((c = reader.read()) != -1) { 
      if (eligible(c)) { 
       sb.append((char)c); 
      } 
     } 
    } catch (Exception e){ 
     e.printStackTrace(); 
    } 

    int index = sb.indexOf("settings.bluetooth"); 
    if (index >= 0) { 
     System.out.println(sb.substring(index)); 
    } 
} 

public static boolean eligible(int c) { 
    return (c >= 'a' && c <= 'z' || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.'); 
} 
+0

我只能提供一個文件樣本的鏈接,因爲整個文件包含大約38000行,並且還包含一些敏感數據。但是可以在這裏找到一個文件樣本的鏈接:http://www.mediafire.com/download/523h456eub8j7c0/node.txt – JPM

+0

@JPM'掃描儀'不適用於這個文件。我仍然在尋找原因。但是,使用'FileReader'可以正常工作,就像我更新後的答案一樣。 –

+0

您好,感謝您的幫助。我剛剛使用了你的代碼,並且必須將「+18」改爲「+65」,以確保它顯示整個行。但是它顯示了一些編碼數據,如下所示:http://i.imgur.com/92R94rA.png。無論如何擺脫廣場?提前致謝。 – JPM