簡體   English   中英

在J2ME中從文件中讀取內容

[英]Reading Content from File in J2ME

我正在嘗試閱讀文件的內容,但似乎無法正常工作。 我在網上瀏覽並找到了不同的實現,如圖所示(read(),read2(),readLine()),但每次運行代碼時,它們都會給出NullPointer異常。 請問我該怎么做才能解決這個問題。

     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();
    }


    }

ISSUE: 使用不正確的方法引用文件

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

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

您正在嘗試從FileConnection中使用的類型的文件架構創建輸入流,因此它將無法工作。 因此,為了解決您的問題,您已使用以下代碼替換read(...)read2(...)readFile(...)的inputstream對象初始化

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

並在execute(...)傳遞文件連接輸入流,如下所示

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

如果您在模擬器/設備中測試應用程序,則可能還需要考慮這些問題

  1. 確保System.getProperty("fileconn.dir.photos")返回NON NULL
  2. 該文件存儲在系統中的適當位置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM