Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java read values from text file

Tags:

java

I am new to Java. I have one text file with below content.

`trace` -
structure(
 list(
  "a" = structure(c(0.748701,0.243802,0.227221,0.752231,0.261118,0.263976,1.19737,0.22047,0.222584,0.835411)),
  "b" = structure(c(1.4019,0.486955,-0.127144,0.642778,0.379787,-0.105249,1.0063,0.613083,-0.165703,0.695775))
 )
)
  

Now what I want is, I need to get "a" and "b" as two different array list.

like image 649
Tapsi Avatar asked May 11 '11 08:05

Tapsi


People also ask

How do you read the contents of a file in Java?

We can use Files class to read all the contents of a file into a byte array. Files class also has a method to read all lines to a list of string. Files class is introduced in Java 7 and it's good if you want to load all the file contents.

How do you read a specific line from a text file in Java?

Java supports several file-reading features. One such utility is reading a specific line in a file. We can do this by simply providing the desired line number; the stream will read the text at that location. The Files class can be used to read the n t h nth nth line of a file.

How do you read a text file and store it to an array in Java?

In Java, we can store the content of the file into an array either by reading the file using a scanner or bufferedReader or FileReader or by using readAllLines method.


2 Answers

You need to read the file line by line. It is done with a BufferedReader like this :

try {
    FileInputStream fstream = new FileInputStream("input.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    String strLine;         
    int lineNumber = 0;
    double [] a = null;
    double [] b = null;
    // Read File Line By Line
    while ((strLine = br.readLine()) != null) {
        lineNumber++;
        if( lineNumber == 4 ){
            a = getDoubleArray(strLine);
        }else if( lineNumber == 5 ){
            b = getDoubleArray(strLine);
        }               
    }
    // Close the input stream
    in.close();
    //print the contents of a
    for(int i = 0; i < a.length; i++){
        System.out.println("a["+i+"] = "+a[i]);
    }           
} catch (Exception e) {// Catch exception if any
    System.err.println("Error: " + e.getMessage());
}

Assuming your "a" and"b" are on the fourth and fifth line of the file, you need to call a method when these lines are met that will return an array of double :

private static double[] getDoubleArray(String strLine) {
    double[] a;
    String[] split = strLine.split("[,)]"); //split the line at the ',' and ')' characters
    a = new double[split.length-1];
    for(int i = 0; i < a.length; i++){
        a[i] = Double.parseDouble(split[i+1]); //get the double value of the String
    }
    return a;
}

Hope this helps. I would still highly recommend reading the Java I/O and String tutorials.

like image 152
krookedking Avatar answered Oct 21 '22 03:10

krookedking


You can play with split. First find the line in the text that matches "a" (or "b"). Then do something like this:

Array[] first= line.split("("); //first[2] will contain the values

Then:

Array[] arrayList = first[2].split(",");

You will have the numbers in arrayList[]. Be carefull with the final brackets )), because they have a "," right after. But that is code depuration and it is your mission. I gave you the idea.

like image 26
Jav_Rock Avatar answered Oct 21 '22 04:10

Jav_Rock