简体   繁体   English

Java I / O在隐藏特定单词的同时阅读文本

[英]Java i/o reading a text while hiding specific words

How would you read the following text while hiding the word SECRET each times it appears ? 每次出现SECRET字词时,如何隐藏下面的字词来阅读以下文字? here is the text : 这是文本:

this line has a secret word. 这行有一个秘密的词。

this line does not have a one. 这条线没有一个。

this line has two secret words. 这行有两个秘密词。

this line does not have any. 这行没有任何内容。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class BufferedReadertest {

    public static void main(String[] args) {

        ArrayList <String>list = new ArrayList<>();


        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("secretwords.txt"));

            while ((sCurrentLine = br.readLine()) != null) {
                list.add(sCurrentLine);

                }

            for (int i = 0; i < list.size(); i++) {

                System.out.println(list.get(i));
            }



        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}

你是说喜欢

System.out.println(list.get(i).replaceAll("SECRET", "******");

I'm n seeing the need to use an ArrayList. 我看不到需要使用ArrayList。 Replace SECRET and print the message when iterating the buffered reader. 迭代缓冲的阅读器时,请更换SECRET并打印消息。

public static void main(String[] args) {

        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("secretwords.txt"));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine.replaceAll("SECRET", ""));

            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
  }
if(line.indexOf("SECRET") > -1){
   System.out.println("Has Secret");
   continue;
}

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

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