简体   繁体   中英

Get some text from a file with java that we know the character and the line

Anyone have trick to get the pieces of text from a file ?

My goal is like this :

Assumpted, we need to read a java file. So,we will created a new java file that just need an input begin of line and character, untill end of line and character of Java file. For the Example, The java file like this :

package test;
public class FileDua {

public int method(int c, int d) {
while (d != 0) {
    if (c > d) {
        c = c - d;
    } else {
        d = d - c;
    }
}
return c;
}  

public int factorial(int n){
if(n == 0){
    return 1;
}else{
    return n * factorial(n-1);
  }
}
}

With buffered or some like that, we know to input :

(4.3 - 10.3)

means line 4 character 3 to line 10 character 3 ?

So, you know, voila, we get this :

public int method(int c, int d) {
while (d != 0) {
    if (c > d) {
        c = c - d;
    } else {
        d = d - c;
    }
}
return c;

}

or, another example 1.3 - 3.3 we get this :

package test;
public class FileDua {

Thanks for everyone that help. It is so appreciated.

update

I am newbie in java, so I can just something like this :

public class FinishingTouch {

public static void main(String[] args) {

    FileReader fileA = null;
    try {

        fileA = new FileReader("src\\FIleDua.java");
        try (BufferedReader br = new BufferedReader(fileA)) {
            String sCurrentLine;
            //print all of the contains
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }
            //now time to get just the line that I want
            String input = "";
            try {
                input = new String(Files.readAllBytes(Paths.get(fileA.toString())));
            } catch (IOException ex) {
                System.out.println("File: " + fileA.toString() + " not found!");
            }

            String[] lines = input.split("\r?\n");

        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException ex) {
        System.out.println("File Not Found");
    } finally {
        try {
            fileA.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

}

What about something like this?

input = "";
try {
    input = new String(Files.readAllBytes(Paths.get(INPUT_PATH)));
} catch (IOException ex) {
    System.out.println("File: " + INPUT_PATH + " not found!");
}

This will make input be a string containing all the text in your file. Then just extract the components of it that you want. You can separate your string into an array of strings by using the new line character as a separator so that each string component will be a single line in your file. Something like

String[] lines = input.split("\r?\n");

Then if you want the fourth through the tenth lines, just return lines[3] through lines[9] . I'm not sure what you mean by referencing character 3 in your examples. From what I can tell, you want to get back the entire line. I don't see where, when saying line 4 character 3, the character 3 comes into play at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM