简体   繁体   English

如何从文件中读取行并将其存储到数组中

[英]How do I read lines from a file and store them into an array

So basically what I'm trying to do is read this file called Products.csv, and then I want to store each lines of it (445 lines) into an array of length 445. 因此,基本上我想做的是读取名为Products.csv的文件,然后将其每一行(445行)存储到长度为445的数组中。

For some reason, if I typed System.out.println (lines[2]); 由于某种原因,如果我键入System.out.println(lines [2]); for example, it does read out the line 2 of the file, however, if I used a loop to read out all the lines with this code System.out.println (lines[a]) while in a loop, it shows me all null null null.... 例如,它确实读出了文件的第2行,但是,如果我在循环中使用循环使用此代码System.out.println(lines [a])读出了所有行,则它将显示所有null null null ....

public class Lab2 
{
        String [] lines = new String [446];
        int x = 0;
    File inFile;

    public long ReadFile(String sfile) throws IOException 
    {
        inFile  = new File(sfile);
        BufferedReader reader = new BufferedReader(new FileReader(inFile));
        String sline = null;
        while ((sline=reader.readLine()) != null) 
        {
                lines[x]=sline;
                x++;
        }
        reader.close();  
        return inFile.length();
         }

        public void OutputLines (String [] s)
        {
            for (int a=0;a<s.length;a++)
            {
               System.out.println (s[a]); 
            }
        }

    public static void main(String[] args)
    {
        try
        {
            Lab2 read = new Lab2();
            read.ReadFile("Products.csv");

        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }

                Lab2 reader = new Lab2 ();
                reader.OutputLines(reader.lines);
        }
}

You're reading the lines into one Lab2 object, then creating a totally new different Lab2 object to display the results. 您正在将这些行读取到一个Lab2对象中,然后创建一个全新的不同Lab2对象以显示结果。 Don't do that since the 2nd Lab2 object has not been filled with data and thus its array is filled with nulls. 不要这样做,因为第二Lab2对象尚未填充数据,因此其数组填充了空值。 Instead use the same Lab2 object for both reading and displaying. 而是使用相同的 Lab2对象进行读取显示。

Change 更改

public static void main(String[] args)
{
    try
    {
        Lab2 read = new Lab2();
        read.ReadFile("Products.csv");

    }
    catch (IOException e)
    {
        System.out.println(e.getMessage());
    }

            Lab2 reader = new Lab2 ();
            reader.OutputLines(reader.lines);
    }

to

public static void main(String[] args) {
    try {
        Lab2 read = new Lab2();
        read.ReadFile("Products.csv");

        // *** display data from the same Lab2 object ***
        read.OutputLines(); // this shouldn't take a parameter
    } catch (IOException e) {
        e.printStacktrace();
    }

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

相关问题 如何从文本文件中读取单个字符并将它们存储到二维数组中? - How do I get individual characters from read in text file and store them into a 2D array? 如何使用扫描仪读取多行文件并将其存储到不同的对象中 - How do I read multiple file lines and store them into different Objects Using Scanner 如何从输入的文件中读取行,然后将最近读取的行存储在数组中? - How can I read lines from a inputted file and then store the most recently read lines in an array? 如何将从文件读取的双精度数存储到数组中? - How do I store doubles read from a file into an array? Java 8 - 如何从文件中读取行并将其有条件地存储到数组中 - Java 8 - How to read lines from file and store it to array conditionally 如何使用BufferedReader将txt文件中的行读取到数组中 - How do I use BufferedReader to read lines from a txt file into an array 如何从文件读取到数组 - How do I read from a File to an array 从文本文件中读取整数并将它们存储到 Java 中的数组中 - Read integers from a text file and store them into an array in Java 从外部文件读取行并将元素存储在数组中 - read lines from external file and store elements in array 您如何从输入中读取一系列数字并将其存储到数组中? - How do you read a seriers of numbers from input and store them into an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM