如何通过filereader读取txt文件并写入修改内容?

问题描述 投票:0回答:1

我必须通过filereader读取一个txt文件,并通过删除所有以“c”和“C”开头的单词来进行审查,并通过创建一个新的txt文件作为审查来写入它。我已经完成了下面的操作,但是我需要使用 Stream() 以一种方法进行读取和写入。 我已经完成了类似的 smtg,但它必须与流一起使用,而不是通过创建不同的列表和字符数组等。


我已经完成了类似的 smtg,但它必须使用流,而不是通过创建不同的列表和字符数组等。

public class Censored {
    static String words = "";
    static String censoredWords = "";
    static List<String> myWords = new ArrayList<>();

    public static void main(String[] args) {    
        System.out.println(censorTxt());
        convertListToStringAndChar();
    }

    private static List<String> censorTxt() {
        try (
                FileReader in = new FileReader("resources/input.txt");
                ){          
            int read = 0;
            while (read != -1) {
                read = in.read();
                String myChar = Character.toString((char) read);                 
                String word = myChar;
                words = words + word;
            }
            String[] splitedTextArray = words.split(" ");
            for (String elements : splitedTextArray) {
                if (!elements.startsWith("c")) {                    
                    myWords.add(elements);
                }
            }
            return myWords;
        } catch (IOException e) {
            System.out.println("smtg went wrong");
        }
        return null;
    }

    private static void convertListToStringAndChar() {
        try {
            FileWriter out = new FileWriter("resources/censored_content.txt");
            for (String strings : myWords) {
                
                censoredWords += strings;
            }
            for (int i = 0 ; i < censoredWords.length() ; i++) {
                int c = censoredWords.charAt(i);                
                out.write(c);                   
            }               
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }       
    }
}
split java-8 filereader java-io filewriter
1个回答
0
投票

可以使用

Files.lines()
在输入文件中提供字符串流来实现。

这里不创建中间列表,审查后的单词被写入特定的文本文件中。

public static void readAndFilter(String inputFile, String outputFile) throws IOException {
    try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputFile), StandardOpenOption.CREATE);
         Stream<String> input = Files.lines(Paths.get(inputFile))) {
            input
             .flatMap(line -> Arrays.stream(line.split("\\s+")))
             .filter(word -> !word.isEmpty())
             .filter(word -> 'c' == Character.toLowerCase(word.charAt(0)))
             .forEach(censored -> {
                 try {
                     writer.write(censored);
                     writer.newLine();
                 } catch (IOException ioex) {
                     System.err.println(ioex);
                 }
             });
    }
}

public static void main(String[] args) throws IOException {
    readAndFilter("my_input.txt", "censored.txt");
}
© www.soinside.com 2019 - 2024. All rights reserved.