【发布时间】:2018-05-01 16:49:18
【问题描述】:
我是 Java 新手,仍在从事各种工作。我正在创建的读/写包有问题。我有记录(文本和随机访问文件类型),但我的问题来自使用 bufferedReader 来收集我的文本文件的输入,在“,”处停止,然后在发送之前获取该信息并将其解析为适当的数据格式使用我拥有的记录方法。
我的格式是这样的:text.txt 文件 = 姓名、年龄、薪水。在这种情况下,任何数字都可以,但在文件中它是一个用逗号分隔的字符串,例如:"James, 22, 1500.20"
我的方法一团糟,这就是我真正陷入困境的地方
package l2Reader;
import java.io.*;
import l2Record.Record;
public class Text extends Reader {
private BufferedReader in;
/**
* Opens a file of employee records for reading
* @param fileName -- name of file to open
* @throws IOException -- if fileName is null or
* unable to open the file for any reason.
*/
public void open(String fileName) throws IOException{
in = new BufferedReader(new FileReader(fileName));
}
/**
* Reads the next employee data Record.
* @return the Record read.
* @throws IOException if an underlying read command throws an exception or
* the data in the file is not able to be interpreted as a valid employee record.
*/
public Record read() throws IOException {
try {
String temp = "";
while((temp = in.readLine()) != null)
if(temp.trim().length() > 0){
temp =temp + in.readLine();
}
System.out.println(temp);
String name = "nub";
byte age = 0;
float salary = 10;
if (name == null){
throw new IOException();
}
//return a record
return new Record(name, age, salary);
} catch (IOException e) {
throw e;
} catch (Exception e) {
//throw new IOException();
e.printStackTrace();
throw new IOException();
}
}
/**
* @return true if there is no more data to be read, false otherwise.
*/
public boolean eof(){
boolean eof = true;
try{
return ! (in.read() != -1);
}
catch (Exception e) {
return true;
}
}
/**
* Closes the file
* @throws IOException -- if unable to close the file
*/
public void close() throws IOException{
try{
in.close();
}catch (Exception e) {
throw new IOException();
}
}
}
**编辑显示整个班级
【问题讨论】:
标签: java split bufferedreader record