2017-09-05 1 views
0

기존 파일을 같은 이름으로 덮어 쓰지 않고 파일을 * .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)로 출력을 제공합니다 그렇지 않은 경우 존재한다는 것입니다. Ifif(!exists)으로 조건을 입력하면 루프의 조건이 처음에만 작동합니다. 제발 도와주세요.

답변

0

파일 존재 여부를 확인하는 상태에서 아무런 조치도 취하지 않고 있습니까? 이름에 아무 것도 추가하지 않으므로 파일이있는 경우에는 동일한 이름 만 저장하려고 시도합니다. 대신 당신이 존재하고 루프 부분에 대한 전에서 사실 확인되어, 변수 존 재 확인, 당신은 체크 실수를하고있는 파일의 존재

1

을 발견하면 추가 해보세요.

다음과 같이 변경해보십시오.

boolean exists1 = tmpDir1.exists(); 
    if(exists1)// change here exists to exists1 
    { 
      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; 
    } 
+0

내 남자 친구 .. 내 일 –

+0

@ Mr.Hunt Congrats를 저장 하셨다면 대답을 승인으로 표시해야합니다. –

관련 문제