如何在不覆盖之前数据的情况下创建多个 java txt 文件? [复制]
Posted
技术标签:
【中文标题】如何在不覆盖之前数据的情况下创建多个 java txt 文件? [复制]【英文标题】:How to create multiple java txt files without overriding the previous data? [duplicate] 【发布时间】:2012-06-18 20:37:29 【问题描述】:可能重复:Writing to a file without overwriting or appending
您好,我正在创建一个执行数据并打印到 txt 文件的程序。我遇到的问题是每次运行程序时都会覆盖我以前的 txt 文件。我不想让它覆盖,也不想附加数据。我想每次让它产生一个创建日期或时间时创建一个新的 txt 文件。有人可以帮助我吗?
这是我的代码:
private static PrintWriter outFile;
/**
* @param args
*/
//Main Method
public static void main(String[] args) throws IOException
//creates the new file to be saved
outFile = new PrintWriter(new FileWriter("trilogy.txt"));
//Create a generator object to create random numbers
Random gen = new Random ();
//Create a scanner object to scan user input.
Scanner scan = new Scanner(System.in);
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
//get current date time with Date()
Date date = new Date();
outFile.println(dateFormat.format(date));
outFile.println();
//Prompts the user to input how many lock combinations he/she wants generated.
System.out.print ("Enter quantity: ");
int quantity = scan.nextInt();
int count = 1;
//Loop - Only numbers 1 - 5 are used
// Only five numbers are generated per one lock combination
for (int i = 0; i < quantity; i++)
int n1 = gen.nextInt(5)+1;
int n2 = gen.nextInt(5)+1;
int n3 = gen.nextInt(5)+1;
int n4 = gen.nextInt(5)+1;
int n5 = gen.nextInt(5)+1;
//While loop making sure that NO numbers are repeated
while (n2==n1)
n2 = gen.nextInt(5)+1;
while (n3==n2 || n3 == n1 || n3==n4||n3==n5)
n3 = gen.nextInt(5)+1;
while (n4 == n3 || n4 == n2 || n4 == n1||n4==n5)
n4 = gen.nextInt(5)+1;
while (n5== n1 || n5==n2 || n5==n3 || n5==n4)
n5 = gen.nextInt(5)+1;
//If statements that output the random lock combinations in different formats.
if (n1==1)
outFile.println ("(" + count +") " + (n1*10 +n2) +"-"+ (n3*10+n4)+"-"+n5);
else if (n2==2)
outFile.println ("(" + count +") " + n2 + "-" + (n1*10 + n3)+ "-" + (n4*10+ n5));
else if (n3==3)
outFile.println ("(" + count +") " + (n3*10 +n2) +"-"+ n1+ "-" +(n4*10+n5));
else if (n4 == 4)
outFile.println ("(" + count +") " + (n4 +"-"+ (n2*100 +n3*10+n1)+"-"+n5));
else
outFile.println ("(" + count +") " + (n5) +"-"+ (n2) +"-"+ (n3) + "-"+ (n4) +"-" +(n1));
count++;
//Spaces one line in between each lock combination
outFile.println();
outFile.close();
【问题讨论】:
奇怪这个问题是在不到一个小时前被问到的:***.com/questions/11055695/… 这是一个副本,但我更喜欢这个版本而不是原版 【参考方案1】:您需要更改以下行:
outFile = new PrintWriter(new FileWriter("trilogy.txt"));
到有时间/日期的东西:
outFile = new PrintWriter(new FileWriter("trilogy" + DATE_TIME + ".txt"));
【讨论】:
【参考方案2】:尝试改变这个:
outFile = new PrintWriter(new FileWriter("trilogy.txt"));
为此添加文本:
outFile = new PrintWriter(new FileWriter("trilogy.txt",true));
:)
【讨论】:
来自 API:docs.oracle.com/javase/1.4.2/docs/api/java/io/FileWriter.html FileWriter(File file, boolean append) 在给定 File 对象的情况下构造 FileWriter 对象。如果第二个参数为真,那么字节将被写入文件的末尾而不是开头。 故事的寓意:总是,总是阅读方法的规范 HI Angel Olle Blazquez 感谢您的反馈。我试图避免它在文件末尾附加和输出。我希望它在每次用户运行程序而不是覆盖时创建一个新的 txt 文件。 试试这个:outFile = new PrintWriter(new FileWriter("trilogy"+new Date()+".txt"));
和评论以上是关于如何在不覆盖之前数据的情况下创建多个 java txt 文件? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
java - 如何在不创建对象的情况下找出文件和目录的大小?
如何在不创建实际新按钮的情况下创建在不同位置执行相同操作的多个按钮?