English 中文(简体)
如何阅读文本档案中的数据并从中提取某些数据到阿雷拉
原标题:How to read data from a text file and save some data from it to an Array

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

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());
    }
}
最佳回答

短ip:

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...

问题回答

您可以按行文读到档案线,并使用String.split(“s+”)。

查阅String.split,这将有助于你在空间上分立每一条线。 一旦分离,你就能够收回被打字栏的价值。 最后,请ArrayList.add,将其添加到您的名单上。

You are simple printing the line . I believe there are can be two ways Since your token element is the fifth element, you can split the string and utilize the 5th element directly

String splitString = strLine.split(" "); String tokenvalue = splitString[4];

或作为轻松操纵的卷宗





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签