如何使用数字创建新文件(如果创建新文件,则增加)。现在我有一个名为“29-03-2013.pdf”的文件。如果再次创建,我想创建一个名为“29-03-2013(1).pdf”的新文件,依此类推。
我的代码:
String finalResponse="";
String pdfnameDate=selectedFromDate+"_"+selectedToDate;
Document doc = new Document(PageSize.A4.rotate(), 50, 10, 10, 10);
try {
File alertsFiledir = new File(Environment.getExternalStorageDirectory(), "/RAMAPP/ALERTS");
if (!alertsFiledir.exists()) {
if (!alertsFiledir.mkdirs()) {
finalResponse="failed";
Log.i("alertsavepdf","directory not created");
return finalResponse;
}
}
File alertsFile = new File(alertsFiledir,pdfnameDate+".pdf");
if (!alertsFile.exists()) {
try {
boolean fileExist = alertsFile.createNewFile();
if(!fileExist)
{
finalResponse="failed";
return finalResponse;
}
} catch (IOException e) {
e.printStackTrace();
finalResponse="failed";
return finalResponse;
}
}
else
{
**//here how to Create a new File like pdfnameDate(1) or pdfnameDate(2).Here pdfnameDate is exist file name**
}
PdfWriter.getInstance(doc, new FileOutputStream(alertsFile));
doc.open();
Log.i("alertsavepdf","pdf file created");
} catch (FileNotFoundException e) {
e.printStackTrace();
finalResponse="failed";
Log.i("alertsavepdf","savepdf error->"+e.toString());
return finalResponse;
} catch (DocumentException e) {
e.printStackTrace();
finalResponse="failed";
Log.i("alertsavepdf","savepdf error->"+e.toString());
return finalResponse;
}