2016-06-21 43 views
0

你好我試圖追加一些變量到一個目錄來創建一個新的子目錄,我在我目錄中,但我不知道如何做到這一點,我知道.mkdir( )允許我創建一個新目錄,但我不知道如何追加變量,然後創建新目錄,以下是我的嘗試: package movefile;附加字符串到目的地來創建新的目錄

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.FilenameFilter; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

public class MoveFile 
{ 
static String newfile; 


public static void main(String[] args) 
{ 
    File srcFolder = new File("/Users/francis/Desktop/folder1"); 
    File destFolder = new File("/Users/francis/Desktop/folder2"); 
    //make sure source exists 
    if(!srcFolder.exists()){ 

     System.out.println("Directory does not exist."); 
     //just exit 
     System.exit(0); 
    }else{  
     try{ 
     copyFolder (srcFolder,destFolder); 
     }catch(IOException e){ 
     e.printStackTrace(); 
     //error, just exit 
      System.exit(0); 
     } 
    }  
    System.out.println("Done"); 
} 

public static void copyFolder(File src, File dest) 
    throws IOException{ 

     if(src.isDirectory()){ 

     //if directory not exists, create it 
     if(!dest.exists()){ 
      dest.mkdir(); 
      System.out.println("Directory copied from " 
          + src + " to " + dest); 
     } 
     //list all the directory contents 
     String files[] = src.list(); 
      FilenameFilter filter = new FilenameFilter() 
    { 
     @Override public boolean accept(File dir, String name) 
    { 
    return name.endsWith(".pdf"); 
    } 
    }; 
     for (String file : files) { 
      //construct the src and dest file structure 
      File srcFile = new File(src, file); //needed for moving 

       //third attempt 
       File f = null; 
       File f1 = null; 
       String vendor; 
       String orderno; 
       String desc; 
       String v, v1, p; 
       boolean bool = false; 

       try { 
        f = new File("/Users/francis/Desktop/folder1/1234567898.pdf"); 
        f1 = new File("/Users/francis/Desktop/folder2/"); 

        v = f.getName(); 
        v1 = f1.getName(); 

        v = v.length() > 9 ? v.substring(0,8) : v; 

        p = "c3269"; 

        vendor = "VendorName"; 

        v = "file_" + p + " - " + v + ".pdf"; 

        File destFile = new File(dest, v); //get dest and insert (append) project number so it creates new folder 
        copyFolder(srcFile,destFile); 

        File appendProject = new File("/Users/francis/Desktop/folder2/"); 

        boolean successfull = appendProject.mkdir(); 

        if (successfull) 
        { 
         System.out.println("New directory was created:"); 
        } 
        else 
         System.out.println("Failed to create new directory"); 

        bool = f.exists(); 

        if(bool) 
        { 
         System.out.println("File name:" + v); 
        } 
        bool = f1.exists(); 
        if (bool) 
        { 
         System.out.println("Folder name:" + v1); 
        }  
       }catch(Exception e){ 
        e.printStackTrace(); 
       } 



      } 
    }else{ 
     //if file, then copy it 
     InputStream in = new FileInputStream(src); 
      OutputStream out = new FileOutputStream(dest); 

      byte[] buffer = new byte[1024]; 

      int length; 
      //copy the file content in bytes 
      while ((length = in.read(buffer)) > 0){ 
       out.write(buffer, 0, length); 
      } 
      in.close(); 
      out.close(); 
      System.out.println("File copied from " + src + " to " + dest); 
    } 
    } 
    } 

這是檢查.pdf文件附加P和V值的名稱的文件夾的那一刻,修剪串8個字符,將文件移動到一個新的文件夾,但我希望他們能夠被移動和它會根據位於變量p內的項目編號創建新的目錄。

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

+0

請從格式化代碼開始,以減少誤解。如果方法太大,例如添加缺少的變量和方法或添加說明。什麼是'srcFile','dest'等以及什麼是'copyFolder(srcFile,destFile);'意味着什麼? – Thomas

+0

srcFile聲明從其中複製項目的源文件,dest是目標文件,這些項目也將被複制到哪裏,複製文件夾是我的課程的名稱,它是爲了移動目的,但我沒有'我想將我的整個程序源代碼粘貼到 –

+0

將它添加到您的帖子,請不要將問題和評論中的信息結合起來使其更難。 – Thomas

回答

0

假設你要複製的文件File srcFile到文件夾File destDir,你可以做到以下幾點:如果你想先創建一個從文件名的目錄

if(!destDir.exists()) { 
    //create destDir and all missing parent folders 
    destDir.mkdirs(); //For simplicity I'll leave out the success checks, don't forget them in your code 
} 

String destFilename = srcFile.getName(); //adapt according to your needs 
File destFile = new File(destDir, destFilename); 

//You'd need to either implement that or use a library like Apache Commons IO 
copy(srcFile, destFile); 

,做這樣的事情:

String destSubDirName = makeDirName(destFilename); //whatever you need to do here 
File destSubDir = new File(destDir, destSubDirName); 
destSubDir.mkdirs(); //again don't forget the checks 

File destFile = new File (destSubDir, destFilename); 

copy(srcFile, destFile); 
+0

第二個例子看起來有點像我試圖做的事情,我試圖抓住dest中包含的任何東西,並將存儲在變量p中的項目編號插入/追加到目錄中,以便它創建一個新的文件夾在這個目錄 –

+0

不能讓這個仍然工作:s –

+0

@DavidThomlinson你可以考慮提供更多的信息,例如你如何嘗試以及發生了什麼(例如,它以什麼方式不起作用)。你能否更新你的問題並添加你的代碼的相關部分?你是否調試過,看看哪裏出了問題? – Thomas

相關問題