在Java中读取包含3列的写字板文件
我想读取包含空格分隔值的文本文件。值是整数。我怎么读呢?我想读每一行,然后读下一行 内容如下所示: 2012年12月11日00.00.01 0100 2012年12月11日00.00.05 0140 2012年12月11日00.00.09 0240 2012年12月11日00.00.13 0247 第一列是日期,第二列是第二列,第三列是升 我如何用Java程序实现它 我考虑使用Scanner类。我制作了这个节目:在Java中读取包含3列的写字板文件,java,Java,我想读取包含空格分隔值的文本文件。值是整数。我怎么读呢?我想读每一行,然后读下一行 内容如下所示: 2012年12月11日00.00.01 0100 2012年12月11日00.00.05 0140 2012年12月11日00.00.09 0240 2012年12月11日00.00.13 0247 第一列是日期,第二列是第二列,第三列是升 我如何用Java程序实现它 我考虑使用Scanner类。我制作了这个节目: import java.io.File; import java.io.FileN
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ScannerExample {
public static void main(String args[]) throws FileNotFoundException {
File text = new File("C:\Users\Desktop\test\test.txt");
Scanner scnr = new Scanner(text);
int lineNumber = 1;
while(scnr.hasNextLine()){
String line = scnr.nextLine();
System.out.println("line " + lineNumber + " :" + line);
lineNumber++;
}
}
}
但是我没有我想要的结果。
有什么帮助吗?这也可以通过简单的谷歌搜索完成。。首先,您会发现如下代码:
import java.io.*;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.txt");
// Use DataInputStream to read binary NOT text.
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
用一些代码替换System.out.println,以便从中获取所有内容,如:
while ((strLine = br.readLine()) != null) {
int start = 1, pos = 0,lastpos= 1;
String dates, times, values;
if(strLine.indexOf("\"",lastpos) > 0){
pos = strLine.indexOf("\"",lastpos);
dates = strLine.substring(start,pos);
lastpos = pos+3;//replace through search for \"
pos = strLine.indexOf("\"",lastpos);
times = strLine.substring(lastpos,pos);
lastpos = pos+2;//replace through search for \"
values = strLine.substring(lastpos,strLine.length());
System.out.println(dates);//Convert to Date
System.out.println(times);//Convert to time
System.out.println(values);//Convert to Integer.valueOf/)
System.out.println("next line");
}
}
Java中最简单的方法是使用ApacheCommons库。 如果使用maven,只需在pom文件中添加以下依赖项
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
数组列将包含您的数据
DateFormat format = new SimpleDateFormat("dd/mm/yyyy");
//column 1
Date date = format.format(column[0]);
//column 2
//I'm not sure to get your second column, is it just second or can it be more?
//column 3
double litres = Double.parseDouble(column[2]);
这看起来像一个CSV文件。你可以用一本书来读
我知道CSV的意思是“逗号分隔值”,您的文件没有逗号,但CSV工具也可以读取以空格分隔的文件。google java文件IO如何分隔每个值?我知道第一个是日期,第二个是秒,第三个是升。我不应该从一开始就申报吗?
for(String line : lines){
String[] columns = line.split(" ") //because your columns seem to be separated by a white space
}
DateFormat format = new SimpleDateFormat("dd/mm/yyyy");
//column 1
Date date = format.format(column[0]);
//column 2
//I'm not sure to get your second column, is it just second or can it be more?
//column 3
double litres = Double.parseDouble(column[2]);