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

Java 读入文件并存储到动态分配的字符串数组中,java,arrays,string,file,io,Java,Arrays,String,File,Io,这里是初学者Java程序员。我已经在互联网上搜索了一段时间,但没有多大成功 我需要读入一个文本文件,并将每一行存储到一个字符串数组中。然而,我不知道文本文件有多大,因此我试图找到一种简单的方法来动态分配字符串数组的大小。我不知道Java库中是否已经有一个方便的工具可以使用。我在想也许可以先计算文件中的行总数,然后分配字符串数组,但我也不知道最好的方法 谢谢你的任何意见 您可以使用阵列列表,而不必担心大小: List<String> fileLines = new ArrayList&

这里是初学者Java程序员。我已经在互联网上搜索了一段时间,但没有多大成功

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


谢谢你的任何意见

您可以使用
阵列列表
,而不必担心大小:

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

try (BufferedReader br = new BufferedReader(new FileReader(file)))
{
    String line;
    while ((line = br.readLine()) != null)
        fileLines.add(line);
}
List fileLines=new ArrayList();
try(BufferedReader br=new BufferedReader(new FileReader(file)))
{
弦线;
而((line=br.readLine())!=null)
文件行。添加(行);
}

文件行
可能会变得相当大,但如果您对此没有意见,那么这是一种简单的开始方式。

您可以使用
阵列列表
而不必担心大小:

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

try (BufferedReader br = new BufferedReader(new FileReader(file)))
{
    String line;
    while ((line = br.readLine()) != null)
        fileLines.add(line);
}
List fileLines=new ArrayList();
try(BufferedReader br=new BufferedReader(new FileReader(file)))
{
弦线;
而((line=br.readLine())!=null)
文件行。添加(行);
}

fileLines
可能会变得很大,但如果您不介意的话,这是一个简单的开始方法。

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

    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);
    }
List fileList=new ArrayList();
//在设置的位置声明文件:
File File=新文件(“C:\\Users\\YourPC\\Desktop\\test.txt”);
//创建在指定位置读取文件的缓冲读取器:
try(BufferedReader br=new BufferedReader(new FileReader(file)))
{
弦线;
//当有东西要读时,读一下:
而((line=br.readLine())!=null)
//将该行添加到数组列表:
文件列表。添加(行);
}捕获(例外e){
//如果出现问题:
e、 printStackTrace();
}
//确定数组列表的长度:
int listTotal=fileList.size();
//定义数组列表长度的数组:
String[]fileSpan=新字符串[listTotal];
//将每个元素索引设置为从数组列表到数组的对应项:

对于(int i=0;i定义一个不需要固定长度的数组列表,因为您可以添加或删除任意数量的元素:

    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);
    }
List fileList=new ArrayList();
//在设置的位置声明文件:
File File=新文件(“C:\\Users\\YourPC\\Desktop\\test.txt”);
//创建在指定位置读取文件的缓冲读取器:
try(BufferedReader br=new BufferedReader(new FileReader(file)))
{
弦线;
//当有东西要读时,读一下:
而((line=br.readLine())!=null)
//将该行添加到数组列表:
文件列表。添加(行);
}捕获(例外e){
//如果出现问题:
e、 printStackTrace();
}
//确定数组列表的长度:
int listTotal=fileList.size();
//定义数组列表长度的数组:
String[]fileSpan=新字符串[listTotal];
//将每个元素索引设置为从数组列表到数组的对应项:

对于(int i=0;i如果您只想要一个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
String[]ari=java.nio.file.Files.readAllLines(path.get(filename)).toArray(新字符串[0]);
//如果数据与UTF8不兼容,请替换(路径、字符集)重载
//如果列表足以满足您的需要,请省略.toArray部分

如果您只想在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
String[]ari=java.nio.file.Files.readAllLines(path.get(filename)).toArray(新字符串[0]);
//如果数据与UTF8不兼容,请替换(路径、字符集)重载
//如果列表足以满足您的需要,请省略.toArray部分

使用数组列表。要读取整个文件,请使用流。使用缓冲读取器。文件的读取应在while循环中完成,使用ReadLine,并循环,直到下一个ReadLine不为null。然后,您可以确定数组列表的大小,并定义该长度的数组,然后仅传输数组的内容使用数组列表。要读取整个文件,请使用流。使用缓冲读取器。文件的读取应在while循环中完成,使用ReadLine,并循环,直到下一个ReadLine不为null。然后,您可以确定数组列表的大小,并定义该长度的数组,然后仅传输将数组列表放入数组中。还要确保文件大小不超过为jvm进程分配的堆内存。非常好,感谢您的帮助。这比我自己编写的代码效率要高得多!还要确保文件大小不超过为jvm进程分配的堆内存。非常好,感谢您的帮助p、 这比我自己写的代码要高效得多!太好了,谢谢你的帮助。这比我自己写的代码要高效得多!而且,这个代码snipet真的帮助了我入门。谢谢!太好了,谢谢你的帮助。这比我自己写的代码要高效得多!还有,这个代码snipet真的帮助我开始学习。谢谢!