Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.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_Text Files - Fatal编程技术网

在Java中从文本文件中读取特定行

在Java中从文本文件中读取特定行,java,text-files,Java,Text Files,从文本文件中提取特定行号数据的最有效方法是什么?例如,如果我使用扫描器解析一个文件,我是否必须首先创建一个长度与文本文件中总行数匹配的数组 如果一个文本文件有30行,而我只想处理第3行、第8行和第12行,有没有一种方法可以专门读取这些行?以下是您的操作方法: import java.io.*; public class Test { public static void main(String [] args) { // The name of the file to open.

从文本文件中提取特定行号数据的最有效方法是什么?例如,如果我使用扫描器解析一个文件,我是否必须首先创建一个长度与文本文件中总行数匹配的数组

如果一个文本文件有30行,而我只想处理第3行、第8行和第12行,有没有一种方法可以专门读取这些行?

以下是您的操作方法:

import java.io.*;

public class Test {
public static void main(String [] args) {

    // The name of the file to open.
    String fileName = "temp.txt";
    int counter = 0;

    // This will reference one line at a time
    String line = null;
    FileReader fileReader = null;

    try {
        // FileReader reads text files in the default encoding.
        fileReader = 
            new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = 
            new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {
            counter++;
            if(counter == 3 || counter == 8 || counter == 12)
            {
               // do your code
            }
        }   

    }
    catch(FileNotFoundException ex) {
        System.out.println(
            "Unable to open file '" + 
            fileName + "'");                
    }
    catch(IOException ex) {
        System.out.println(
            "Error reading file '" 
            + fileName + "'");                  
        // Or we could just do this: 
        // ex.printStackTrace();
    }
    finally
    {
        if(fileReader != null){
           // Always close files.
           bufferedReader.close();            
        }
    }
}
}
这是你能做的。(这只是你计划的一部分,并不完全是你计划的一部分)

试试这个

try
    {
        String sCurrentLine;
        br = new BufferedReader(new FileReader("File_Path"));
        int counter = 0;
        while ((sCurrentLine = br.readLine()) != null)
        {
            counter++;
            if (counter == 3 || counter == 8 || counter == 12)
            {
                System.out.println("" + br.read());
                if (counter == 12)
                    break;
            }
        }
    }
    catch (IOException e)
    {
        System.out.println("Exception " + e);

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

关于最后一个问题:根据这一点,Java8允许您从文件中提取特定的行。答案中提供了示例。

在这里,您可以找到根据行号读取文件的解决方案。此网站是所有编程语言中包含此代码的最佳网站。我已经给了你java代码


如果您不确定每条线的长度是否一致,则可能需要使用计数器循环!令人惊叹的。谢谢大家。我想我将使用计数器变量。有道理。@Jon,你真的应该接受其中一个答案,或者写下你的答案。你应该在
finally
块中关闭你的流,以确保它们不会被异常打开。现在你有了引用问题;)
bufferedReader
是在
try{…}
块的上下文中定义的,这意味着它对
块不可见。最后,
close
也可以抛出
IOException
。是的,我让你为那次投票工作;)@这就是为什么我讨厌快速编辑。。。哦,好吧,我会解决它的参考问题仍然存在。
try
    {
        String sCurrentLine;
        br = new BufferedReader(new FileReader("File_Path"));
        int counter = 0;
        while ((sCurrentLine = br.readLine()) != null)
        {
            counter++;
            if (counter == 3 || counter == 8 || counter == 12)
            {
                System.out.println("" + br.read());
                if (counter == 12)
                    break;
            }
        }
    }
    catch (IOException e)
    {
        System.out.println("Exception " + e);

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