[英]Java - Only read first line of a file
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中的ex文本:
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(",");
}
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));
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);
With Java 8 and java.nio
you 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 TAsks assumption is correct, you can realize that with an additional 如果TAsks假设是正确的,您可以通过额外的实现来实现
.filter(s -> !s.equals(""))
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;
}
}
Also, beside of all other solutions presented here, you could use guava
utility class ( Files
), like below: 此外,除了此处提供的所有其他解决方案之外,您还可以使用guava
实用程序类( Files
),如下所示:
import com.google.common.io.Files;
//...
String firstLine = Files.asCharSource(myTextFile).readFirstLine();
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);
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;
}
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 tokenizer to split the string ReadLine从开始到出现\\ r andor \\ n读取每一行你也可以使用tokenizer来分割字符串
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类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.