繁体   English   中英

使用String.Split从文件中分离文本

[英]Separating texts from a File with String.Split nothing happens

已编辑

我正在尝试将文本拆分为数组。 我有一个由不同文本(例如文本集合)组成的.txt。 我需要将.txt中每个不同文本的整个文本放在数组的某个位置。 我使用JFileChooser从文件中恢复文本。 然后,我尝试使用“ regex” String.Split处理它,然后尝试打印它。 “ FileChooser的第一部分可以工作,但是当尝试在Array中分离文本时,我不知道它是否有效,因为System.out不会打印所有文本的预期Array。”

这是.txt的示例,每个文本都用“ * TEXT”分隔。

*TEXT 017 01/04/63 PAGE 020
THE ALLIES AFTER NASSAU IN DECEMBER 1960, THE U.S ........
*TEXT 020 01/04/63 PAGE 021
THE ROAD TO JAIL IS PAVED WITH NONOBJECTIVE ART SINCE THE KREMLIN'S SHARPEST BARBS THESE DAYS ARE AIMED AT MODERN ART AND WESTERN ESPIONAGE...
*TEXT 025 01/04/63 PAGE 024
RED CHINA FIXING FRONTIERS RED CHINA PRODUCED A SECOND SURPRISE LAST WEEK...

这是我的代码,首先是FileChooser,然后是String.Split

import java.io.*;
import java.lang.Object.*;
import java.util.regex.*;
import javax.swing.JFileChooser;

public class Reader{

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

        File inFile;
        FileReader fr;
        BufferedReader bufReader;
        JFileChooser chooser;

        int reply;
        String doc = "";

        String line;
        try{

            chooser = new JFileChooser();
            reply = chooser.showOpenDialog(null);
            doc = chooser.getCurrentDirectory().getPath() + System.getProperty("file.separator") + chooser.getSelectedFile().getName();
            inFile = new File(doc);
            fr = new FileReader(inFile);
            bufReader = new BufferedReader (fr);

            do{
                line = bufReader.readLine();
                if(line ==null )
                    return;
            } while(line!=null);

            //**HERE STARTS THE STRING.SPLIT**
            //"line" at the end of next line it supposed to be the whole .txt
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(line)));
            StringBuilder br = new StringBuilder();
            String newLine ="";
            while(true){
                if(line == null)
                    break;
                br.append(line);
            }
            newLine = br.toString();
            String arr[] = newLine.split("\\*TEXT");
            System.out.println(java.util.Arrays.toString(arr));
            //**HERE ENDS**


            bufReader.close();

        }//end try
        catch(Exception e)
        {   System.out.println("error: "+e.getMessage());   }

    }//main
}//end class reader

谢谢你的帮助! :3

您可以使用此正则表达式拆分文本:

^(?=\*TEXT)

工作演示

首先,您的代码永远不会到达split的调用。 您的代码只有两条路径:用户取消文件选择器对话框,这将导致NullPointerException ,或者文件被选择,这将不可避免地命中

...
do{
            line = bufReader.readLine();
            if(line ==null )
                return;
   } while(line!=null);

二,行

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(line)));

尝试使用变量line的值指定的名称打开文件 ,这可能不是您想要的。

您的代码的固定版本为:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import javax.swing.JFileChooser;

public class Reader{

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

        File inFile;
        FileReader fr;
        BufferedReader bufReader;
        JFileChooser chooser;

        int reply;

        try{

            chooser = new JFileChooser();
            reply = chooser.showOpenDialog(null);
            // Read all the lines in the file at once
            List<String> lines = Files.readAllLines(Paths.get(chooser.getSelectedFile().getAbsolutePath()), StandardCharsets.UTF_8);

            // Merge the read lines into a String
            StringBuilder sb = new StringBuilder();
            for (String line : lines){
                sb.append(line);
                sb.append('\n');
            }

            String newLine = sb.toString();

            // Split the String
            String arr[] = newLine.split("\\*TEXT");
            System.out.println(java.util.Arrays.toString(arr));

        }//end try
        catch(Exception e)
        {   System.out.println("error: "+e.getMessage());   }

    }//main
}//end class reader

请注意,仅从Java API 1.7开始提供API类FilesPaths

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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