2016-06-21 12 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); 
    } 
    } 
    } 

現時点ではこれは、名前にPを追加.pdfファイルとV値のフォルダをチェックしている8文字までの文字列をトリミングして、新しいフォルダにファイルを移動するが、私は移動するためにそれらを望むと、変数pの中にあるプロジェクト番号に基づいて新しいディレクトリを作成します。

ご協力いただければ幸いです。

+0

を誤解を減らすために、コードの書式設定を開始してください。また、欠落している変数やメソッドを追加するか、メソッドが大きすぎる場合は説明を追加します。 'srcFile'、' dest'などとは何か、そして 'copyFolder(srcFile、destFile);は何をするのですか? – Thomas

+0

srcFileは、アイテムがコピーされるソースファイルを宣言します。destは、これらのアイテムがコピーされる場所のコピー先ファイルです。コピーフォルダは、クラスの名前です。移動目的ですが、私のプログラム全体のソースコードを貼り付けたいのですが。 –

+0

あなたの投稿に追加してください。質問とコメントの情報を組み合わせるだけで、難しくなります。 – Thomas

答えて

0

あなたは次のことを行うことができFile destDirフォルダにファイルFile srcFileをコピーしたいと仮定すると:

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

2番目の例は、私がやろうとしているようなものです。destに含まれているものをすべて取得しようとしています。変数pの内部に格納されているプロジェクト番号をディレクトリに追加/追加して、このディレクトリ内のフォルダ –

+0

これを動作させることはできません:s –

+0

@DavidThomlinsonあなたは_might_より多くの情報を提供することを検討してくださいどのように試したのか、何が起こったのか(例:どのようにしてうまくいかなかったのか)あなたの質問を更新し、コードの関連部分を追加できますか?そして、それが間違って始まるところを見るためにデバッグしましたか? – Thomas

関連する問題