简体   繁体   English

PrintWriter 仅打印文本文件中的第一行

[英]PrintWriter is printing only the first line in text file

The closing of PrintWriter is placed in the finally block, so logically it should write all the existing line in the text file as the code below suggests, 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;
}

But what it does now is that it prints just the first line, the line after it are not printed upon execution, so what is the problem here?但是现在它只打印第一行,之后的行在执行时没有打印,那么这里有什么问题呢? It was working fine yesterday, but all of the sudden this issue started to appear.昨天还好好的,突然就开始出现这个问题了。

Content in text file:文本文件中的内容:

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

Testing your code with your file worked for me.用你的文件测试你的代码对我有用。 But the error that you receive signals that you are not reading an integer with scanner.nextInt() .但是您收到的错误信号表明您没有使用 scanner.nextInt scanner.nextInt()读取 integer 。

I would test your full file with this snippet below.我会用下面的这段代码来测试你的完整文件。 Just to check where does the parsing fail.只是为了检查解析失败的地方。 And fix that problem.并解决这个问题。

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");
    
}

And you could always use Scanner functions such as .hasNextInt() .而且您始终可以使用Scanner函数,例如.hasNextInt()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM