【问题标题】:How to read line by line from a file in Android Studio using InputStreamReader如何使用 InputStreamReader 从 Android Studio 中的文件中逐行读取
【发布时间】:2019-04-09 04:33:20
【问题描述】:

我现在可以读取文件,但我对如何逐行读取字符串以运行我创建的解析器感到困惑。任何建议都会有所帮助。

public void ReadBtn() {
        char[] inputBuffer = new char[READ_BLOCK_SIZE];
        int charRead;
        String s = "";
        int READ_BLOCK_SIZE = 100;

        //reading text from file
        try {
            FileInputStream fileIn = openFileInput("mytextfile.txt");
            InputStreamReader InputRead = new InputStreamReader(fileIn);
            BufferedReader BR = new BufferedReader(InputRead);

            while((charRead = InputRead.read(inputBuffer)) > 0) {
                // char to string conversion

            String readstring = String.copyValueOf(inputBuffer, 0, charRead);
                s += readstring;
                getContactInfo(s);


            }

            InputRead.close();


        } catch(Exception e) {
            e.printStackTrace();
        }
    }

【问题讨论】:

    标签: java android line fileinputstream


    【解决方案1】:

    -试试这个代码。将 sdCard 路径替换为 mytextfile.txt 所在的文件路径。

    String sdCard = Environment.getExternalStorageDirectory().getAbsolutePath();
    
        String fileName = "mytextfile.txt";
        String path = sdCard + "/" + MarketPath + "/";
    
        File directory = new File(path);
        if (directory.exists()) {
            File file = new File(path + fileName);
            if (file.exists()) {
                String myData = ""; // this variable will store your file text
                try {
                    FileInputStream fis = new FileInputStream(file);
                    DataInputStream in = new DataInputStream(fis);
                    BufferedReader br =new BufferedReader(new InputStreamReader(in));
                    String strLine;
                    while ((strLine = br.readLine()) != null) {
                        myData = myData + strLine;
                    }
    
    
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
        }
    
    }
    

    【讨论】:

    • 谢谢!我选择使用内部存储,但我想我理解你的意思。
    【解决方案2】:

    您可以阅读ArrayList 中的所有行:

    public void ReadBtn() {
        int READ_BLOCK_SIZE = 100;
        ArrayList<String> linesList = new ArrayList<>();
    
        // reading text from file
        try {
            FileInputStream fileIn=openFileInput("mytextfile.txt");
            InputStreamReader InputRead= new InputStreamReader(fileIn);
            BufferedReader br = new BufferedReader(InputRead);
    
            String line = br.readLine();
            while (line != null) {
                linesList.add(line);
                line = br.readLine();
            }
    
            InputRead.close();
    
            // here linesList contains an array of strings
    
            for (String s: linesList) {
                // do something for each line
            }
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

    • 你能解释一下,为什么?因为我在应用程序中有类似的功能并且它可以工作。问题是如何逐行读取或如何打开外部文件?
    • 我正在尝试逐行阅读。我提供的代码可以读取整个文件。因此,由于我只能读取整个文件而不是逐行读取,因此当我通过解析器传递 s 时,我会得到空结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 2014-12-01
    • 2019-10-17
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多