读取文本文件并将内容添加到 ArrayList
Reading a text file & adding the content to an ArrayList
我正在学习从文本文件中获取输入并通读内容的所有不同方法。我正在努力使用 try - catch 块(带资源),并读取文件名。
以下是我到目前为止为这部分所写的内容:-
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();
}
}
我不仅尝试对一个文件执行此操作,而且尝试对其中两个文件执行此操作。无论如何,这是一个输入示例:-
控制台:文件名是什么?
TempData2018a.txt
输入后,我试图遍历文本文件中的数据并将其添加到 StationRecord[=34= 类型的 ArrayList ](我的另一个class)。
如有任何建议和指导,我们将不胜感激!此外,关于如何使用 2 个文件执行此操作的任何输入都很棒!
编辑:.txt 文件数据示例
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 中。
使用Java 7 & Java 8 API' s 功能,你可以像下面这样解决你的问题:
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:-
因为这是 2018 年,请停止使用 new File(..)
并开始使用 nio API。当你使用 Java 8 时,你可以很容易地实现你想要在这里实现的目标!因此,您可以像这样创建 StationRecord
:
Path filePath = Paths.get(pathToFile);
String stationName = Files.lines(filePath)
.findFirst()
.get();
List<StationRecord> stationRecords =
Files.lines(filePath)
.skip(1) //Skip first line since it has station name
.map(line -> line.split("\s")) // split at a space starting from 2nd line
.map(
stationData -> new StationRecord(Integer.valueOf(stationData[0]),
Integer.valueOf(stationData[1]), Integer.valueOf(stationData[2]),
Integer.valueOf(stationData[3]), Double.valueOf(stationData[4]))) // Create StationRecord object using the split fields
.collect(Collectors.toList()); // Collect result to an ArrayList
我正在学习从文本文件中获取输入并通读内容的所有不同方法。我正在努力使用 try - catch 块(带资源),并读取文件名。
以下是我到目前为止为这部分所写的内容:-
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();
}
}
我不仅尝试对一个文件执行此操作,而且尝试对其中两个文件执行此操作。无论如何,这是一个输入示例:-
控制台:文件名是什么?
TempData2018a.txt
输入后,我试图遍历文本文件中的数据并将其添加到 StationRecord[=34= 类型的 ArrayList ](我的另一个class)。
如有任何建议和指导,我们将不胜感激!此外,关于如何使用 2 个文件执行此操作的任何输入都很棒!
编辑:.txt 文件数据示例
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 中。
使用Java 7 & Java 8 API' s 功能,你可以像下面这样解决你的问题:
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:-
因为这是 2018 年,请停止使用 new File(..)
并开始使用 nio API。当你使用 Java 8 时,你可以很容易地实现你想要在这里实现的目标!因此,您可以像这样创建 StationRecord
:
Path filePath = Paths.get(pathToFile);
String stationName = Files.lines(filePath)
.findFirst()
.get();
List<StationRecord> stationRecords =
Files.lines(filePath)
.skip(1) //Skip first line since it has station name
.map(line -> line.split("\s")) // split at a space starting from 2nd line
.map(
stationData -> new StationRecord(Integer.valueOf(stationData[0]),
Integer.valueOf(stationData[1]), Integer.valueOf(stationData[2]),
Integer.valueOf(stationData[3]), Double.valueOf(stationData[4]))) // Create StationRecord object using the split fields
.collect(Collectors.toList()); // Collect result to an ArrayList