读入文件并存储到动态分配的字符串数组中

时间:2022-06-25 15:43:57

Beginner Java programmer here. I have searched for a while on the interwebs without much success.

初学Java程序员在这里。我在互联网上搜索了一段时间没有取得多大成功。

I need to read in a text file and store each line into a string array. However I do not know how big the text file will be thus I was trying to figure out a easy way to dynamically allocate the size of the string array. I didn't know if there was a handy tool already in the Java library I could use. I was thinking maybe counting the total # of lines in the file first, then allocating the string array, but I also didn't know the best way to do that.

我需要读入一个文本文件并将每一行存储到一个字符串数组中。但是我不知道文本文件有多大,因此我试图找出一种动态分配字符串数组大小的简单方法。我不知道我可以使用的Java库中是否有一个方便的工具。我想可能首先计算文件中的总行数,然后分配字符串数组,但我也不知道最好的方法。

Thank you for any input!

谢谢你的任何输入!

3 个解决方案

#1


1  

You could use an ArrayList and not worry about sizing:

您可以使用ArrayList而不用担心调整大小:

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

try (BufferedReader br = new BufferedReader(new FileReader(file)))
{
    String line;
    while ((line = br.readLine()) != null)
        fileLines.add(line);
}

fileLines could get pretty big, but if you're okay with that then this is an easy way to get started.

fileLines可能会变得很大,但是如果你对它没问题,那么这是一个简单的入门方法。

#2


1  

Define an array-list which does not require a fixed length, as you can add or remove as many elements as you wish:

定义一个不需要固定长度的数组列表,因为您可以根据需要添加或删除任意数量的元素:

    List<String> fileList = new ArrayList<String>();
    //Declare a file at a set location:
    File file = new File("C:\\Users\\YourPC\\Desktop\\test.txt");
    //Create a buffered reader that reads a file at the location specified:
    try (BufferedReader br = new BufferedReader(new FileReader(file)))
    {
        String line;
        //While there is something left to read, read it:
        while ((line = br.readLine()) != null)
            //Add the line to the array-list:
            fileList.add(line);
    }catch(Exception e){
        //If something goes wrong:
        e.printStackTrace();
    }

    //Determine the length of the array-list:
    int listTotal = fileList.size();
    //Define an array of the length of the array-list:
    String[] fileSpan = new String[listTotal];

    //Set each element index as its counterpart from the array-list to the array:
    for(int i=0; i<listTotal; i++){

        fileSpan[i] = fileList.get(i);
    }

#3


0  

If you only want a working program (and not practice in coding and debugging) in Java8+:

如果您只想在Java8 +中使用工作程序(而不是在编码和调试中练习):

 String[] ary = java.nio.file.Files.readAllLines(Paths.get(filename)).toArray(new String[0]);
 // substitute the (Path,Charset) overload if your data isn't compatible with UTF8
 // if a List<String> is sufficient for your needs omit the .toArray part

#1


1  

You could use an ArrayList and not worry about sizing:

您可以使用ArrayList而不用担心调整大小:

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

try (BufferedReader br = new BufferedReader(new FileReader(file)))
{
    String line;
    while ((line = br.readLine()) != null)
        fileLines.add(line);
}

fileLines could get pretty big, but if you're okay with that then this is an easy way to get started.

fileLines可能会变得很大,但是如果你对它没问题,那么这是一个简单的入门方法。

#2


1  

Define an array-list which does not require a fixed length, as you can add or remove as many elements as you wish:

定义一个不需要固定长度的数组列表,因为您可以根据需要添加或删除任意数量的元素:

    List<String> fileList = new ArrayList<String>();
    //Declare a file at a set location:
    File file = new File("C:\\Users\\YourPC\\Desktop\\test.txt");
    //Create a buffered reader that reads a file at the location specified:
    try (BufferedReader br = new BufferedReader(new FileReader(file)))
    {
        String line;
        //While there is something left to read, read it:
        while ((line = br.readLine()) != null)
            //Add the line to the array-list:
            fileList.add(line);
    }catch(Exception e){
        //If something goes wrong:
        e.printStackTrace();
    }

    //Determine the length of the array-list:
    int listTotal = fileList.size();
    //Define an array of the length of the array-list:
    String[] fileSpan = new String[listTotal];

    //Set each element index as its counterpart from the array-list to the array:
    for(int i=0; i<listTotal; i++){

        fileSpan[i] = fileList.get(i);
    }

#3


0  

If you only want a working program (and not practice in coding and debugging) in Java8+:

如果您只想在Java8 +中使用工作程序(而不是在编码和调试中练习):

 String[] ary = java.nio.file.Files.readAllLines(Paths.get(filename)).toArray(new String[0]);
 // substitute the (Path,Charset) overload if your data isn't compatible with UTF8
 // if a List<String> is sufficient for your needs omit the .toArray part