Java txt解析器新行问题

Java txt解析器新行问题,java,parsing,Java,Parsing,早上好。使用拆分方法的解析器遇到问题。目标是读入txt文件,提取should语句,然后用这些should语句编写一个新的txt文件。当文本在一个连续的行上时,我让它工作。如果在txt文件中有新行,则仅用最后一行重写该文件。可能是我的循环结构?还有关于从打开新文件的目录中保存新文件的建议吗?多谢各位 import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.Arr

早上好。使用拆分方法的解析器遇到问题。目标是读入txt文件,提取should语句,然后用这些should语句编写一个新的txt文件。当文本在一个连续的行上时,我让它工作。如果在txt文件中有新行,则仅用最后一行重写该文件。可能是我的循环结构?还有关于从打开新文件的目录中保存新文件的建议吗?多谢各位

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/*This Program Will launch a File Explorer.
User will then chose a .txt file to be parsed.
A new file will be created labeled "Parsed_(Document Name)".*/

public class Parser {

    @SuppressWarnings("resource")
    public static void main(String[] args) {

        JFileChooser chooser = new JFileChooser();
        Scanner userFile = new Scanner(System.in);

        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {

            try {
                System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName() + "\n");

                File file = new File(chooser.getSelectedFile().getName());
                String newFile = ("Parsed_" + file);

                userFile = new Scanner(file);

                while (userFile.hasNextLine()) {

                    String document = userFile.nextLine();
                    // Line breaks used by Parser
                    String[] sentences = document.split("\\.|\\?|\\!|\\r");

                    List<String> ShouldArray = new ArrayList<String>();

                    for (String shouldStatements : sentences) {

                        if (shouldStatements.contains("Should") || shouldStatements.contains("should"))
                            ShouldArray.add(shouldStatements);

                    }

                    FileWriter writer = new FileWriter(newFile);
                    BufferedWriter bw = new BufferedWriter(writer);

                    for (String shallStatements : ShouldArray) {

                        System.out.println(shallStatements);

                        bw.append(shallStatements);
                        bw.newLine();

                    }

                    System.out.println("\nParsed Document Created: " + newFile);

                    JOptionPane.showMessageDialog(null, "Parsed Document Created: " + newFile);
                    bw.close();

                    writer.close();

                }

                userFile.close();

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileWriter;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
导入javax.swing.JFileChooser;
导入javax.swing.JOptionPane;
/*此程序将启动文件资源管理器。
然后,用户将选择要解析的.txt文件。
将创建一个新文件,标记为“已解析(文档名)”*/
公共类解析器{
@抑制警告(“资源”)
公共静态void main(字符串[]args){
JFileChooser chooser=新的JFileChooser();
Scanner userFile=新扫描仪(System.in);
int returnVal=chooser.showOpenDialog(null);
if(returnVal==JFileChooser.APPROVE_选项){
试一试{
System.out.println(“您选择打开此文件:“+chooser.getSelectedFile().getName()+”\n”);
File File=新文件(chooser.getSelectedFile().getName());
字符串newFile=(“已解析文件”+文件);
userFile=新扫描仪(文件);
while(userFile.hasNextLine()){
String document=userFile.nextLine();
//解析器使用的换行符
String[]句=document.split(“\\.\\?\\\\!\\\\r”);
List ShouldArray=new ArrayList();
for(字符串shouldStatements:句子){
if(shouldStatements.contains(“应该”)| shouldStatements.contains(“应该”))
ShouldArray.add(shouldStatements);
}
FileWriter writer=新的FileWriter(新文件);
BufferedWriter bw=新的BufferedWriter(writer);
用于(字符串shallStatements:ShouldArray){
系统输出打印(shallStatements);
bw.附加(shallStatements);
换行符();
}
System.out.println(“\n创建的授权文档:“+newFile”);
showMessageDialog(null,“创建的解析文档:“+newFile”);
bw.close();
writer.close();
}
userFile.close();
}捕获(例外情况除外){
例如printStackTrace();
}
}
}
}
测试文件1(工作!)

大家好。这是一份装箱单。你应该有一把牙刷。你应该有一个手机充电器。你绝对应该有你的钱包

测试文件1输出:

你应该有一把牙刷 你应该有一个手机充电器 你肯定应该有你的钱包

测试文件2(仅打印最后一行)

大家好。这是一份装箱单。你应该有一把牙刷。你应该有一个手机充电器。 下面是一些随机文本,显示解析器将不包含此内容。
你绝对应该有你的钱包

测试文件2输出:


您肯定应该拥有钱包

您需要在循环之外创建结果数组

 /** Placed here**/
 List<String> ShouldArray = new ArrayList<String>();
 while (userFile.hasNextLine()) {

                String document = userFile.nextLine();
                // Line breaks used by Parser
                String[] sentences = document.split("\\.|\\?|\\!|\\r");

                /** REMOVED HERE **/

                for (String shouldStatements : sentences) {

                    if (shouldStatements.contains("Should") || shouldStatements.contains("should"))
                        ShouldArray.add(shouldStatements);

                }
               ......

最后,你的电路板上只有一个有限的结果集

你在循环中覆盖你的Arraylist,但是你实际上并不需要它

File file = chooser.getSelectedFile();
System.out.println("You chose to open this file: " + file.getName() + "\n");

String newFile = "Parsed_" + file.getName();

// open all closable objects using try-with-resources 
try (Scanner userFile = new Scanner(file); 
            BufferedWriter bw = new BufferedWriter(new FileWriter(newFile))) {

   while (userFile.hasNextLine()) {
      String document = userFile.nextLine();
     // Line breaks used by Parser
     String[] sentences = document.split("\\.|\\?|\\!|\\r");

    for (String s : sentences) {
        if (s.contains("Should") || s.contains("should")) {
            System.out.println(s);
            bw.append(s);
            bw.newLine();
      }
   } 

   System.out.println("\nParsed Document Created: " + newFile);

  JOptionPane.showMessageDialog(null, "Parsed Document Created: " + newFile);
   // bw.close(); // not needed anymore 

我重构了代码,删除了不需要的“ShouldArray”

伪码

While there are lines to read in the In file
    Read each line
    Split each line into Array of sentences

    Loop through each sentence
        If each sentence contains Should or should Then
          Write sentence to Out file
        End If
    End Loop
End While

Close Out file
Close In file
以下代码适用于:

多行:

Hello all. Here is a a packing list.
You Should have a toothbrush. You Should have a Phone charger.
Here is some random text to show the parser will not include this.
You definitely should have your wallet!
单线:

Hello all. Here is a a packing list. You Should have a toothbrush. You should have a Phone charger. And you definitely should have your wallet!


看起来您正在为每一行循环while循环。然后在每个循环中创建一个新文件并打印一行。因此,在1行之后,您将覆盖上一条语句。将扫描定界符设置为标点符号和换行符如何?
Hello all. Here is a a packing list. You Should have a toothbrush. You should have a Phone charger. And you definitely should have your wallet!
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;

public class ShouldStringsParser {

    public ShouldStringsParser(String inFile, String outFile) throws IOException {
        File file = new File(inFile);
        FileWriter writer = new FileWriter(outFile);
        BufferedWriter bw = new BufferedWriter(writer);
        Scanner userFile;
        userFile = new Scanner(file);
        String[] sentences;

        while (userFile.hasNextLine()) {
            String line = userFile.nextLine();
            System.out.println(line);

            sentences = line.split("\\.|\\?|\\!|\\r");

            for (String shouldStatements : sentences) {
                if (shouldStatements.contains("Should") || shouldStatements.contains("should")) {
                    System.out.println(">>>" + shouldStatements);
                    bw.append(shouldStatements);
                    bw.newLine();
                }
            }
        }

        bw.close();
        writer.close();
        userFile.close();
    }

    public static void main(String[] args) {
        try {
            new ShouldStringsParser("inDataMultiLine.txt", "outDataMultiLine.txt");

            new ShouldStringsParser("inDataSingleLine.txt", "outDataSingleLine.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}