【问题标题】:PrintWriter is printing only the first line in text filePrintWriter 仅打印文本文件中的第一行
【发布时间】:2021-11-30 09:14:24
【问题描述】:

PrintWriter 的关闭放在 finally 块中,所以逻辑上它应该按照下面的代码将所有现有行写入文本文件中,

     public boolean updatecentre(String row, int cuid) throws IOException
{
    boolean updated = false;
    int dbcuid;
    String dbcname, dbcentre, dba1, dba2, dba3, vac1, dose1, vac2, dose2, vac3, dose3, dbstr;
    
    File F1 = new File("Centre.txt");
    File Ftc1 = new File("Temp.txt");
    FileWriter fw1 = new FileWriter(Ftc1, true);
    BufferedWriter bw1 = new BufferedWriter(fw1);
    PrintWriter pw1 = new PrintWriter(bw1);
    Scanner Sc1 = new Scanner(F1);
    Sc1.useDelimiter("[/\n]");
    try
    {
        while(Sc1.hasNext())
        {
            dbcuid = Sc1.nextInt();
            dbcname = Sc1.next();
            dba1 = Sc1.next();
            dba2 = Sc1.next();
            dba3 = Sc1.next();
            vac1 = Sc1.next();
            dose1 = Sc1.next();
            vac2 = Sc1.next();
            dose2 = Sc1.next();
            vac3 = Sc1.next();
            dose3 = Sc1.next();
            dbstr = dbcuid+"/"+dbcname+"/"+dba1+"/"+dba2+"/"+dba3+"/"+vac1+"/"+dose1+"/"+vac2+"/"+dose2+"/"+vac3+"/"+dose3;
            if(dbcuid == cuid)
            {
                pw1.print(row +"\n");
                updated = true;
            }
            else
            {
                pw1.print(dbstr+"\n");
            }
        }
    }
    catch(Exception e)
    {
        
    }
    finally
    {
        Sc1.close();
        pw1.flush();
        pw1.close();
        bw1.close();
        fw1.close();
        F1.delete();
        File dump = new File("Centre.txt");
        Ftc1.renameTo(dump);  
    }
    return updated;
}

但是现在它只打印第一行,之后的行在执行时没有打印,那么这里有什么问题呢?昨天还好好的,突然就开​​始出现这个问题了。

文本文件中的内容:

1/Axiata Arena Bukit Jalil/L2-E-10, Enterprise 4,/Technology Park Malaysia, Bukit Jalil,/57000, Kuala Lumpur/Pfizer-BioNTech/100/CoronaVac/50/AstraZeneca/25
2/Titiwangsa Stadium/Jalan Tembeling,/Titiwangsa,/53200, Kuala Lumpur/Pfizer-BioNTech/100/CoronaVac/50/AstraZeneca/25
3/Kompleks Sukan Pandamaran Klang/Padamaran,/Port Klang,/42000, Selangor/Pfizer-BioNTech/100/CoronaVac/50/AstraZeneca/25
4/KPL Ampang Puteri Hospital/1, Jalan Mamanda 9,/Taman Dato Ahmad Razali,/68000 Ampang, Selangor/Pfizer-BioNTech/100/CoronaVac/50/AstraZeneca/25
5/Mines Convention Centre/MIECC, Jalan Dulang,/Mines Wellness City, Seri Kembangan,/43300, Selangor/Pfizer-BioNTech/100/CoronaVac/50/AstraZeneca/25

【问题讨论】:

  • 你是否用调试器检查过你是否多次执行while循环?
  • 尝试打印 catch 块中的任何内容,以确保没有异常:catch (Exception e) { e.printStackTrace(); }
  • 那个分隔符似乎有点可疑。你的文件是什么格式的?
  • @Renis1235 我已经更新了帖子并包含了文本文件。
  • @OussamaZAGHDOUD 它说 java.util.InputMismatchException,似乎dbcuid = Sc1.nextInt(); 行没有从文本文件中获取数字,但每行中的第一个值是数字

标签: java printwriter


【解决方案1】:

用你的文件测试你的代码对我有用。但是您收到的错误信号表明您没有使用scanner.nextInt() 读取整数。

我会用下面的这个 sn-p 测试你的完整文件。只是为了检查解析失败的地方。并解决这个问题。

String dbcuidStr = sc.next();

try
{
    System.out.println("This is the ID: " + dbcuidStr);
    dbcuid = Integer.parseInt(dbcuidStr);
}
catch (Exception e)
{
    System.out.println(dbcuidStr + " could not be parsed to an integer");
    
}

而且您始终可以使用Scanner 函数,例如.hasNextInt()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 2021-09-20
    • 1970-01-01
    相关资源
    最近更新 更多