Java 将.txt文件数据放入数组中

Java 将.txt文件数据放入数组中,java,java.util.scanner,Java,Java.util.scanner,我是java新手,我想知道如何读取.txt文件,然后将每一行放入数组单元格。 .txt文件的格式必须如下所示: car //goes in array[0] boat //goes in array[1] ship //goes in array[2] airplane //goes in array[3] //...and so on.. 我已经尝试过创建一个以这种方式实现的ReadFile类: import java.io.*; import java.util.*; public cla

我是java新手,我想知道如何读取.txt文件,然后将每一行放入数组单元格。 .txt文件的格式必须如下所示:

car //goes in array[0]
boat //goes in array[1]
ship //goes in array[2]
airplane //goes in array[3]
//...and so on..
我已经尝试过创建一个以这种方式实现的ReadFile类:

import java.io.*;
import java.util.*;
public class ReadFile {
    private Scanner x;

public void open(){
    try{
        x = new Scanner(new File("time_table_data.txt"));
    }catch(Exception e){
        System.out.println("Could Not Create The File");
    }
}

public String read(){
    String s = "";
    while(x.hasNext()){
        String a = x.next();
        s = a.format("%s\n",a);
    }
    return s;
}


public void close(){
    x.close();
}


}

问题是你不知道会有多少词。要解决这个问题,可以使用ArrayList

List<String> entries = new ArrayList<String>();
while (scanner.hasNext())
{
    entries.add(scanner.nextLine());
}
System.out.println(entries);

问题是你不知道会有多少词。要解决这个问题,可以使用ArrayList

List<String> entries = new ArrayList<String>();
while (scanner.hasNext())
{
    entries.add(scanner.nextLine());
}
System.out.println(entries);
只要做:

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

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
       lines.add(line); // Add line to list
    }
} // Try-with-resources closes reader
List line=new ArrayList();
try(BufferedReader br=new BufferedReader(new FileReader(file))){
弦线;
而((line=br.readLine())!=null){
行。添加(行);//将行添加到列表
}
}//尝试使用资源关闭读卡器
你不需要扫描器或任何其他花哨的东西,当你只是寻找整条线

如果您真的需要一个数组而不是最后的列表,您可以从最终列表中读取数组。

只需执行以下操作:

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

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
       lines.add(line); // Add line to list
    }
} // Try-with-resources closes reader
List line=new ArrayList();
try(BufferedReader br=new BufferedReader(new FileReader(file))){
弦线;
而((line=br.readLine())!=null){
行。添加(行);//将行添加到列表
}
}//尝试使用资源关闭读卡器
你不需要扫描器或任何其他花哨的东西,当你只是寻找整条线


如果你真的需要一个数组而不是最后的列表,你可以从最后的列表中读出数组。

制作一个方法,从文件中读取所有数据并存储在列表中,如下所示

public ArrayList<String> fileRead(String fileName){
        File           f;
        String         s;
        FileReader     fr = null;
        BufferedReader br = null;
        ArrayList<String>   sl = new ArrayList<String>();
        try {
            f  = new File(fileName); 
            fr = new FileReader(f);
            br = new BufferedReader(fr);
            while((s=br.readLine())!=null){
                sl.add(s);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
                try {
                    if(br!=null)
                        br.close();
                    if(fr!=null)
                        fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return sl;
}
public ArrayList fileRead(字符串文件名){
文件f;
字符串s;
FileReader fr=null;
BufferedReader br=null;
ArrayList sl=新的ArrayList();
试一试{
f=新文件(文件名);
fr=新文件读取器(f);
br=新的缓冲读取器(fr);
而((s=br.readLine())!=null){
sl.add(s);
}
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
如果(br!=null)
br.close();
如果(fr!=null)
fr.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回sl;
}

创建一个方法,从文件中读取所有数据并存储在列表中,如下所示

public ArrayList<String> fileRead(String fileName){
        File           f;
        String         s;
        FileReader     fr = null;
        BufferedReader br = null;
        ArrayList<String>   sl = new ArrayList<String>();
        try {
            f  = new File(fileName); 
            fr = new FileReader(f);
            br = new BufferedReader(fr);
            while((s=br.readLine())!=null){
                sl.add(s);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
                try {
                    if(br!=null)
                        br.close();
                    if(fr!=null)
                        fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return sl;
}
public ArrayList fileRead(字符串文件名){
文件f;
字符串s;
FileReader fr=null;
BufferedReader br=null;
ArrayList sl=新的ArrayList();
试一试{
f=新文件(文件名);
fr=新文件读取器(f);
br=新的缓冲读取器(fr);
而((s=br.readLine())!=null){
sl.add(s);
}
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
如果(br!=null)
br.close();
如果(fr!=null)
fr.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回sl;
}
如果您愿意使用,那么您可以非常轻松地做到这一点:

import org.apache.commons.io.FileUtils;

String[] linesArr = new String[0];
List<String> lines = FileUtils.readLines(new File("FILE_NAME.txt"));
if (lines != null) {
     linesArr = lines.toArray(linesArr);
}
import org.apache.commons.io.FileUtils;
字符串[]linesArr=新字符串[0];
列表行=FileUtils.readLines(新文件(“File_NAME.txt”);
如果(行!=null){
linesArr=lines.toArray(linesArr);
}
如果您愿意使用,那么您可以非常轻松地做到这一点:

import org.apache.commons.io.FileUtils;

String[] linesArr = new String[0];
List<String> lines = FileUtils.readLines(new File("FILE_NAME.txt"));
if (lines != null) {
     linesArr = lines.toArray(linesArr);
}
import org.apache.commons.io.FileUtils;
字符串[]linesArr=新字符串[0];
列表行=FileUtils.readLines(新文件(“File_NAME.txt”);
如果(行!=null){
linesArr=lines.toArray(linesArr);
}

您面临的确切问题是什么?你忘了问问题了。问题通常以问号结束。那么你的问题是什么?你使用这个类并调用它的方法的代码在哪里?@peeskillet我认为OP要求的是那个不在这里的类。而且在类中实际有一个数组也会有帮助,因为这是必需的。在你的阅读方法中也要使用这个数组。你面临的确切问题是什么?你忘了问问题了。问题通常以问号结束。那么你的问题是什么?你使用这个类并调用它的方法的代码在哪里?@peeskillet我认为OP要求的是那个不在这里的类。而且在类中实际有一个数组也会有帮助,因为这是必需的。还可以在read方法中使用该数组,如果希望将其转换回数组,可以使用entries.toArray();如果希望将其转换回数组,可以使用entries.toArray();我是使用图书馆的坚定支持者,尽管我不确定它是否适合初学者。我认为最好先了解你是如何做某件事的,从那时起,使用一个可能比你做得更好的库。@Giannis我同意,但我认为它应该在这里,因为在你学习了基础知识之后,它会让生活变得更简单。如果有人真的想知道基本知识,我建议写它时不要使用java.util.Scanner类,即使用FileInputStream读取、读取字节并划分为行,这将是最好的学习练习。我是使用库的坚定支持者,尽管我不确定这是否适合初学者。我认为最好先了解你是如何做某件事的,从那时起,使用一个可能比你做得更好的库。@Giannis我同意,但我认为它应该在这里,因为在你学习了基础知识之后,它会让生活变得更简单。如果有人真的想知道基础知识,我建议编写它时不要使用java.util.Scanner类,即。