如何从文本文件中读取数据并将其中的一些数据保存到数组中

我的计算机中有一个文本文件,我正在阅读我的java程序,我想建立一些标准。 这是我的记事本文件:

#Students #studentId studentkey yearLevel studentName token 358314 432731243 12 Adrian Afg56 358297 432730131 12 Armstrong YUY89 358341 432737489 12 Atkins JK671 #Teachers #teacherId teacherkey yearLevel teacherName token 358314 432731243 12 Adrian N7ACD 358297 432730131 12 Armstrong EY2C 358341 432737489 12 Atkins F4NGH 

当我从这个记事本文件中读取时,我得到了我的应用程序中的确切数据,但我想只读取学生中的标记列,并将它们放在名为studentTokens的数组中。 这是代码

 public static void main(String[] args) { ArrayList studentTokens = new ArrayList(); try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("c:/work/data1.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println (strLine); } //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } 

简短提示:

 private static Integer STUDENT_ID_COLUMN = 0; private static Integer STUDENT_KEY_COLUMN = 1; private static Integer YEAR_LEVEL_COLUMN = 2; private static Integer STUDENT_NAME_COLUMN = 3; private static Integer TOKEN_COLUMN = 4; public static void main(String[] args) { ArrayList studentTokens = new ArrayList<>(); try (FileInputStream fstream = new FileInputStream("test.txt"); InputStreamReader inputStreamReader = new InputStreamReader(fstream); BufferedReader br = new BufferedReader(inputStreamReader)) { String strLine; // Read File Line By Line while ((strLine = br.readLine()) != null) { strLine = strLine.trim(); if ((strLine.length() != 0) && (strLine.charAt(0) != '#')) { String[] columns = strLine.split("\\s+"); studentTokens.add(columns[TOKEN_COLUMN]); } } } catch (Exception e) {// Catch exception if any System.err.println("Error: " + e.getMessage()); return; } for (String s : studentTokens) { System.out.println(s); } } 

上面的代码不是完整的解决方案。 它提取所有令牌(学生和教师)。 我希望你能成功地让那里的学生代币工作……

而不是在java.io中做事,你必须考虑转移到java.nio ,它现在有一些非常好的API可以解决。

这是一个小代码,可以为您提供所需的结果。

 import java.io.BufferedReader; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; public class StudentToken { public static void main(String[] args) throws Exception { List studenttoken = new ArrayList(); Path sourcePath = Paths.get(args[0]); Charset charset = Charset.forName("US-ASCII"); BufferedReader reader = Files.newBufferedReader(sourcePath, charset); int width = 0; int height = 0; String line = null; line = reader.readLine(); while (!((line = reader.readLine()).trim()).equals("#Teachers")) { line = line.trim(); if (width == 0) { String[] str = line.split("\\s+"); width = str.length; } if (line.length() > 0) height++; } reader.close(); BufferedReader reader1 = Files.newBufferedReader(sourcePath, charset); reader1.readLine(); for (int i = 0; i < height; i++) { if(!((line = reader1.readLine()).trim()).equals("#Teachers")) { line = line.trim(); String[] str = line.split("\\s+"); for (int j = 0; j < width; j++) { // this condition will add only those elements which fall under token column. if (j == (height) && i != 0) studenttoken.add(str[j]); } } } reader1.close(); System.out.println(studenttoken); } } 

以下是测试运行的输出:

在此处输入图像描述

希望这可能有所帮助。 问候。

您可以逐行读取文件并使用String.split (“\ s +”)。

看看String.split方法,它将帮助您分割空间上的每一行。 拆分后,可以检索令牌列的值。 最后,调用ArrayList.add将其添加到列表中。

你很简单的打印线。 我相信可以有两种方式因为你的标记元素是第五个元素,你可以分割字符串并直接使用第5个元素

String splitString = strLine.split(“\ t”); String tokenvalue = splitString [4];

或者维护为csv文件以便于操作