2014-12-03 72 views
0

我已經創建了要搜索的特定標籤的文件的自定義功能,並返回它的價值,就像這樣:自定義功能,搜索文件的標籤總是返回null

函數調用:

getSingleLineValue("tagname"); 
在文件

實測值行:

<tagname>=tagvalue 

返回的字符串:

tagvalue 

這裏是該函數的代碼:

public String getSingleLineValue(String tag) { 
    // The value 
    String value; 
    // If the list of passed tags contains the wanted tag 
    if(passedTags.contains(tag)) { 
     // Close the readers 
     close(); 
     // RESET EVERYTHING 
     try { 
      // Re-create the FileReader 
      fileReader = new FileReader(file); 
      // Re-create the BufferedReader 
      bufferedReader = new BufferedReader(fileReader); 
      // Reset the passed tags array 
      passedTags.clear(); 
      // Recall the function 
      value = getSingleLineValue(tag); 
      // Return the value 
      return value; 
     } catch(IOException e) { 
      // Handle the exception 
      e.printStackTrace(); 
     } 
    } else { 
     try { 
      // The current line 
      String line; 
      // While the file has lines left 
      while ((line = bufferedReader.readLine()) != null) { 
       // If the current line contains a pair of tag marks (<and>) 
       if (line.contains("<") && line.contains(">")) { 
        // If the line contains the tag 
        if(line.contains(tag)) { 
         // Store the parts of the tag in an array (tag and value) 
         String[] tagParts = line.split("="); 
         // Get the value 
         value = tagParts[1]; 
         // Return the value 
         return value; 
        } else { 
         // Get the passed tag 
         String passedTag = line.substring(1, line.indexOf(">") - 1); 
         // Add the tag to the passed tags array 
         passedTags.add(passedTag); 
        } 
       } 
      } 
     } catch(IOException e) { 
      // Handle the exception 
      e.printStackTrace(); 
     } 
    } 
    // If the tag wasn't found, return null 
    return null; 
} 

對象調用file是一個簡單的File對象與路徑我想要閱讀該文件。它在同一個班上宣佈。

稱爲fileReaderbufferedReader的對象就是它聽起來的樣子。一個FileReaderBufferedReader,聲明如下(也在同一類):

private FileReader fileReader; 
private BufferedReader bufferedReader; 

而且在類的構造函數:

fileReader = new FileReader(file); 
bufferedReader = new BufferedReader(fileReader); 

的問題是,這個函數總是返回null,所以當我打電話吧,我是多麼希望它的工作反而會是這個樣子以上示範:

函數調用:

getSingleLineValue("tagname"); 
文件

找到行:

未知(?也許這就是問題所在)

返回的字符串:

null 

問題可能是一些與行,因爲當我打印出來,我在控制檯中沒有收到任何消息,但我真的不知道它有什麼問題,如果是問題。

所有幫助非常感謝!

回答

0

我發現了什麼問題。 這不是功能,它實際上工作正常,但它是文件的編碼

我創建了一個簡單的計算機版本,並添加了該函數,並打印了所有讀取的行,並發現文件在程序中的讀取方式與Sublime Text(計算機上的文本編輯器)中的讀取方式不同。它看起來像這樣的一行:

ABCDEFG 
在文本編輯器

,是這樣的讀取程序:

A B C D E F G 

因此,基本上,它添加的每個字符之間的空間。我認爲問題出在文件編碼上,所以這個功能本身就很有用。

編輯:我用一個FileInputStream,並設置它的編碼解決了文件的編碼問題「UTF-16LE」,這是對我的文件正確的編碼,所以上面的代碼,而不是看起來像這樣:

public String getSingleLineValue(String tag) { 
    // The value 
    String value; 
    // If the list of passed tags contains the wanted tag 
    if(passedTags.contains(tag)) { 
     // Close the readers 
     close(); 
     // RESET EVERYTHING 
     try { 
      // Re-create the FileInputStream 
      fileInput = new FileInputStream(file); // <----- Changed 
      // Re-create the InputStreamReader 
      inputReader = new InputStreamReader(fileInput, "UTF-16LE"); // <----- Changed 
      // Re-create the BufferedReader 
      bufferedReader = new BufferedReader(inputReader); // <----- Changed 
      // Reset the passed tags array 
      passedTags.clear(); 
      // Recall the function 
      value = getSingleLineValue(tag); 
      // Return the value 
      return value; 
     } catch(IOException e) { 
      // Handle the exception 
      e.printStackTrace(); 
     } 
    } else { 
     try { 
      // The current line 
      String line; 
      // While the file has lines left 
      while ((line = bufferedReader.readLine()) != null) { 
       // If the current line contains a pair of tag marks (<and>) 
       if (line.contains("<") && line.contains(">")) { 
        // If the line contains the tag 
        if(line.contains(tag)) { 
         // Store the parts of the tag in an array (tag and value) 
         String[] tagParts = line.split("="); 
         // Get the value 
         value = tagParts[1]; 
         // Return the value 
         return value; 
        } else { 
         // Get the passed tag 
         String passedTag = line.substring(1, line.indexOf(">") - 1); 
         // Add the tag to the passed tags array 
         passedTags.add(passedTag); 
        } 
       } 
      } 
     } catch(IOException e) { 
      // Handle the exception 
      e.printStackTrace(); 
     } 
    } 
    // If the tag wasn't found, return null 
    return null; 
}