기존 파일을 같은 이름으로 덮어 쓰지 않고 파일을 * .xlsx로 저장하려고합니다. 필자는 지정된 파일이있는 경우에만 file (1) .xlsx, file (2) .xlsx와 같은 새로운 파일 이름에 번호 접미사를 추가 할 것을 고려했습니다. 여기에 지금까지 무엇을 시도했다 :같은 이름의 기존 파일을 덮어 쓰지 않고 파일을 저장하는 방법
do {
s = JOptionPane.showInputDialog("Enter the name of the file name");
File tmpDir = new File(System.getProperty("user.home"), "Documents\\Challan_Reports\\" + s + ".xlsx");
boolean exists = tmpDir.exists();
if (exists) {
JOptionPane.showMessageDialog(this, "File Name Already exists try again!");
}
else {
break;
}
} while (true);
if (s.equals("") || s.equals(null)) {
//.........................................................................
File tmpDir = new File(System.getProperty("user.home"), "Documents\\Challan_Reports\\" + gname + " " + date + ".xlsx");
boolean exists = tmpDir.exists();
if (exists) {
JOptionPane.showMessageDialog(this, "exists 1");
for (int m= 1; true;m++)
{
JOptionPane.showMessageDialog(this, "exists m " + m);
File tmpDir1 = new File(System.getProperty("user.home") + "\\Documents\\Challan_Reports\\" + gname + " " + date + " (" + m + ").xlsx");
System.out.println(System.getProperty("user.home") + "\\Documents\\Challan_Reports\\" + gname + " " + date + " (" + m + ").xlsx");
boolean exists1 = tmpDir1.exists();
if (exists) {
System.out.println("exists file");
continue;
}
else {
System.out.println(" not exists");
filename = System.getProperty("user.home") + "\\Documents\\Challan_Reports\\" + gname + " " + date + " (" + m + ").xlsx";
break;
}
}
}
// FileOutputStream out = new FileOutputStream(new File(System.getProperty("user.home"),"Documents\\Challan_Reports\\"+gname+" "+date+".xlsx"));
// workbook.write(out);
// out.close();
}
FileOutputStream out = new FileOutputStream(new File(filename));
workbook.write(out);
out.close();
System.out.println(
"Writesheet.xlsx written successfully");
JOptionPane.showMessageDialog(this, filename + ".xlsx \n\\Generated at path" + System.getProperty("user.home") + "\\Documents\\Challan_Reports");
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
문제는이 ..... 파일 (1) 또는 파일 (2)로 출력을 제공합니다 그렇지 않은 경우 존재한다는 것입니다. If
if(!exists)
으로 조건을 입력하면 루프의 조건이 처음에만 작동합니다. 제발 도와주세요.
내 남자 친구 .. 내 일 –
@ Mr.Hunt Congrats를 저장 하셨다면 대답을 승인으로 표시해야합니다. –