如何创建多个java txt文件而不覆盖以前的数据?
可能重复:如何创建多个java txt文件而不覆盖以前的数据?,java,Java,可能重复: 嗨,我正在创建一个程序,它执行数据并打印到txt文件中。我遇到的问题是,每次运行该程序都会覆盖我以前的txt文件。我不希望它覆盖,也不希望附加数据。我想创建一个新的txt文件,每次有它产生的日期或时间创建。有人能帮我吗 这是我的密码: private static PrintWriter outFile; /** * @param args */ //Main Method public static void main(String[
嗨,我正在创建一个程序,它执行数据并打印到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();
}
}
私有静态PrintWriter输出文件;
/**
*@param args
*/
//主要方法
公共静态void main(字符串[]args)引发IOException
{
//创建要保存的新文件
outFile=newprintWriter(newfilewriter(“trilogy.txt”);
//创建生成器对象以创建随机数
Random gen=新的Random();
//创建扫描仪对象以扫描用户输入。
扫描仪扫描=新扫描仪(System.in);
DateFormat DateFormat=新的简化格式(“yyyy/MM/dd”);
//使用日期()获取当前日期时间
日期=新日期();
outFile.println(dateFormat.format(date));
outFile.println();
//提示用户输入他/她希望生成的锁组合数。
System.out.print(“输入数量:”);
int数量=scan.nextInt();
整数计数=1;
//循环-仅使用数字1-5
//每个锁组合只生成五个数字
对于(int i=0;i
您需要更改以下行:
outFile = new PrintWriter(new FileWriter("trilogy.txt"));
指向上面有时间/日期的对象:
outFile = new PrintWriter(new FileWriter("trilogy" + DATE_TIME + ".txt"));
尝试更改此选项:
outFile = new PrintWriter(new FileWriter("trilogy.txt"));
要使其附加文本,请执行以下操作:
outFile = new PrintWriter(new FileWriter("trilogy.txt",true));
:)。如果第二个参数为真,那么字节将被写入文件的末尾而不是开头。故事的寓意:总是,总是读取方法的specsHI Angel Olle Blazquez感谢您的反馈。我试图避免它在文件末尾追加和输出。我希望它在用户每次运行程序时创建一个新的txt文件,而不是重写。请尝试以下操作:
outFile=new PrintWriter(new FileWriter(“trilogy”+new Date()+“.txt”)代码>和注释