开发者

Find a specific line of a text file (not by line_number) and store it as a new String

I am trying to read a text file in java using FileReader and BufferedReader classes. Following an online tutorial I made two classes, one called ReadFile and one FileData. Then I tried to extract a small part of the text file (i.e. between lines "ENTITIES" and "ENDSEC"). Finally l would like to tell the program to find a specific line between the above-mentioned and store it as an Xvalue, which I could use later. I am really struggling to figure out how to do the last part...any help would be very much apprciated!

//FileData Class

    package textfiles;

    import java.io.IOException; 


    public class FileData {

public static void main (String[] args) throws IOException {

    String file_name = "C:/Point.txt";

    try {

        ReadFile file = new ReadFile (file_name);
        String[] aryLines = file.OpenFile();

        int i;
        for ( i=0; i < aryLines.length; i++ ) {
        System.out.println( aryLines[ i ] ) ;
}

    }

    catch (IOException e) {
        System.out.println(e.getMessage() );
    }

 }

 }

// ReadFile Class

    package textfiles;

    import java.io.IOException;
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.lang.String;

    public class ReadFile {

private String path;

public ReadFile (String file_path) {
    path = file_path;
}

public String[] OpenFile() throws IOException {

    FileReader fr = new FileReader (path);
    BufferedReader textReader = new BufferedReader (fr); 

     int numberOfLines = readLines();
      String[] textData = new String[numberOfLines];
     String nextline = "";

     int i;
             // String Xvalue; 

    for (i=0; i < numberOfLines; i++) {
         String oneline = textReader.readLine();

         int j = 0;

         if (oneline.equals("ENTITIES")) {
             nextline = oneline;
             System.out.println(oneline);
             while (!nextline.equals("ENDSEC")) {
                 nextline = textReader.readLine();
                 textData[j] = nextline;

            //  xvalue = ..........

                 j = j + 1;
                 i = i+1;
             }
         }       
         //textData[i] = textReader.readLine();
     }

     textReader.close( );
     return textData;

}

int readLines() throws IOException {

    FileReader file_to_read = new FileReader (开发者_运维技巧path);
    BufferedReader bf = new BufferedReader (file_to_read);

    String aLine;
    int numberOfLines = 0;

    while (( aLine = bf.readLine()) != null ) {
        numberOfLines ++;
    }

    bf.close ();

    return numberOfLines;

}

}


I don't know what line you are specifically looking for but here are a few methods you might want to use to do such operation:

private static String START_LINE = "ENTITIES";
private static String END_LINE = "ENDSEC";

public static List<String> getSpecificLines(Srting filename) throws IOException{
    List<String> specificLines = new LinkedList<String>();
    Scanner sc = null;
    try {
        boolean foundStartLine = false;
        boolean foundEndLine = false;
        sc = new Scanner(new BufferedReader(new FileReader(filename)));
        while (!foundEndLine && sc.hasNext()) {
            String line = sc.nextLine();
            foundStartLine = foundStartLine || line.equals(START_LINE);
            foundEndLine = foundEndLine || line.equals(END_LINE);
            if(foundStartLine && !foundEndLine){
                specificLines.add(line);
            }
        }
    } finally {
        if (sc != null) {
            sc.close();
        }
    }
    return specificLines;
}

public static String getSpecificLine(List<String> specificLines){
    for(String line : specificLines){
        if(isSpecific(line)){
            return line;
        }
    }
    return null;
}

public static boolean isSpecific(String line){
    // What makes the String special??
}


When I get it right you want to store every line between ENTITIES and ENDSEC?

If yes you could simply define a StringBuffer and append everything which is in between these to keywords.

// This could you would put outside the while loop
StringBuffer xValues = new StringBuffer();

// This would be in the while loop and you append all the lines in the buffer
xValues.append(nextline);

If you want to store more specific data in between these to keywords then you probably need to work with Regular Expressions and get out the data you need and put it into a designed DataStructure (A class you've defined by our own).

And btw. I think you could read the file much easier with the following code:

BufferedReader reader = new BufferedReader(new
InputStreamReader(this.getClass().getResourceAsStream(filename)));

try {
   while ((line = reader.readLine()) != null) {
     if(line.equals("ENTITIES") {
       ...
     }

} (IOException e) {
   System.out.println("IO Exception. Couldn't Read the file!");
}

Then you don't have to read first how many lines the file has. You just start reading till the end :).

EDIT:

I still don't know if I understand that right. So if ENTITIES POINT 10 1333.888 20 333.5555 ENDSEC is one line then you could work with the split(" ") Method.

Let me explain with an example:

String line = "";
String[] parts = line.split(" ");
float xValue = parts[2]; // would store 10
float yValue = parts[3]; // would store 1333.888
float zValue = parts[4]; // would store 20
float ...    = parts[5]; // would store 333.5555

EDIT2:

Or is every point (x, y, ..) on another line?!

So the file content is like that:

ENTITIES POINT 
10 
1333.888 // <-- you want this one as xValue 
20 
333.5555 // <-- and this one as yvalue?
ENDSEC

BufferedReader reader = new BufferedReader(new
InputStreamReader(this.getClass().getResourceAsStream(filename)));

try {
   while ((line = reader.readLine()) != null) {
     if(line.equals("ENTITIES") {

        // read next line
        line = reader.readLine();
        if(line.equals("10") {
         // read next line to get the value
         line = reader.readLine(); // read next line to get the value
         float xValue = Float.parseFloat(line);
        }

        line = reader.readLine();

        if(line.equals("20") {
           // read next line to get the value
           line = reader.readLine(); 
           float yValue = Float.parseFloaT(line);
        }
     }

} (IOException e) {
   System.out.println("IO Exception. Couldn't Read the file!");
}

If you have several ENTITIES in the file you need to create a class which stores the xValue, yValue or you could use the Point class. Then you would create an ArrayList of these Points and just append them..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜