Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 写入CSV文件,can';不要写到第一行_Java_Csv_Bufferedwriter - Fatal编程技术网

Java 写入CSV文件,can';不要写到第一行

Java 写入CSV文件,can';不要写到第一行,java,csv,bufferedwriter,Java,Csv,Bufferedwriter,我的意思是基本上我的问题是我不能写入空csv文件的第一行。我可以给第二个人写信。也就是说,如果我自己手动输入了第一行,我可以写入第二行 任何帮助都会得到感谢,真的,干杯。我被难住了 更新: try{ Writer rwrite = new BufferedWriter(new FileWriter(file, true)); rwrite.append(reservation.toString() + "\n"); rwrite.close(); }

我的意思是基本上我的问题是我不能写入空csv文件的第一行。我可以给第二个人写信。也就是说,如果我自己手动输入了第一行,我可以写入第二行

任何帮助都会得到感谢,真的,干杯。我被难住了

更新:

try{
      Writer rwrite = new BufferedWriter(new FileWriter(file, true));
      rwrite.append(reservation.toString() + "\n");
      rwrite.close();
    }
    catch(IOException e){
      System.out.println("Could not open BufferedWriter.");
      e.printStackTrace();
    }
我把我的代码改成了这个,这个很管用,但在我看来有点怪异哈哈。无论如何,感谢您的帮助,我们仍将继续阅读。

试试这个:-

try{

     File file = new File("reservations.csv");
     Scanner fileIn = new Scanner(file);

    try {
        if(fileIn.hasNext()){
        PrintWriter printWriter = new PrintWriter(new FileWriter("reservations.csv", true));
        printWriter.write(reservation.toString() + "\n");
        printWriter.close();
        }

        if(!fileIn.hasNext()){


            PrintWriter printWriter = new PrintWriter(new FileWriter("reservations.csv"));
            printWriter.write(reservation.toString() + "\n");
            printWriter.close();
        } 
        } catch (IOException e) {
            System.out.println("Could not open BufferedWriter.");
            e.printStackTrace();
        }


    }
    catch(IOException e){
     System.out.println("Error: failed to read from: " + "reservations.csv");
     e.printStackTrace();
    }
试试这个:-

try{

     File file = new File("reservations.csv");
     Scanner fileIn = new Scanner(file);

    try {
        if(fileIn.hasNext()){
        PrintWriter printWriter = new PrintWriter(new FileWriter("reservations.csv", true));
        printWriter.write(reservation.toString() + "\n");
        printWriter.close();
        }

        if(!fileIn.hasNext()){


            PrintWriter printWriter = new PrintWriter(new FileWriter("reservations.csv"));
            printWriter.write(reservation.toString() + "\n");
            printWriter.close();
        } 
        } catch (IOException e) {
            System.out.println("Could not open BufferedWriter.");
            e.printStackTrace();
        }


    }
    catch(IOException e){
     System.out.println("Error: failed to read from: " + "reservations.csv");
     e.printStackTrace();
    }

尝试新的FileWriter(文件),但我不需要将其添加为true吗?我会试试的,谢谢。这会打印到第一行,但其他所有内容都会覆盖第一行。但你的代码在我的机器上运行正常。你犯了什么错误?你应该试着发一封邮件。在您发布的代码中,什么是
保留
?请尝试使用新的FileWriter(文件),但我不需要将其添加为true吗?我会试试的,谢谢。这会打印到第一行,但其他所有内容都会覆盖第一行。但你的代码在我的机器上运行正常。你犯了什么错误?你应该试着发一封邮件。在您发布的代码中,什么是
预订
?没有第二行。我快发疯了,哈哈。无论如何,谢谢buddy.hmm,但是,同样的代码写入csv的每一行,我在我的一个应用程序中广泛使用它。可能是别的原因,没有第二条线路。我快发疯了,哈哈。无论如何,谢谢buddy.hmm,但是,同样的代码写入csv的每一行,我在我的一个应用程序中广泛使用它。那可能是别的原因