如何从文本文件中读取数据并将其中的一些数据保存到数组中 [英] How to read data from a text file and save some data from it to an Array

查看:143
本文介绍了如何从文本文件中读取数据并将其中的一些数据保存到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a text file in my computer which I am reading form my java program, I want to build some criteria. Here is my Notepad File :

   #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的数组中。这是代码

when I read from this note pad file, I get the exact data as it is in my application but I want to read only the token column inside students and put them in my array named studentTokens. Here is the code

public static void main(String[] args) {
    ArrayList<String > studentTokens = new ArrayList<String>();

    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());
    }
}


推荐答案

A简短提示:

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<String> 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);
    }
}

上述代码不是完整的解决方案。它提取所有令牌(学生和教师)。我希望你能从那里开始让它适用于学生代币......

The above code is not complete solution. It extracts all tokens (for students and teachers). I hope you'll manage to make it work just for student tokens from there on...

这篇关于如何从文本文件中读取数据并将其中的一些数据保存到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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