2013-07-23 23 views
6

私はJDK 6を使用しています。ディレクトリからファイルを別のディレクトリにコピーする方法

私はFolder1Folder2の2つのフォルダ名を持っています。

Folder1は、以下のファイル

TherMap.txt 

TherMap1.txt 

TherMap2.txt 

たびFolder2TherMap.txtとして名前を持つ唯一つのファイルを持っています。

私は何をしたい、

コピーfolder1から任意のファイルとTherMap.txt .IFはすでにTherMap.txt、それを削除して貼り付け、Folder2に存在するような名前でFolder2に貼り付けられました。私はそれが

public void FileMoving(String sourceFilePath, String destinationPath, String fileName) throws IOException { 
    File destinationPathObject = new File(destinationPath); 
    File sourceFilePathObject = new File(sourceFilePath); 
    if ((destinationPathObject.isDirectory()) && (sourceFilePathObject.isFile())) 
    //both source and destination paths are available 
    { 
     //creating object for File class 
     File statusFileNameObject = new File(destinationPath + "/" + fileName); 
     if (statusFileNameObject.isFile()) 
     //Already file is exists in Destination path 
     { 
      //deleted File 
      statusFileNameObject.delete(); 
      //paste file from source to Destination path with fileName as value of fileName argument 
      FileUtils.copyFile(sourceFilePathObject, statusFileNameObject); 
     } 
     //File is not exists in Destination path. 
     { 
      //paste file from source to Destination path with fileName as value of fileName argument 
      FileUtils.copyFile(sourceFilePathObject, statusFileNameObject); 
     } 
    } 
} 

を働いていないcode.but私は上をクリックして、それがエラーを示す、FileUtils機能を使用していますが、私はmain()

//ExternalFileExecutionsObject is class object 
ExternalFileExecutionsObject.FileMoving(
      "C:/Documents and Settings/mahesh/Desktop/InputFiles/TMapInput1.txt", 
      "C:/Documents and Settings/mahesh/Desktop/Rods", 
      "TMapInput.txt"); 

で上記の関数を呼び出す、次を書いたため

エラー、次のコードで自動的に新しいパッケージが生成されました。

私のコードは、動作していなくても間違いを示していません。

どうすればこの問題を解決できますか。

おかげ

+4

から[ファイルのコピーの可能性の重複:あなたは7にアップグレードすると、次の

public static void copyFile(File from, File to) throws IOException { Files.copy(from.toPath(), to.toPath()); } 

参照を行うことができるようになります

public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile(); } FileInputStream fIn = null; FileOutputStream fOut = null; FileChannel source = null; FileChannel destination = null; try { fIn = new FileInputStream(sourceFile); source = fIn.getChannel(); fOut = new FileOutputStream(destFile); destination = fOut.getChannel(); long transfered = 0; long bytes = source.size(); while (transfered < bytes) { transfered += destination.transferFrom(source, 0, source.size()); destination.position(transfered); } } finally { if (source != null) { source.close(); } else if (fIn != null) { fIn.close(); } if (destination != null) { destination.close(); } else if (fOut != null) { fOut.close(); } } } 

Javaで1つのディレクトリを別のディレクトリに格納する](http:// stackoverflow。com/questions/1146153/copying-files-from-one-directory-to-java) –

+0

ApacheCommonsへの参照をダウンロードして追加していません。代わりに、あなたはcopyFileスキーマと一致するメソッドスタブをプロジェクトに作成しました。 ApacheCommonsのソリューションに従うつもりならば、ライブラリ全体をダウンロードし、そのライブラリへの参照を追加する必要があります。 – Ted

+0

さらに、作成したcopyFileメソッドスタブを削除する必要があります。そうしないと、あいまいなメソッド呼び出しが発生します。 – Ted

答えて

8

使用Apache CommonsFileUtils FileUtils.copyDirectory(source, desc);

+0

私はJava側に新しいので、あなたは明確に説明することができます。コードを変更する必要がある場所 –

+0

1つのディレクトリから別のディレクトリに1つのファイルをコピーしたい。私はdirectory.andをコピー&ペーストしたくないし、一度にすべてのファイルをコピーアンドペーストしたくない。 –

+0

@All誰かがこの回答を理解してくれたら、教えてください。私は 'JRE 6 'を使用しています。 –

1

は、あなたのコードは、あなたがここで見つけるApacheCommonsライブラリをダウンロードする必要がありますApacheCommonsソリューションを使用するためにために動作していない:

http://commons.apache.org/

とそれへの参照を追加します。

JRE 6を使用しているので、すべてのNIOファイルユーティリティを使用することはできません。また、フォーラムの投稿に簡単に回答できるApache Commonsを愛する人もいますが、そのユーティリティをちょうど追加する必要はありません。一つの機能を得る。 ApacheCommonsを使用せずにtransferFromメソッドを使用するこのコードを使用することもできます。

https://gist.github.com/mrenouf/889747

Standard concise way to copy a file in Java?

関連する問題