【问题标题】:Reading and printing a text file with delimiters in Java在 Java 中读取和打印带有分隔符的文本文件
【发布时间】:2019-09-09 01:54:52
【问题描述】:

我正在尝试读取和打印带有 Java 分隔符的文本文件。当我使用该函数时,它只打印第一个对象。 我使用 Apache NetBeans 11.1。这是我的代码:

public void Show() throws FileNotFoundException, IOException{
    FileInputStream fstream=new FileInputStream("usuarios.txt");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String aux;
    String total = "";
    String name="" , addres="";
    String id = "";
    int information=0;
    try{           
    while((aux=br.readLine())!=null){
        for(int i=0;i<aux.length(); i++){
            if(aux.charAt(i)!='&'){
                total+=aux.charAt(i);
            }else{
                information++;
                switch(information){
                    case 1:{
                        id=total;
                        break;
                    }
                    case 2:{
                        name=total;
                        break;
                    }
                    case 3:{
                        addres=total;
                        break;
                    }
                }
                total="";
            }
        }
        System.out.println("Id: " + id + " Name: " + name + " Address: " + addres);
    }
    }catch(IOException ex){

    }
}

在文本文件中我有:

1&John&Address #1&
2&Peter&Address #2&
3&Robert&Address #3&

我的输出:

Id: 1 Name: John Address: Address #1
Id: 1 Name: John Address: Address #1
Id: 1 Name: John Address: Address #1

预期输出:

Id: 1 Name: John Address: Address #1
Id: 2 Name: Peter Address: Address #2
Id: 3 Name: Robert Address: Address #3

【问题讨论】:

  • 你正在吃异常。您的程序可能会引发错误。尝试在 catch 块中打印错误
  • stackoverflow.com/a/17838196/2956272 问题是你为什么会抓到它?
  • 在打开你的while循环后尝试打印aux,看看你是否真的在阅读文件的每一行

标签: java


【解决方案1】:

您忘记在每次 while 迭代后设置 information=0

while((aux=br.readLine())!=null){
    information=0; // You forgot to update 'information' variable
    for(int i=0;i<aux.length(); i++){
    ...

【讨论】:

    【解决方案2】:

    这不是答案,而是对您粘贴的代码的一些一般性建议。

    而不是做:

    FileInputStream fstream = new FileInputStream("usuarios.txt");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    

    你可以这样做。您不需要坚持fstreamin,它们的唯一目的是设置一个新的BufferedReader。此外,您在此处打开的所有内容也应稍后关闭。

    BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream("usuarios.txt"))));
    

    而不是像这样在方法签名中抛出异常:

    public void Show() throws FileNotFoundException, IOException
    

    删除它们,然后在您的代码周围加上try...catch,包括e.printStackTrace() 之类的东西,这样您就可以看到有关问题的信息。一般来说,您应该避免向调用者抛出任何异常,除非可以合理地期望调用者知道该做什么。在这种情况下,您的代码可能会引发调用者可能不知道的文件异常——它只是调用了Show(),然后与文件的流/读取器问题有关的事情就会爆炸。

    public void Show()
    

    将整个内容包装在 try 块中,以便您可以添加 finally 子句来关闭各种输入流和读取器,如下所示。好的做法是在需要时打开它们,并在完成后关闭它们。

    BufferedReader br = null;
    try {
        try {
            br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream("usuarios.txt"))));
            // your code goes here
        } finally {
            if (br != null) {
                br.close();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 2019-04-01
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多