简体   繁体   English

如何在Java NetBeans中创建新的“ .txt”文件而不覆盖以前保存的文件?

[英]how can i create a new “.txt” file in java netbeans without overwriting the previous saved file?

how can i create a new ".txt" file in java netbeans without overwriting the previous saved file? 如何在Java NetBeans中创建新的“ .txt”文件而不覆盖以前保存的文件?

here is my code, i used the method of setting a filename because i don't know yet how to create a new .txt file and not to overwrite the previous one 这是我的代码,我使用了设置文件名的方法,因为我还不知道如何创建一个新的.txt文件,并且不覆盖前一个文件

        File file = new File("Basic Student Information.txt");
    try {
        Files.write(Paths.get("Basic Student Information.txt"),list);
        Scanner scan = new Scanner(file);
        //while(scan.hasNext()){
        //    JOptionPane.showMessageDialog(null,scan.nextLine());
        //}
    } catch (IOException ex) {
        Logger.getLogger(StudentInfo.class.getName()).log(Level.SEVERE, null, ex);
    }

This is a HW program I guess, so I should not give you the exact code. 我猜这是一个硬件程序,所以我不应该给您确切的代码。 But what you can do is to check if the file exists, if not then write to it else create a new file. 但是您可以做的是检查文件是否存在,如果不存在,请写入文件,否则创建一个新文件。 Something like this : 像这样的东西:

File f = new File(filePathString);
if(f.exists() && !f.isDirectory()) { 
// do something
} else {
//create a new file and write to it
}

You can check if the file already exists: 您可以检查文件是否已经存在:

File f = new File("name.txt");
if(f.exists() && !f.isDirectory()) { 
   // create file with other name
}else{
   // Use that filename
}

Is something like this that you want? 你想要这样的东西吗?

Instead of hardcoding the file name in the declaration, 与其对声明中的文件名进行硬编码,

File file = new File("Basic Student Information.txt");

try setting the file name as a variable itself 尝试将文件名本身设置为变量

String fileName = "something.txt";
File file = new File(fileName);

And if your file would be overwritten, simply edit the string instead. 如果文件将被覆盖,则只需编辑字符串即可。

if(f.exists() && !f.isDirectory()) 
{ 
   fileName = "somethingelse.txt";
}
else
{
   // Use filename
}
//Name file as normal

If you will be doing this multiple times, you can even set up a for loop that adds the current iteration to the file name in order to dynamically generate new names. 如果您要进行多次,甚至可以设置一个for循环,将当前迭代添加到文件名中,以便动态生成新名称。

Easiest way to create unique file names is to put the date and time in the file name. 创建唯一文件名的最简单方法是将日期和时间放入文件名中。 Like this. 像这样。

Basic Student Information 2016/01/26 11:04:02.txt

And here's one way to do this: 这是执行此操作的一种方法:

package com.ggl.testing;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CreateFileName {

    public static void main(String[] args) {
        CreateFileName createFileName = new CreateFileName();
        String fs = createFileName.createFileName("Basic Student Information",
                "txt");
        System.out.println(fs);
    }

    public String createFileName(String name, String extension) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date currentDate = new Date();
        return name + " " + dateFormat.format(currentDate) + "." + extension;
    }

}

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

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