解析缓冲读取器数据,在某些字符处停止



我是Java的新手,仍然在做各种各样的事情。 我正在创建的读/写包有问题。 我有写下来(文本和随机访问文件类型(,但我的问题来自使用 bufferedReader 收集我的文本文件的输入,停止在",",然后获取该信息并将其解析为适当的数据格式,然后再将其发送到我拥有的记录方法。

我的格式是这样的:文本.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();
}
}
}

**已编辑以显示整个班级

public Record read() throws IOException {
String line = in.readLine();
if (line == null) {
throw new IOException(); //maybe return null?
}
String[] values = line.split(",");
String name = values[0];
int age = Integer.parseInt(values[1]);
float salary = Float.parseFloat(values[2]);
return new Record(name, age, salary);
}

但也许问题不在这里。我看不到你在哪里初始化了你的缓冲阅读器。也许描述一下帮助我们的过程。

最新更新