1

私の前の質問 - Java: How to read directory folder, count and display no of files and copy to another folder? へのフォローアップ 、フォルダを読み込んで、フォルダ内のファイル数を数え(ファイルの種類は問わない)、ファイル数を表示してから、新しいフォルダにコピーしたい。ただし、次の例外が発生します。

スレッド「メイン」での例外 java.io.FileNotFoundException: C:\project\curFolder (アクセスが拒否されました) C:\project\newFolder は既に存在します: プロセスを続行します! java.io.FileInputStream.open(Native Method) で java.io.FileInputStream.(FileInputStream.java:120) で filetransfer.FileTransfer.copyFiles(FileTransfer.java:54) で filetransfer.FileTransfer.checkDir(FileTransfer.java: 44) filetransfer.FileTransfer.readDirectory(FileTransfer.java:29) at filetransfer.FileTransfer.main(FileTransfer.java:12) Java 結果: 1 ビルド成功 (合計時間: 0 秒)

私は学生であることを覚えておいてください。これは私がこれまでに行ったことです:

public class FileTransfer {

    public static void main(String[] args) throws FileNotFoundException, IOException {
         readDirectory();
    }

public static void readDirectory() throws FileNotFoundException, IOException {
        //create new file object with location of folder
        File curFolder = new File("C:/project/curFolder/");
        int totalFiles = 0;
        //for loop to count the files in the directory using listfiles method
        for (File file : curFolder.listFiles()) {
            //determine if the file object is a file
            if (file.isFile()) {
                //count files ++
                totalFiles++;
            }
        }
        //display number of files in directory
        System.out.println("Number of files: " + totalFiles);
        checkDir();
    }

    public static void checkDir() throws FileNotFoundException, IOException {
        //check if destination directory exist, if not create directory
        //create new file object with copy folder destination
        File newFolder = new File("C:/project/newFolder/");
        //Check if folder exist: True: Println with message(true and continuing)
        if (newFolder.exists()) {
            System.out.println(newFolder + " already exist: Continuing with process!");
        } else {
            //False: Create Dir
            newFolder.mkdir();
            System.out.println(newFolder + " created!");
        }
        copyFiles();
    }

    public static void copyFiles() throws FileNotFoundException, IOException {
        //copy files from specified directory to new directory
        File fromCur = new File("C:/project/curFolder/");
        File toNew = new File("C:/project/newFolder/");
        FileInputStream from = null;
        FileOutputStream to = null;
        try {
            from = new FileInputStream(fromCur);
            to = new FileOutputStream(toNew);
            byte[] buffer = new byte[4096];
            int bytesRead;

            while ((bytesRead = from.read(buffer)) != -1) {
                to.write(buffer, 0, bytesRead);
            }
        } finally {
            if (from != null) {
                try {
                    from.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
            if (to != null) {
                try {
                    to.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }
}
4

2 に答える 2

0

あなたがやろうとしているのは、フォルダー内のファイルではなく、フォルダー自体をコピーすることです。copyFilesメソッドのこの部分を変更してみてください。

    File fromCur = new File("C:/project/curFolder/");
        File toNew = new File("C:/project/newFolder/");
        FileInputStream from = null;
        FileOutputStream to = null;

    try {

    to = new FileOutputStream(toNew);
    byte[] buffer = new byte[4096];
    int bytesRead;

    for (File fileTemp : fromCur.listFiles()) {
     if (fileTemp.isFile()) {
        from = new FileInputStream(fileTemp);
         while ((bytesRead = from.read(buffer)) != -1) {
          to.write(buffer, 0, bytesRead);
         }
     }
    }

ループ中、これはフォルダー自体ではなく、フォルダー内のファイルを参照します。

于 2012-10-24T10:18:56.453 に答える
0

エラーは次の行にあります。

from = new FileInputStream(fromCur);

C:\project\curFolderファイルではなくフォルダだからです。FileInputStreamドキュメントによると、表示されている例外は正しいものです。

例外: FileNotFoundException - ファイルが存在しない場合、通常のファイルではなくディレクトリである場合、またはその他の理由で読み取り用に開くことができない場合。

(上記の引用での私の強調)

入力ファイルがディレクトリであることを確認してから、ファイルをリストして1つずつコピーすることをお勧めします。

于 2012-10-24T10:19:06.060 に答える