java 从文本文件中读取数据的java程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15744827/
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
Program in java that reads data from a text file
提问by userefoikon
date time kg
12/10/2013 00.00.01 1
13/11/2013 00.00.05 2
17/12/2013 00.00.90 5
21/12/2013 00.00.23 6
27/12/2013 00.00.43 9
I have these data in an txt file. I would like to make o program in java that would read these data. I ' ve written the code above but I have mistakes. Could someone help me? The data have space between each other.
我在 txt 文件中有这些数据。我想在java中制作o程序来读取这些数据。我已经编写了上面的代码,但是我有错误。有人可以帮助我吗?数据之间有空间。
import java.io*;
public class ReadTextfile{
public static void main (String[] args) {
File file = new File ("test.txt");
StringBuilder line = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader (new FileReader(file));
String text = null;
while ((text = reader.readLine()) !=null) {
line.append(text)
.append(System.getProperty ("line.separator"));
}
}
catch (IOException e) {
e.printStackTrace();
catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
try {
if (reader !=null){
reader.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(line.toString());
}
}
回答by Maroun
catch (FileNotFoundException e)
This is unreachable code, since above it you caught IOException
.
这是无法访问的代码,因为在它上面你捕获了IOException
.
Note that:
需要注意的是:
public class FileNotFoundException extends IOException
Your code won't compile. Remove this catch (You didn't even close it..)
您的代码将无法编译。删除这个捕捉(你甚至没有关闭它..)
Another thing, if this is not a type, you should replace java.io*
with import java.io.*
.
另一件事,如果这不是类型,则应替换java.io*
为import java.io.*
.
回答by Hussain Akhtar Wahid 'Ghouri'
boy you are only having some syntax problem
男孩,你只是有一些语法问题
1 : replace
1:更换
import java.io* with import java.io.*
2 : take care of your catch body being started and closed properly
2 : 照顾好你的catch body是否正确启动和关闭
try
{
// your code
}
catch(Exception e)
{
}
here is the working code , compare your program
这是工作代码,比较你的程序
import java.io.*;
public class ReadTextfile{
public static void main (String[] args)
{
File file = new File ("C:/Users/hussain.a/Desktop/test.txt");
StringBuilder line = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader (new FileReader(file));
String text = null;
while ((text = reader.readLine()) !=null) {
line.append(text)
.append(System.getProperty ("line.separator"));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if (reader !=null){
reader.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
System.out.println(line.toString());
}
}
回答by dsingleton
I would take the following approach:
我会采取以下方法:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReadTextFile
{
public static void main(String[] args) throws FileNotFoundException
{
File file = new File("test.txt");
Scanner scanner = new Scanner(file);
List<Result> results = new ArrayList<Result>();
while(scanner.hasNextLine())
{
String currentLine = scanner.nextLine();
String [] resultArray = currentLine.split(" ");
results.add(new Result(resultArray[0], resultArray[1], resultArray[2]));
}
scanner.close();
}
private static class Result
{
private String date;
private String time;
private String kg;
public Result(String date, String time, String kg)
{
super();
this.date = date;
this.time = time;
this.kg = kg;
}
public String getDate()
{
return date;
}
public String getTime()
{
return time;
}
public String getKg()
{
return kg;
}
}
}
Now you can pull out any information that you want to from the list of results that you have.
现在您可以从您拥有的结果列表中提取您想要的任何信息。
So if you wanted to print everything, you could do the following:
因此,如果您想打印所有内容,可以执行以下操作:
for(Result singleResult : results)
{
System.out.println(singleResult.getDate() + " " + singleResult.getTime() + " " + singleResult.getKg());
}
You basically can do whatever you want to with the data. This approach would also allow you to transform the data into different types before you even create the Result object.
你基本上可以对数据做任何你想做的事情。这种方法还允许您在创建 Result 对象之前将数据转换为不同的类型。