Java IO创建/附加文件

Java IO创建/附加文件,java,file,file-io,Java,File,File Io,我试图创建一个文件,如果它不存在,如果它确实存在附加到它。 这是最好的方法吗?我不确定在一种方法中有两次尝试捕捉对个人是好的 public static void main(String [] args) { String fileLocation = "/temp/"; String name = "Bob"; String timeStamp = "1988-03-15"; Path path = Paths.ge

我试图创建一个文件,如果它不存在,如果它确实存在附加到它。 这是最好的方法吗?我不确定在一种方法中有两次尝试捕捉对个人是好的

   public static void main(String [] args)
    {
        String fileLocation = "/temp/";
        String name = "Bob";
        String timeStamp = "1988-03-15";
        Path path = Paths.get(fileLocation+ "info.log");

        if(!Files.exists(path)){
            try {
                Files.createFile(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
            SimpleDateFormat tTimeFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
            writer.write(tTimeFormatter.format(System.currentTimeMillis()) + " name: " + name + " Timestamp: "+ timeStamp);
            writer.newLine();
        } catch (IOException e) {
          System.out.print(e.getStackTrace());
        }
    }

您可以使用StandardOpenOptions(创建和附加)写入文件

Files.write(Paths.get(""), new byte[] {}, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
创建-意味着如果文件不存在,它将创建一个新文件,否则将获得现有文件。 追加-表示将新数据追加到文件中的现有内容


因此,您可以用一行代码完成所有操作。

您可以使用标准OpenOptions:CREATE和APPEND写入文件

Files.write(Paths.get(""), new byte[] {}, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
创建-意味着如果文件不存在,它将创建一个新文件,否则将获得现有文件。 追加-表示将新数据追加到文件中的现有内容


因此,您可以用一行完成所有操作。

尝试使用如下printWriter类

java.io.PrintWriter p = new java.io.PrintWriter(yourFileHere);
// You can use println to print a new line if it allready exists
p.println(yourTextHere);
// Or append to the end of the file
p.append("TEXT HERE!!!!")

尝试像这样使用printWriter类

java.io.PrintWriter p = new java.io.PrintWriter(yourFileHere);
// You can use println to print a new line if it allready exists
p.println(yourTextHere);
// Or append to the end of the file
p.append("TEXT HERE!!!!")
当且仅当具有此名称的文件尚不存在时,createNewFile()方法才会创建一个以此抽象路径名命名的新的空文件。如果文件创建成功,则此方法返回true值;如果文件已存在或操作失败,则此方法返回false值

if (myFile.createNewFile()){
    System.out.println("File is created!");
   }else{
    System.out.println("File already exists.");
   }
当且仅当具有此名称的文件尚不存在时,createNewFile()方法才会创建一个以此抽象路径名命名的新的空文件。如果文件创建成功,则此方法返回true值;如果文件已存在或操作失败,则此方法返回false值

if (myFile.createNewFile()){
    System.out.println("File is created!");
   }else{
    System.out.println("File already exists.");
   }

这里的问题是什么?检查文件是否存在的最佳方法是什么?如果不存在,请创建文件,如果存在,请附加到文件。这里有一个逻辑问题,如果文件不存在且创建失败,它仍将尝试写入文件,因此导致了另一个异常。非常感谢-尽管我认为@eg04lt3r解决方案现在可以防止这种情况发生。这里的问题是什么?检查文件是否存在的最佳方法是什么,如果不存在,创建文件,如果存在,追加到文件。如果文件不存在,并且创建失败,那么这里有一个逻辑问题,它仍然会尝试写入文件,从而导致另一个异常。非常感谢-尽管我认为@eg04lt3r解决方案现在可以防止这种情况发生。哦,太棒了-我不知道我可以放入多个选项,创建和附加一起工作。是的,最好使用Files类来处理文件。另外,如果我想要一个新行id,则需要执行Files.write(路径,“新行”,选项);是的,你可以检查文件类的其他“写入”方法,如果你需要的话可以使用它们。哦,太棒了-我不知道我可以放入多个选项,创建和附加在一起都有效。是的,最好使用文件类来处理文件。如果我想要一个新行id,需要单独编写文件。写入(路径,“新行”,选项);是的,您可以检查类文件的其他“写入”方法,并在需要时使用它们。