【问题标题】:parsing bufferedReader data, stopping at certain characters解析 bufferedReader 数据,在某些字符处停止
【发布时间】: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


    【解决方案1】:
    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);
    }
    

    但也许问题不在这里。我看不到您在哪里初始化了 bufferedReader。或许描述一下对我们有帮助的过程。

    【讨论】:

    • 我可以填写更多。
    • 我使用 write 方法创建了一个文本文件(创建了一个 printWriter 文件 .txt,它可以工作),我需要在 read 方法中打开并作为记录返回。目前我已经提供了默认变量作为实际工作的占位符。现在,我的阅读只为我提供了空值,我无法使用这些值。我应用了“临时”部分来尝试找出我的文件返回的内容,到目前为止都是空的。
    • "while" 没有括号,所以它只触及 if 条件。因此,目前,您阅读了所有文件并在执行其他任何操作之前检查“if”。在“in.readline()”的最后一次迭代中,没有更多行要读取,因此它返回“null”。这就是为什么你总是得到空值。把这些括号放在上面,就可以了。如果您只想从读取中获取一条记录,请使用 if 而不是 while(也许您使用 while 进行调试)。
    • 只是这样的提示,如果您要再次抛出异常,则无需尝试捕获。只需像您一样将其添加到函数签名中即可。它只是更干净;)
    • 谢谢 GabLeg,虽然直到后来我才看到这个,但实际上我使用了与此非常相似的东西,并且它起作用了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多