尝试仅获取包含1个单词的行。
当前方法获得正确的结果,但有时输入文件在每个单词之间有超过4行。所以需要一种方法来只获取包含1个单词的行。任何想法?
以下是输入文字的示例:
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)
答案 0 :(得分:3)
好吧,而不是
if ( lineNum % 4 == 0 ) continue;
条件,您只需检查您刚刚阅读的行是否包含多个令牌:
if (line.split(" ").length > 1) continue;
或
if (line.indexOf(" ") >= 0) continue;
后一种情况应该比前者更有效。
答案 1 :(得分:2)
取决于您对“字”的定义:
让我们坚持使用前两个,并使用正则表达式进行检查,因此我们也可以轻松忽略前导和尾随空格。这有三种方式:
if (line.matches("\\s*[a-zA-Z]+\\s*")) // One or more ASCII letters
outputStream.println(line);
if (line.matches("\\s*\\p{L}+\\s*")) // One or more Unicode letters
outputStream.println(line);
if (line.matches("\\s*\\S+\\s*")) // One or more non-space characters
outputStream.println(line);
对于MalformedInputException
,它是由代码页不匹配引起的(StreamDecoder
抛出异常)。
newBufferedReader(path)
以UTF-8读取文件,该文件可能位于系统默认代码页中,而不是UTF-8中。
改为使用newBufferedReader(path, Charset.defaultCharset())
。
答案 2 :(得分:1)
而不是
if ( lineNum % 4 == 0 ) continue;
只需检查行是否包含空格。
if(line.trim().contains(" ")) continue;
答案 3 :(得分:1)
您在java.io.BufferedReader.readLine(未知来源)中收到错误,因此找不到输入文件... 尝试更改文件名
String fileName = "InputWords";
to
String fileName = "InputWords.txt";
答案 4 :(得分:1)
工作!!需要添加字符集。
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();
}
}