简体   繁体   English

使用arraylist在Java中存储文件

[英]Storing file in java using arraylist

I am reading a file and want to save it in an array list to be able to call this method in another class for later use. 我正在读取文件,并希望将其保存在数组列表中,以便能够在另一个类中调用此方法以供以后使用。 Here's my code: 这是我的代码:

    package com.qmul.rfid.reader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class ReadFile {

        public static void main(String[] args) {

            ArrayList<ReadFile> fileList = new ArrayList<ReadFile> ();

            try (BufferedReader br = new BufferedReader(new FileReader("C:\\tagNo.txt")))
            {

                String CurrentLine;

                while ((CurrentLine = br.readLine()) != null) {
                    System.out.println(CurrentLine);


                    fileList.add(CurrentLine);

                }

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

        }
    }

I get an error at fileList.add(CurrentLine);. 我在fileList.add(CurrentLine);处收到错误。 I know this is because CurrentLine does not match with ReadFile. 我知道这是因为CurrentLine与ReadFile不匹配。

How can I make this work? 我该如何进行这项工作?

Thank you. 谢谢。

The parameters in the < ... > brackets specify the type of the elements stored in the list. < ... >括号中的参数指定列表中存储的元素的类型 In this case, you want to store String objects. 在这种情况下,您要存储String对象。 So it should be 所以应该

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

You could replace 您可以更换

ArrayList <ReadFile> fileList = new ArrayList<ReadFile> (); ArrayList <ReadFile> fileList = new ArrayList <ReadFile>();

by 通过

List<String> fileList = new ArrayList<>(); List <String> fileList = new ArrayList <>();

in JDK 7 style 以JDK 7样式

尝试使用ArrayList<String> fileList而不是ArrayList<ReadFile>

You declared an arraylist that stores ReadFile but you want to add a String to the list which causes this exception. 您声明了一个存储ReadFile的arraylist,但是要向该列表添加一个String ,这会导致此异常。 Create another List from type List<String> and store the file content there. List<String>类型创建另一个List并将文件内容存储在那里。

CurrentLine类型为StringfileList应该为:

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

Your arrayList is of type ReadFile. 您的arrayList类型为ReadFile。 So it expects you to add elements of type ReadFile. 因此,它希望您添加ReadFile类型的元素。 What you need is change it to String. 您需要将其更改为String。

 ArrayList<ReadFile> fileList = new ArrayList<ReadFile> ();

to

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

Your ArrayList can hold objects of type ReadFile only, since you declared your ArrayList using <ReadLine> 由于您使用<ReadLine>声明了ArrayList ,因此ArrayList只能保存ReadFile类型的对象。

 Change your ReadFile class to something like 

Class ReadFile {

  String line;
  ReadFile(String currentLine){
     this.line = currentLine;
  }
}

Then you can do 那你可以做

fileList.add(new ReadFile(CurrentLine));

You have to provide String instead of readfile. 您必须提供String而不是readfile。 because here your list is expecting readfile. 因为在这里您的列表需要读取文件。

ArrayList list = new ArrayList(); ArrayList list = new ArrayList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM