用Java从文件中读取单词/短语,进行区分,然后写入另一个文件
我有一个文本文件,每行有一个单词或短语。我如何:用Java从文件中读取单词/短语,进行区分,然后写入另一个文件,java,file,sorting,distinct-values,Java,File,Sorting,Distinct Values,我有一个文本文件,每行有一个单词或短语。我如何: 把这些短语读入记忆 使不同(消除重复) 按字母顺序排序 将结果写回文件 StackOverflow在其他语言(如、Python和)中也有类似问题的答案。但是我找不到适用于Java的示例代码。下面的Java 8示例代码应该会有所帮助。它既不健壮,也没有经过很好的测试。并且它假设您的所有数据都可以轻松地存储在内存中。虽然不是完美的,但这个例子应该让你朝着正确的方向前进 ()定义了用于收集一组不同值的。作为该接口的实现,我们将使用 请注意,我们试图
- 把这些短语读入记忆
- 使不同(消除重复)
- 按字母顺序排序
- 将结果写回文件
StackOverflow在其他语言(如、Python和)中也有类似问题的答案。但是我找不到适用于Java的示例代码。下面的Java 8示例代码应该会有所帮助。它既不健壮,也没有经过很好的测试。并且它假设您的所有数据都可以轻松地存储在内存中。虽然不是完美的,但这个例子应该让你朝着正确的方向前进 ()定义了用于收集一组不同值的。作为该接口的实现,我们将使用 请注意,我们试图从每行中修剪任何空白。不幸的是,
String::trim
方法没有完成这项工作。在实际工作中,我会用调用更好的库来进行修剪来代替这个调用,例如。您可能希望删除所有非打印字符
我们使用isEmpty
方法测试每个字符串,以过滤掉没有任何字符的字符串
以下代码使用自动关闭任何打开的文件
// Read file.
// For each line, trim string.
// Add to Set to make collection of distinct values.
// Convert to List, and sort.
// Write back values to text file.
Set< String > set = new HashSet<>( );
String path = "/Users/yourUser/yourInputFile.txt";
File file = new File( path );
try ( BufferedReader br = new BufferedReader( new FileReader( file ) ) ) {
String line;
while ( ( line = br.readLine( ) ) != null ) {
// Process the line.
String keyword = line.trim( );
if ( ! keyword.isEmpty( ) ) { // If keyword is not empty, collect it.
set.add( keyword );
}
}
} catch ( IOException e ) {
e.printStackTrace( );
}
让我们将排序后的列表写入存储器中的文件
// Write to file.
// Use Try-With-Resources to automatically close the file if opened.
try (
FileWriter writer = new FileWriter( "/Users/yourUser/yourOutputFile.txt" ) ;
) {
for ( String k : keywords ) {
writer.write( k + "\n" ); // You may want a different newline instead of the Unix-style LINE FEED hard-coded here with "\n".
}
} catch ( IOException e ) {
e.printStackTrace( );
}
如果愿意,请在控制台上查看结果
System.out.println( "Size: " + keywords.size( ) );
System.out.println( "keywords: " + keywords );
System.out.println( "Done." );
您可以利用:
- API(从Java 7开始提供)
- API(从Java 8开始提供)
public static void main(String... args) {
Path input = Paths.get("/Users/yourUser/yourInputFile.txt");
Path output = Paths.get("/Users/yourUser/yourOutputFile.txt");
try {
List<String> words = getDistinctSortedWords(input);
Files.write(output, words, UTF_8);
} catch (IOException e) {
//log error and/or warn user
}
}
private static List<String> getDistinctSortedWords(Path path) throws IOException {
try(Stream<String> lines = Files.lines(path, UTF_8)) {
return lines.map(String::trim)
.filter(s -> !s.isEmpty()) // If keyword is not empty, collect it.
.distinct()
.sorted()
.collect(toList());
}
}
FileReader始终使用平台默认编码,这通常不是一个好主意-您可能应该改用InputStreamReader(并且在编写部分使用带有适当字符集的OutputStreamReader)。
public static void main(String... args) {
Path input = Paths.get("/Users/yourUser/yourInputFile.txt");
Path output = Paths.get("/Users/yourUser/yourOutputFile.txt");
try {
List<String> words = getDistinctSortedWords(input);
Files.write(output, words, UTF_8);
} catch (IOException e) {
//log error and/or warn user
}
}
private static List<String> getDistinctSortedWords(Path path) throws IOException {
try(Stream<String> lines = Files.lines(path, UTF_8)) {
return lines.map(String::trim)
.filter(s -> !s.isEmpty()) // If keyword is not empty, collect it.
.distinct()
.sorted()
.collect(toList());
}
}
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toList;