Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取以特定行开头的文件_Java_Iostream_Java.util.scanner_Bufferedreader_Fileinputstream - Fatal编程技术网

Java 读取以特定行开头的文件

Java 读取以特定行开头的文件,java,iostream,java.util.scanner,bufferedreader,fileinputstream,Java,Iostream,Java.util.scanner,Bufferedreader,Fileinputstream,我有这个脚本,我想从带有HDK文本的行中提取行,到每行带有SDK文本的行 #! /bin/sh # ================================================================== # ______ __ _____ # /_ __/___ ____ ___ _________ _/ /_ /__ / # / / / __ \/ __ `__ \/ ___/ __ `/

我有这个脚本,我想从带有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++;
    }       

  }   



  }

但是它不起作用,我用比这个方法更多的方法导航文件,我不能限制在所希望的区域中的阅读

我不知道你面临的电子商务问题到底是什么。
但是我可以看到您的代码中有一个问题

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()

您的代码将被执行。

欢迎使用StackOverflow。询问代码的问题必须证明对正在解决的问题的最低理解。包括尝试过的解决方案、它们不起作用的原因以及预期结果。另请参见:我想从行号=numinf开始读取,这就是为什么我将line=scnr.nextline()放在这里,因为您已经完成了从scnr引用的读取,所以当您尝试使用scnr在第二个扫描仪循环中读取时,您的代码将出现异常。所以我的理解是你需要一个完整的解决方案。