2017-08-08 23 views
2

您好,我目前正在編寫一種Java方法,我試圖創建新文件,但我需要這些文件不是相同的名稱,而是增加名稱值,如下所示:如何在新文件名中增加數字?

/Users /本人/桌面/ myFile0.xml

/Users/Myself/Desktop/myFile1.xml

/Users/Myself/Desktop/myFile2.xml

/Users/Myself/Desktop/myFile3.xml

所以我有t在我的代碼中執行以下操作,但我不明白爲什麼當我在每個循環內調用文件(創建一個新循環)時,編號不會增加?

public void pickFolder() throws Exception { 

    chooserFolder.setDialogTitle("Specify your save location"); 
    chooserFolder.setDialogType(JFileChooser.SAVE_DIALOG); 

    int numbers = 0; 
    chooserFolder.setSelectedFile(new File("myFile" + numbers++ + ".xml")); 
    chooserFolder.setFileFilter(new FileNameExtensionFilter("xml file", "xml")); 

    int userSelection = chooserFolder.showSaveDialog(null); 
    if (userSelection == JFileChooser.APPROVE_OPTION) { 

     for (File file : files) { 

      chooserFolder.setSelectedFile(new File(chooserFolder.getSelectedFile().getAbsolutePath())); 


      fileToSave = chooserFolder.getSelectedFile(); 
      if (fileToSave.createNewFile()) { 
       System.out.println("File is created!"); 
       fileToSave = chooserFolder.getSelectedFile(); 

      } else { 
       JOptionPane.showMessageDialog(null, "File already exists."); 
      } 

      System.out.println("Save as file: " + fileToSave.getAbsolutePath()); 

     } 

任何幫助將不勝感激,謝謝!

+2

一個更好的例子嗎? – ByeBye

+0

你寫了一個不在你發佈的代碼中的for循環。請擴展你的例子。 –

+0

我不明白,是for循環內的所有代碼?然後你應該在該循環之外聲明變量號。 – Johan

回答

1

我在你的代碼中看到的是你在增加數字之前將數字設置爲零。如果有任何問題,請嘗試將int numbers=0移出您的循環! (你沒有在代碼中寫任何循環)。當然,提供更多信息將會有所幫助。

0

你的for-loop沒有可以增加的計數器,因爲它是一個for-each-loop(如果這是你的意思是循環)。您也只能撥打chooserFolder.setSelectedFile(new File("myFile" + numbers++ + ".xml"));一次,並且只有numbers++發生。爲了給出一個適當的解決方案,你需要提供所有的代碼。 chooserFolder.setSelectedFile(new File(chooserFolder.getSelectedFile().getAbsolutePath()));也沒有任何意義。一旦你把所有的代碼,我們可以提供一個解決方案

0

你在哪裏,你增加循環請使用時間戳解決這個問題

String fileName = new SimpleDateFormat("yyyyMMddHHmm'.txt'").format(new Date()); 

這裏有以下

package com.seleniummaster.examplefile; 

import java.io.File; 
import java.io.IOException; 
import java.net.InetAddress; 
import java.net.UnknownHostException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

public class CreateFileWithTimeStamp { 

    public static void main(String[] args) 
    { 
     CreateFileWithTimeStamp("test"); 
    } 

    //Create a new file 
    public static void CreateFileWithTimeStamp(String filename) { 
     //get current project path 
     String filePath = System.getProperty("user.dir"); 
     //create a new file with Time Stamp 
     File file = new File(filePath + "\\" + filename+GetCurrentTimeStamp().replace(":","_").replace(".","_")+".txt"); 

     try { 
      if (!file.exists()) { 
       file.createNewFile(); 
       System.out.println("File is created; file name is " + file.getName()); 
      } else { 
       System.out.println("File already exist"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
     // Get current system time 
    public static String GetCurrentTimeStamp() { 
     SimpleDateFormat sdfDate = new SimpleDateFormat(
       "yyyy-MM-dd HH:mm:ss.SSS");// dd/MM/yyyy 
     Date now = new Date(); 
     String strDate = sdfDate.format(now); 
     return strDate; 
    } 
    // Get Current Host Name 
    public static String GetCurrentTestHostName() throws UnknownHostException { 
     InetAddress localMachine = InetAddress.getLocalHost(); 
     String hostName = localMachine.getHostName(); 
     return hostName; 
    } 

    // Get Current User Name 
    public static String GetCurrentTestUserName() { 
     return System.getProperty("user.name"); 
    } 


    } 
相關問題