Java 读取txt文件内容并存储在数组中

Java 读取txt文件内容并存储在数组中,java,android,eclipse,Java,Android,Eclipse,我一直在尝试读取一个txt文件。txt文件包含行,例如 First Line Second Line Third Line . . . 现在我使用以下代码 InputStream is = null; try { is = getResources().getAssets().open("myFile.txt"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();

我一直在尝试读取一个txt文件。txt文件包含行,例如

First Line
Second Line
Third Line
.
.
.
现在我使用以下代码

InputStream is = null;
try {
    is = getResources().getAssets().open("myFile.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
    e.printStackTrace();
}

ArrayList<String> arrayOfLines = new ArrayList<String>();

Reader reader;
//char[] buffer = new char[2048];
try {
    Reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    int n;
    while   ((n = reader.read()) != -1) {

    }
}catch (Exception e) {
    e.printStackTrace();
}
InputStream=null;
试一试{
is=getResources().getAssets().open(“myFile.txt”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
ArrayList arrayOfLines=新的ArrayList();
读者;
//char[]buffer=新字符[2048];
试一试{
Reader=新的BufferedReader(新的InputStreamReader(即“UTF-8”);
int n;
而((n=reader.read())!=-1){
}
}捕获(例外e){
e、 printStackTrace();
}
我的问题是,如何在arrayList中存储每一行。Ofc我们必须检查
“/n”
,但如何检查。

这:

int n;
while   ((n = reader.read()) != -1) {

}
应该看起来更像这样:

String line = reader.readLine();
while   (line!=null) {
    arrayOfLines.add(line);
    line = reader.readLine();
}
由于您使用的是
BufferedReader
,因此应该调用
readLine()
,而不是读取字符缓冲区。
读取器
声明也需要是
缓冲读取器

这样:

int n;
while   ((n = reader.read()) != -1) {

}
应该看起来更像这样:

String line = reader.readLine();
while   (line!=null) {
    arrayOfLines.add(line);
    line = reader.readLine();
}

由于您使用的是
BufferedReader
,因此应该调用
readLine()
,而不是读取字符缓冲区。
Reader
声明也需要是
BufferedReader

您也可以使用该类


您不必担心
\n
,因为您也可以使用该类

您不必担心
\n
,因为此代码应该可以工作

 ArrayList<String> arrayOfLines = new ArrayList<String>();
 FileInputStream fstream = new FileInputStream("myfile.txt");
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  while ((strLine = br.readLine()) != null)   {
  arrayOfLines.add(strLine);
  }
ArrayList arrayOfLines=new ArrayList();
FileInputStream fstream=新的FileInputStream(“myfile.txt”);
DataInputStream in=新的DataInputStream(fstream);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
弦斯特林;
而((strLine=br.readLine())!=null){
arrayOfLines.add(斯特林);
}
此代码应该有效

 ArrayList<String> arrayOfLines = new ArrayList<String>();
 FileInputStream fstream = new FileInputStream("myfile.txt");
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  while ((strLine = br.readLine()) != null)   {
  arrayOfLines.add(strLine);
  }
ArrayList arrayOfLines=new ArrayList();
FileInputStream fstream=新的FileInputStream(“myfile.txt”);
DataInputStream in=新的DataInputStream(fstream);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
弦斯特林;
而((strLine=br.readLine())!=null){
arrayOfLines.add(斯特林);
}

@umar您需要将
读卡器
更改为
缓冲读卡器
@umar您需要将
读卡器
更改为
缓冲读卡器
它只存储单词而不是整行。它只存储单词而不是整行。