Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 只提取一个单词的行?_Java_String_Bufferedreader - Fatal编程技术网

Java 只提取一个单词的行?

Java 只提取一个单词的行?,java,string,bufferedreader,Java,String,Bufferedreader,尝试只获取其中包含1个单词的行 当前方法得到正确的结果,但有时输入文件的每个字之间有超过4行。所以需要一种方法,只获取包含一个单词的行。有什么想法吗 以下是输入文本的示例: adversary someone who offers opposition The students are united by shared suffering, and by a common adversary. — New York Times (Nov 10, 2014) aplomb great cooln

尝试只获取其中包含1个单词的行

当前方法得到正确的结果,但有时输入文件的每个字之间有超过4行。所以需要一种方法,只获取包含一个单词的行。有什么想法吗

以下是输入文本的示例:

adversary
someone who offers opposition
The students are united by shared suffering, and by a common adversary. 
— New York Times (Nov 10, 2014)
aplomb
great coolness and composure under strain
I wish I had handled it with aplomb. 
— New York Times (May 18, 2014)
apprehensive
因此,输出应该如下所示:

adversary
aplomb
apprehensive
以下是迄今为止的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Process {

    public static void main(String[] args) {

        String fileNameOutput = "OutputFile.txt";
        String fileName = "InputWords";

        try (BufferedReader bReader = Files.newBufferedReader(Paths.get(fileName))){

            PrintWriter outputStream = new PrintWriter(fileNameOutput); 
            int lineNum = 0;
            String line = null;

            while ( (line = bReader.readLine() ) != null ) {
               lineNum++;

             if ( lineNum % 4 == 0 ) continue;


                outputStream.println(line);


            }
                outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }



    }

}
谢谢你抽出时间


编辑

从下面建议的修复程序中从控制台获取此错误

java.nio.charset.MalformedInputException: Input length = 1
    at java.nio.charset.CoderResult.throwException(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at Process.main(Process.java:20)
而不是

if ( lineNum % 4 == 0 ) continue;
if ( lineNum % 4 == 0 ) continue;
条件下,您只需检查刚才读取的行是否包含多个令牌:

if (line.split(" ").length > 1) continue;

后者应该比前者更有效。

而不是前者

if ( lineNum % 4 == 0 ) continue;
if ( lineNum % 4 == 0 ) continue;
只需检查行是否包含空格

if(line.trim().contains(" ")) continue;

您在java.io.BufferedReader.readLine(未知源)处收到一个错误,因此找不到输入文件。。。 尝试更改文件名

String fileName = "InputWords";

to

String fileName = "InputWords.txt";

取决于您对“单词”的定义:

  • 一系列字母
  • 非空白字符的任意字符序列
  • 表示一个单词的字形(如中文)
让我们继续使用前两个,并使用正则表达式进行检查,这样我们也可以轻松地忽略前导和尾随空格。这里有三种方法:

if(line.matches(“\\s*[a-zA-Z]+\\s*”)//一个或多个ASCII字母
outputStream.println(行);
if(line.matches(“\\s*\\p{L}+\\s*”)//一个或多个Unicode字母
outputStream.println(行);
if(line.matches(“\\s*\\s+\\s*”)//一个或多个非空格字符
outputStream.println(行);

至于
格式错误的InputException
,它是由代码页不匹配引起的(异常是由
StreamDecoder
引发的)

newbufferederader(path)
读取UTF-8中的文件,该文件可能位于系统默认代码页中,而不是UTF-8中


使用新缓冲区(路径,Charset.defaultCharset())。

工作!!需要添加字符集

   public static void main(String args[]){
        //testAnimal();
         String fileNameOutput = "OutputFile.txt";
            String fileName = "InputWords.txt";

            Charset cs = Charset.defaultCharset() ;
            try (BufferedReader bReader = Files.newBufferedReader(Paths.get(fileName), cs)){

                PrintWriter outputStream = new PrintWriter(fileNameOutput); 
                int lineNum = 0;
                String line = null;

                while ( (line = bReader.readLine() ) != null ) {
                   lineNum++;

                  if (line.split(" ").length > 1) continue;


                    outputStream.println(line);


                }
                    outputStream.close();

            } catch (IOException e) {
                e.printStackTrace();
            }


   }

啊,太棒了,现在就试试。干杯是的,有一个错误。。。(格式不正确的PutException:Input length=1)@JonathanLaliberte哪行代码给出了该异常?不完全确定如何找到该异常。你是说这个?“at Process.main(Process.java:20)”@JonathanLaliberte Process.java的第20行是什么?”类型字符串中包含的方法(CharSequence)不适用于参数(char)“谢谢,但出于某种原因,即使有此建议,仍然会出现这个该死的错误。有什么想法吗?谢谢!我尝试了一下,但仍然得到了描述中的错误。。。知道为什么吗?是的,读取UTF-8格式的文件,文件可能不是UTF-8格式,而是系统默认的代码页。改用
newbufferedraider(path,Charset.defaultCharset())
。但是代码运行良好,没有任何此处建议的编辑。这不可能是问题所在。因为错误发生在
BufferedReader.readLine()
中,并且没有人告诉您更改该调用,所以错误不是由我们所说的引起的。错误是由
-
字符引起的,该字符不是符号,而是符号。