Java 如何使用InputStreamReader在Android Studio中逐行读取文件

Java 如何使用InputStreamReader在Android Studio中逐行读取文件,java,android,line,fileinputstream,Java,Android,Line,Fileinputstream,我现在能够读入一个文件,但我不知道如何逐行读取字符串来运行我创建的解析器。任何建议都会有帮助 public void ReadBtn() { char[] inputBuffer = new char[READ_BLOCK_SIZE]; int charRead; String s = ""; int READ_BLOCK_SIZE = 100; //reading text from file t

我现在能够读入一个文件,但我不知道如何逐行读取字符串来运行我创建的解析器。任何建议都会有帮助

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

-试试这个代码。将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();
            }

        }
    }

}

您可以读取
阵列列表中的所有行:

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();
    }
}
public void ReadBtn(){
int READ_BLOCK_SIZE=100;
ArrayList linesList=新的ArrayList();
//从文件中读取文本
试一试{
FileInputStream fileIn=openFileInput(“mytextfile.txt”);
InputStreamReader InputRead=新的InputStreamReader(fileIn);
BufferedReader br=新的BufferedReader(InputRead);
String line=br.readLine();
while(行!=null){
行列表。添加(行);
line=br.readLine();
}
InputRead.close();
//这里linesList包含一个字符串数组
用于(字符串s:linesList){
//为每一行做点什么
}
}捕获(例外e){
e、 printStackTrace();
}
}

谢谢!我选择使用内部存储,但我想我理解你的意思。你能解释一下,为什么吗?因为我在一个应用程序中有一个类似的函数,它可以工作。问题是如何逐行读取或如何打开外部文件?我正在尝试逐行读取。我提供的代码使我能够读取整个文件。因此,由于我只能读取整个文件,而不能逐行读取,因此当我将s传递给解析器时,会得到空结果。