用java从文件中获取一些文本,我们知道字符和行

用java从文件中获取一些文本,我们知道字符和行,java,Java,有人有从文件中获取文本片段的技巧吗 我的目标是: 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){

有人有从文件中获取文本片段的技巧吗

我的目标是:

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);
  }
}
}
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();
        }
    }
}
假设,我们需要读取一个java文件。所以,我们将创建一个新的java文件,它只需要输入行和字符的开头,直到java文件的行和字符的结尾。例如,java文件如下所示:

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);
  }
}
}
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();
        }
    }
}
对于缓冲或类似的,我们知道输入:

(4.3 - 10.3)
指第4行第3字符到第10行第3字符

所以,你知道,瞧,我们得到了这个:

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

或者,另一个例子1.3-3.3我们得到:

public int method(int c, int d) {
while (d != 0) {
    if (c > d) {
        c = c - d;
    } else {
        d = d - c;
    }
}
return c;
package test;
public class FileDua {
谢谢大家的帮助。非常感谢

update
我是java新手,所以我可以这样做:

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

}像这样的东西怎么样

input = "";
try {
    input = new String(Files.readAllBytes(Paths.get(INPUT_PATH)));
} catch (IOException ex) {
    System.out.println("File: " + INPUT_PATH + " not found!");
}
这将使
input
成为包含文件中所有文本的字符串。然后只提取你想要的成分。通过使用新行字符作为分隔符,可以将字符串分隔为字符串数组,以便每个字符串组件都是文件中的一行。差不多

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

然后,如果需要第四行到第十行,只需返回
行[3]
行[9]
。我不知道你在例子中引用字符3是什么意思。据我所知,你想收回整条线。当我说第4行第3个字符时,我看不出第3个字符在哪里起作用。

你能用某种方式澄清这个问题吗?输入字符的意义是什么?如何传递输入
(4.3-10.3)
?以什么形式?作为两个
?作为
字符串
?像这样的
getText(intstartLine、intstartChar、intendLine、intendChar)
方法对您有用吗?如果您展示了迄今为止所做的尝试,它也会有所帮助