Java - 只读取文件的第一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24666805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:56:18  来源:igfitidea点击:

Java - Only read first line of a file

java

提问by Doc Holiday

I only want to read the first line of a text file and put that first line in a string array.

我只想读取文本文件的第一行并将第一行放入字符串数组中。

This is what I have but its reading the whole file.

这就是我所拥有的,但它正在读取整个文件。

ex text in myTextFile:

myTextFile 中的前文本:

Header1,Header2,Header3,Header4,Header5
1,2,3,4,5
6,7,8,9,10





String line= System.getProperty("line.separator");
String strArray[] = new String[5];


String text = null;
BufferedReader brTest = new BufferedReader(new FileReader(myTextFile));
    text = brTest .readLine();
        while (text != line) {
            System.out.println("text = " + text );
             strArray= text.split(",");
         }

采纳答案by Elliott Frisch

If I understand you, then

如果我理解你,那么

String text = brTest.readLine();
// Stop. text is the first line.
System.out.println(text);
String[] strArray = text.split(",");
System.out.println(Arrays.toString(strArray));

回答by CoderCroc

I think you are trying to get one line only if it's not empty.

我认为您仅在不为空时才尝试获取一行。

You can use

您可以使用

 while ((text=brTest .readLine())!=null){
    if(!text.equals("")){//Ommit Empty lines
         System.out.println("text = " + text );
         strArray= text.split(",");
         System.out.println(Arrays.toString(strArray));
         break;
    }
 }

回答by bumbumpaw

use BufferedReader.readLine()to get the first line.

用于BufferedReader.readLine()获取第一行。

BufferedReader brTest = new BufferedReader(new FileReader(myTextFile));
    text = brTest .readLine();
   System.out.println("Firstline is : " + text);

回答by Hyman

You need to change the condition of your loop

您需要更改循环的条件

  String[] nextLine;   
  while((nextLine = brTest.readLine()) != null) {
      ...
  }

ReadLine reads each line from beginning up to the occurrence of \r andor \n You can also use tokenizerto split the string

ReadLine 从开始读取每一行直到出现 \r 和或 \n 也可以使用分词器来分割字符串

String[] test = "this is a test".split("\s");

In addition it seems the file is of type CSV if it is please mention that in the question.

此外,如果文件是 CSV 类型,请在问题中提及。

回答by Inder Sharma

Use this

用这个

BuffereedReader reader = new BufferedReader(new FileReader(textFile));
StringBuilder builder = new StringBuilder();

StringBuilder sb = new StringBuilder();
String line = br.readLine();

        while (line != null) {
            sb.append(line);
         break;
        }
if(sb.toString.trim().length!=0)
System.out.println("first line"+sb.toString);

回答by feob

With Java 8 and java.nioyou can also do the following:

使用 Java 8,java.nio您还可以执行以下操作:

String myTextFile = "path/to/your/file.txt";
Path myPath = Paths.get(myTextFile);
String[] strArray = Files.lines(myPath)
    .map(s -> s.split(","))
    .findFirst()
    .get();

If TAsksassumption is correct, you can realize that with an additional

如果TAsks假设是正确的,您可以通过额外的

.filter(s -> !s.equals(""))

回答by tmucha

Also, beside of all other solutions presented here, you could use guavautility class (Files), like below:

此外,除了此处介绍的所有其他解决方案之外,您还可以使用guava实用程序类 ( Files),如下所示:

import com.google.common.io.Files;
//...

String firstLine = Files.asCharSource(myTextFile).readFirstLine();

回答by Snow Bases

I hope this will help someone

我希望这会帮助某人

to read the first line:

阅读第一行:

public static String getFirstLine() throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("C:\testing.txt")); 

    String line = br.readLine(); 
    br.close();
    return line;
}

to read the whole text:

阅读全文:

public static String getText() throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("C:\testing.txt")); 
    StringBuilder sb = new StringBuilder(); 
    String line = br.readLine(); 

    while (line != null) { 
        sb.append(line).append("\n"); 
        line = br.readLine(); 
    } 
    String fileAsString = sb.toString();
    br.close();
    return fileAsString;
}