我在.txt
文件中有以下数据,我想在Java中读到ArrayList
:
GPS BIIR-2 (PRN 13)
1 24876U 97035A 17141.14457135 .00000003 00000-0 00000-0 0 9998
2 24876 55.5552 228.1750 0035254 106.3776 254.0559 2.00561862145482
GPS BIIR-3 (PRN 11)
1 25933U 99055A 17141.73713684 -.00000027 00000-0 00000-0 0 9993
2 25933 51.6152 78.7972 0165513 94.1246 263.1302 2.00558460129112
GPS BIIR-4 (PRN 20)
1 26360U 00025A 17141.81895226 .00000099 00000-0 00000-0 0 9997
2 26360 53.1134 155.9897 0041040 91.6015 67.4278 2.00562427124819
GPS BIIR-5 (PRN 28)
1 26407U 00040A 17141.94548343 -.00000038 00000-0 00000-0 0 9999
2 26407 56.6412 346.1706 0199324 270.1305 261.3402 2.00566941123498
GPS BIIR-6 (PRN 14)
1 26605U 00071A 17141.89387574 .00000011 00000-0 00000-0 0 9994
2 26605 55.1263 225.8873 0093188 248.2178 154.4748 2.00565957121094
GPS BIIR-7 (PRN 18)
1 26690U 01004A 17141.89275187 .00000099 00000-0 00000-0 0 9997
2 26690 53.0355 158.9288 0182848 256.8341 254.6988 2.00552856119496
GPS BIIR-8 (PRN 16)
1 27663U 03005A 17141.54592882 -.00000038 00000-0 00000-0 0 9991
2 27663 56.7105 345.9236 0092607 24.3398 337.2543 2.00561164104855
GPS BIIR-9 (PRN 21)
1 27704U 03010A 17141.94336059 .00000017 00000-0 00000-0 0 9995
2 27704 53.8441 99.3104 0242356 264.2597 344.1458 2.00562515103650
GPS BIIR-10 (PRN 22)
1 28129U 03058A 17141.33287910 .00000097 00000-0 00000-0 0 9991
2 28129 52.9229 158.9448 0077529 260.9570 98.2202 2.00545637 98328
GPS BIIR-11 (PRN 19)
1 28190U 04009A 17141.74233858 -.00000063 00000-0 00000-0 0 9997
2 28190 55.9902 46.9639 0100903 55.1890 240.6200 2.00564687 96524
GPS BIIR-12 (PRN 23)
1 28361U 04023A 17141.86069817 .00000018 00000-0 00000-0 0 9997
2 28361 54.1113 221.0276 0118403 219.7092 72.6776 2.00554771 94586
GPS BIIR-13 (PRN 02)
1 28474U 04045A 17141.27054522 .00000022 00000-0 00000-0 0 9993
2 28474 54.2236 98.7980 0170296 246.9269 339.1728 2.00549707 91965
GPS BIIRM-1 (PRN 17)
1 28874U 05038A 17141.73783501 -.00000064 00000-0 00000-0 0 9994
2 28874 56.1469 44.2221 0118454 254.4937 59.3377 2.00568905 85376
如您所见,数据将是每三行一个完整的对象。那么,如何将String
元素添加到Java中的ArrayList中,每个元素由.txt
文件中的三行组成?
我成功地将整个文件读入单个元素,并且可以在字符串中搜索值,但我希望每组数据(三行)都是ArrayList
中的新元素。
这是我目前使用的方法。显然,这只是在每行的ArrayList中创建一个新元素。
private void menuLoadTleFileActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Choose a TLE file...");
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"TXT Files", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();
consoleTextArea.append("TLE File Loaded!");
ArrayList<String> lines = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(selectedFile)))
{
String currentLine;
while ((currentLine = br.readLine()) != null){
System.out.println(currentLine);
lines.add(currentLine);
consoleTextArea.append(currentLine);
}
} catch (IOException ex) {
System.out.println(ex);
}
}
}
答案 0 :(得分:0)
ArrayList可以包含数组列表。所以,只需做这样的事情:
public List<String[]> readGeoMessages(InputStream in) {
List<String[]> geoMessages=new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String str;
while ((str=reader.readLine())!=null) {
String[] rec=new String[3];
rec[0]=str;
rec[1]=reader.readLine();
rec[2]=reader.readLine();
geoMessages.add(rec);
}
return geoMessages;
}