簡體   English   中英

Java I / O在隱藏特定單詞的同時閱讀文本

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

每次出現SECRET字詞時,如何隱藏下面的字詞來閱讀以下文字? 這是文本:

這行有一個秘密的詞。

這條線沒有一個。

這行有兩個秘密詞。

這行沒有任何內容。

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", "******");

我看不到需要使用ArrayList。 迭代緩沖的閱讀器時,請更換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