在文件中搜索特定文本并在JAVA中复制以下内容

时间:2018-02-20 08:25:33

标签: java file bufferedreader

我正在使用BufferedReader读取文件并在文件中查找文本。如果找到文本,则需要将以下内容复制到另一个文件中。 该文件的内容如下所示:

...............
Transforming com.depositmobi.ActivationDoneActivity... 
Transforming com.depositmobi.ReadOffertActivity... 
Transforming dummyMainClass... 
Inside Possible Callers !
cg.size(): 976
sources: false null
Finished !!!!
CLINIT edge: $r1 = new com.depositmobi.Main in <dummyMainClass: void dummyMainMethod(java.lang.String[])> ==> <com.depositmobi.Main: void <clinit>()>
STATIC edge: staticinvoke <com.depositmobi.Main: void <clinit>()>() in <dummyMainClass: void dummyMainMethod(java.lang.String[])> ==> <com.depositmobi.Main: void <clinit>()>
CLINIT edge: $r12 = new android.view.View in <dummyMainClass: void dummyMainMethod(java.lang.String[])> ==> <android.view.View: void <clinit>()>
CLINIT edge: $r11 = new android.os.Bundle in <dummyMainClass: void dummyMainMethod(java.lang.String[])> ==> <android.os.Bundle: void <clinit>()>
CLINIT edge: $r10 = new com.depositmobi.ReadOffertActivity in <dummyMainClass: void dummyMainMethod(java.lang.String[])> ==> <android.app.Activity: void <clinit>()>
 ................

如果在文本文件中找到&#39; Finished !!!!&#39; 这个词,则将整个内容复制到名为 soot.txt <的文件中/ em>的

使用以下代码,我可以在完成之前阅读内容!!!&#39;。完成后如何获取内容?

BufferedReader br = new BufferedReader(new FileReader(file));
while (!br.readLine().startsWith("Finished !!!!")) {
            System.out.println(br.readLine() );
    }

2 个答案:

答案 0 :(得分:0)

试试这段代码。

BufferedReader br = new BufferedReader(new FileReader(file)); 
String line;
Boolean finishedFound = Boolean.FALSE;
StringBuilder contents = new StringBuilder();
while ((line = br.readLine()) != null) {
            if(line.startsWith("Finished !!!!")) {
                finishedFound = Boolean.TRUE;
                continue;
            }
            if(finishedFound) {
                contents.append(line+System.lineSeparator());
            }

        }
 // Here your code to write the contents to soot.txt

答案 1 :(得分:0)

你可以这样做:

class Alert extends Component {
  constructor(props) {
    super(props);

    this.state = {
      text: props.text
    };
  }

  componentWillReceiveProps(e) {
    this.setState(e);
  }

  onHide = () => {
    this.setState({ text: '' });
  };

  render() {
    return (
      this.state.text ? (
        <div style={{backgroundColor: 'Red'}}>
          {this.state.text}
          <button onClick={this.onHide}>X</button>
        </div>
      ) : null
    )
  }
}

class Form extends Component {
  constructor() {
    super();
    this.state = {
      error: ''
    };
  }

  onRender = () => {
    this.setState({ error: '123' });
  };

  onHideRender = () => {
    this.setState({ error: '' });
  };

  render() {
    return (
      <div>
        <button onClick={this.onRender}>Render Alert</button>
        <button onClick={this.onHideRender}>Hide Alert</button>
        <Alert text={this.state.error} />
      </div>
    );
  }
}