读取以特定行开头的文件

时间:2013-07-17 22:23:03

标签: java iostream java.util.scanner bufferedreader fileinputstream

我有这个脚本,我想从HDK文本行中提取行到每行SDK文本行

#! /bin/sh
# ==================================================================
#  ______                           __     _____
# /_  __/___  ____ ___  _________ _/ /_   /__  /
#  / / / __ \/ __ `__ \/ ___/ __ `/ __/     / /
# / / / /_/ / / / / / / /__/ /_/ / /_      / /
#/_/  \____/_/ /_/ /_/\___/\__,_/\__/     /_/

HDK 
# Multi-instance Apache Tomcat installation with a focus
# on best-practices as defined by Apache, SpringSource, and MuleSoft
# and enterprise use with large-scale deployments.
# Credits:
#       Google -> Couldn't survive without it
#       Stackoverflow.com -> Community support
#       SpringSource -> Specifically best-practices and seminars (Expert Series)
SDK 
# Based On:
#       http://www.springsource.com/files/uploads/tomcat/tomcatx-performance-tuning.pdf
http://www.springsource.com/files/u1/PerformanceTuningApacheTomcat-Part2.pdf
#       http://www.springsource.com/files/uploads/tomcat/tomcatx-large-scale-deployments.pdf

请帮忙! 我确实试过了

public class ScannerExample {

public static void main(String args[]) throws FileNotFoundException {

    //creating File instance to reference text file in Java
    File text = new File("E:\\file.txt");

    //Creating Scanner instnace to read File in Java
    Scanner scnr = new Scanner(text);

    //Reading each line of file using Scanner class
    int lineNumber = 1;
    int lineNumber1 = 1;
    int numinf = 1;
    int numsup=1;

    while(scnr.hasNextLine()){
        String line = scnr.nextLine();
        if (line.contains("HDK")){numinf=lineNumber;
        System.out.println(numinf);
  }

        if (line.contains("SDK")){numsup=lineNumber;
        System.out.println(numsup);}


        lineNumber++;
    }   
    Scanner scnr1 = new Scanner(text);

    while(scnr1.hasNextLine()){
        String line = scnr.nextLine();
        if (lineNumber>numinf){
        System.out.println("line " + lineNumber + " :" + line);}
        lineNumber1++;
    }       

  }   



  }

但它没有用,我用超过这种方法导航文件,我无法限制希望区域的读取!

1 个答案:

答案 0 :(得分:0)

我不确切地知道你面临的问题究竟是什么 但我可以在您的代码中看到问题

while(scnr1.hasNextLine()){
        String line = scnr.nextLine();//this line is a problem
        if (lineNumber>numinf){
        System.out.println("line " + lineNumber + " :" + line);}
        lineNumber1++;
    }

将scnr.nextLine()更改为scnr1.nextLine()
你的代码将被执行。