2011-12-12 36 views
3

我正在嘗試讀取文件的內容,但似乎不起作用。我瀏覽網頁,發現不同的實現(read(),read2(),readLine()),但每次運行代碼時,它們都會給出NullPointer異常。請問我能做些什麼來糾正這個問題。從J2ME中的文件中讀取內容

 private String folder; 
     static String filename; 
     //IMPLEMENTATION 1 
     private void readFile(String f) { 
      try { 
       InputStreamReader reader = new InputStreamReader(getClass().getResourceAsStream(f)); 
       String line = null; 
       while ((line = readLine(reader)) != null) { 
        System.out.println(line); 
       } 
       reader.close(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
/** 
     * Reads a single line using the specified reader. 
     * @throws java.io.IOException if an exception occurs when reading the 
     * line 
     */ 
     private String readLine(InputStreamReader reader) throws IOException { 
      // Test whether the end of file has been reached. If so, return null. 
      int readChar = reader.read(); 
      if (readChar == -1) { 
       return null; 
      } 
      StringBuffer string = new StringBuffer(""); 
      // Read until end of file or new line 
      while (readChar != -1 && readChar != '\n') { 
       if (readChar != '\r') { 
        string.append((char) readChar); 
       } 
       // Read the next character 
       readChar = reader.read(); 
      } 
      return string.toString(); 
     } 

     //IMPLEMENTATION 2 
     private String read(String file) throws IOException { 
      InputStream is = getClass().getResourceAsStream(file); 
      StringBuffer sb = new StringBuffer(); 
      int chars, i = 0; 
      while ((chars = is.read()) != -1) { 
       sb.append((char) chars); 
      } 
      return sb.toString(); 
     } 

     //IMPLEMENTATION 3 
     private String read2(String file) throws IOException { 
      String content = ""; 
      Reader in = new InputStreamReader(this.getClass().getResourceAsStream(file)); 
      StringBuffer temp = new StringBuffer(1024); 
      char[] buffer = new char[1024]; 
      int read; 
      while ((read = in.read(buffer, 0, buffer.length)) != -1) { 
       temp.append(buffer, 0, read); 
      } 
      content = temp.toString(); 
        return content; 
     } 

     public void execute() throws IOException { 
      folder = System.getProperty("fileconn.dir.photos") + "mcast/"; 
      String path = folder + filename + ".txt"; 
      FileConnection c = (FileConnection) Connector.open(path, Connector.READ_WRITE); 

      try { 
       // Checking if the directoy exists or not. If it doesn't exist we create it. 
       if (c.exists()) { 
      readFile(path); 
        //read(path); 
        // read2(path); 
        System.out.println(read(path)); 
       } else { 
        System.out.println(filename + ".txt does not exist. Please specify a correct file name"); 
       } 
      } finally { 
       c.close(); 
      } 
     } 

private String readLine(InputStreamReader reader) throws IOException { 
     // Test whether the end of file has been reached. If so, return null. 
     int readChar = reader.read(); 
     if (readChar == -1) { 
      return null; 
     } 
     StringBuffer string = new StringBuffer(""); 
     // Read until end of file or new line 
     while (readChar != -1 && readChar != '\n') { 
      // Append the read character to the string. Some operating systems 
      // such as Microsoft Windows prepend newline character ('\n') with 
      // carriage return ('\r'). This is part of the newline character 
      // and therefore an exception that should not be appended to the 
      // string. 
      if (readChar != '\r') { 
       string.append((char) readChar); 
      } 
      // Read the next character 
      readChar = reader.read(); 
     } 
     return string.toString(); 
    } 


    } 
+0

你在哪裏得到'NullPointerException'?你有調試你的應用程序嗎? – bharath

+0

是的。我在InputStreamReader中取得了它read = new InputStreamReader(getClass()。getResourceAsStream(f)); – nnanna

回答

2

ISSUE:參考文件中使用不正確的方法

getResourceAsStream(...)是用於加載資源從類路徑從您的二進制包(的.jar)或類路徑目錄。

所以它本質上意味着,以從二進制包使用的getClass讀取一個文件()。的getResourceAsStream()讀取設備的物理內存使用API​​的FileConnection的文件。

您正試圖從FileConnection中使用的文件模式創建輸入流,因此它不起作用。因此,要解決你的問題,你有下面的代碼替換InputStream對象初始化在read(...)read2(...)readFile(...)

InputStreamReader reader = new InputStreamReader(in);//這裏in是InputStream類型的方法的輸入參數

execute(...)通過文件連接的InputStream如下

readFile(c.openInputStream());//這裏c是類型的的FileConnection對象

您也可能要考慮這些,如果你是在模擬器/設備測試您的應用程序

  1. 確保System.getProperty("fileconn.dir.photos")回報 非零
  2. 該文件存儲在適當的位置在系統中
+0

@nnanna,你能解決這個問題嗎? – Vimal

+0

還沒有。我理解你給出的理由,但我不明白如何實現你的解釋。 – nnanna

+0

如果您可以進行所需的更改,我將不勝感激。我將編輯上面的原始代碼,以包含我省略的readLine方法。 – nnanna