如何使用數字創建一個新文件(如果創建了一個新增文件)。現在我有一個文件作爲「29-03-2013.pdf」在direcoty。如果再次創建,我想創建一個名稱爲「29-03-2013(1).pdf」的新文件等等。如何在同一個目錄下創建一個像existsFileName(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;
}
是的,我用while循環修改你的代碼以滿足我的需求。它工作正常。 – Ramprasad