如何将文本文件分为两部分?

时间:2018-10-29 17:25:01

标签: java

我正在读取文本文件以使用Scanner解析数据。该模式不是唯一的,并且重复进行使得难以解析。我想在原始文件的相同位置将文本文件拆分为第二部分。第二个文件应从特定字符串开始。这样,我可以在使用扫描仪时指向其他文件。

filePath = Paths.get(fileLocation);
Scanner scan = new Scanner(filePath);

文本文件内容如下

Initial data  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxx Inputs xxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxx Outputs xxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

我想将Outputs开头的数据放入另一个文本文件

1 个答案:

答案 0 :(得分:1)

        //br is the bufferedreader
    String line=null;
        boolean outputflag=false;
        while((line=br.readLine())!=null){
        if(line.contains("Outputs")) {

        outputflag=true;
//if you do not want the word Outputs in the output file just add 7 to start index
line=line.substring(line.indexOf("Outputs"),line.length());
//write line to outputs file
        }

        if(outputflag) {
        //write line to ouputfile
        }
        }