阅读文本文件&将内容添加到ArrayList [英] Reading a text file & adding the content to an ArrayList

查看:90
本文介绍了阅读文本文件&将内容添加到ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习从文本文件中获取输入并阅读内容的所有不同方法。我正在努力使用try-catch块(带资源),并读取文件名。

I am learning about all of the different ways to take input from a text file and read through the content. I am struggling to use a try - catch block (with resources), and reading in the file name.

以下是我为此部分编写的内容: -

Below is what I have written for this part so far:-

public static void main(String[] args) 
{

    ArrayList<StationRecord> data = new ArrayList<>();
    String stationName = null;

    Scanner scan = new Scanner(System.in);
    System.out.println("What is the filename?");
    String input = scan.nextLine();
    File file = new File(input);

    try(BufferedReader in = new BufferedReader(new FileReader(file))){

      while(scan.hasNext()){

       stationName = scan.nextLine();
       int yearMonthDay = scan.nextInt();
       int max = scan.nextInt();
       int min = scan.nextInt();
       int avg = scan.nextInt();
       double dif = scan.nextDouble();

       StationRecord sr = new StationRecord(yearMonthDay, max, min, avg, dif);
       data.add(sr);
      }

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

    }
}

我正在尝试这样做不仅是一个文件,还有两个文件。无论如何,这里有一个输入样本: -

I am trying to do this for not only one file, but two of them. Regardless, here is a sample of input:-

控制台:文件名是什么?

Console: What is the filename?

TempData2018a.txt

输入后,我试图通过文本文件中的数据并将其添加到 StationRecord 类型的 ArrayList (我的其他类)。

After this is entered, I am trying to go through the data in the text file and add it to the ArrayList of type StationRecord (my other class).

任意建议和指导将不胜感激!关于如何使用2个文件执行此操作的任何输入都会很棒!

Any suggestions and guidance would be much appreciated! Also any input on how you might do this with 2 files would be awesome!.

编辑:.txt文件数据示例

.txt file data example

PITTSBURGH ALLEGHENY CO AIRPORT PA
20180101 11 2 7 -22.614762
20180102 12 5 9 -20.514762
20180103 23 2 13 -16.414762

我试图将整个第一行存储在名为stationName的变量中。然后我试图将下一个int,int,int,int double存储在 StationRecord 类型的 ArrayList 中。

I am trying to store that whole first line in a variable called stationName. Then Im trying to store the next int, int, int, int double in an ArrayList of StationRecord type.

推荐答案

使用 Java 7 & Java 8 API的功能,您可以解决以下问题:

Using Java 7 & Java 8 API's feature, you can solve your problem like below:

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LineOperation {

private static List<String> lines;

public static void main(String[] args) throws IOException {

    lines = Collections.emptyList();

    try {
          lines = Files.readAllLines(Paths.get("C:\\Users\\Abhinav\\Downloads\\TempData2018a.txt"), StandardCharsets.UTF_8);

          String stationName = lines.get(0);

          String[] arr = null;

          ArrayList<StationRecord> data = new ArrayList<>();

          for(int i=1;i<lines.size();i++) {

              arr = lines.get(i).split(" ");

              data.add(new StationRecord(Long.parseLong(arr[0]), Integer.parseInt(arr[1]), Integer.parseInt(arr[2]), Integer.parseInt(arr[3]), Double.parseDouble(arr[4])));
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
}
}

但我强烈建议你请参阅以下链接,以进一步澄清 Java I / O : -

But I strongly recommend you to refer below links for your further clarifications on Java I/O:-


  1. Java - 从文件中读取

将文件读入ArrayList

这篇关于阅读文本文件&amp;将内容添加到ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆