0

コンソールに書き込んだディレクトリとサブディレクトリから別のディレクトリに写真をコピーするにはどうすればよいですか。

コンソールからディレクトリを取得する際に問題があります。

このコードは機能しますが、変更すると

private File[] images = new File("C:/Users/Public/Pictures/Sample Pictures"+"/").listFiles() 

new File(path+"/").listFiles() 

動いていない。

public class Copy {
    private String path;
    private File[] images = new File("C:/Users/Public/Pictures/Sample Pictures"
            + "/").listFiles();

    private Copy() throws IOException {
        getPath();
        finder();
    }

    public static void main(String[] args) throws IOException {
        Copy copy = new Copy();
    }

    private void getPath() {
        System.out.print("Enter directory: ");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        try {
            path = br.readLine();
        } catch (IOException ioe) {
            System.out.println("IO error trying to read your name!");
            System.exit(1);
        }
    }

    private void finder() throws IOException {
        System.out.println("" + path);
        for (File f : images) {

            if (f.isDirectory()) {
                File[] nextimages = new File(
                        "C:/Users/Public/Pictures/Sample Pictures" + "/"
                                + f.getName()).listFiles();
                for (File z : nextimages) {
                    System.out.println("Processing: " + z.getName() + "...");
                    if (z.isHidden()) {
                        System.out.println("Skipping, file is hidden...");
                        continue;
                    }
                    process(z);
                }
                continue;
            }

            if (f.isHidden()) {
                System.out.println("Skipping, file is hidden...");
                continue;
            }
            process(f);

        }
    }

    private void process(File file) throws IOException {
        BufferedImage image = ImageIO.read(file);
        saveThumbnail(file, image);
    }

    private void saveThumbnail(File originalFile, BufferedImage thumbnail)
            throws IOException {
        String filename = originalFile.getName();
        String fileExt = filename.substring(filename.lastIndexOf('.') + 1);
        ImageIO.write(thumbnail, fileExt, new File("D:/Stahovanie/Zadanie/"
                + filename));
    }
}
4

1 に答える 1

0

あなたが持っている

private String path;
private File[] images = new File("C:/Users/Public/Pictures/Sample Pictures"+"/").listFiles();

ですのではじめpathnullimagesここで、コンストラクターのコードの前にフィールドが初期化されることを知る必要があります。

new File(path+"/").listFiles();

に変更するのと同じになります。

new File(null+"/").listFiles();

だからそれは生み出す

new File("null/").listFiles();

その問題を解決するには、呼び出してみてください

images = new File(path+"/").listFiles();

いつpath正しい値に設定されるので、おそらくgetPathメソッドの後です。

于 2013-07-07T13:09:01.783 に答える