2016-08-08 4 views
0

私は、特定のフォルダを新しい場所に自動的にコピーするためのJAVAプログラムを作成しています。ループを使用して、指定した各フォルダーのソースとデスティネーションに対して同じ機能を使用します。問題は、関数が最初のフォルダをコピーしてから次のフォルダをコピーするのではなく、新しい場所に複数回コピーすることです。フォルダの場所は文字列配列に保持され、値[i]を変更することによって特定のフォルダが選択されます。関数がループするたびに[i]が増加しますが、ループは[i]の値とコピーする次のフォルダーを選択しません。Javaプログラム、int i(i = 0)のデフォルト値が各ループに1ずつ増加しても使用されています

誰もがこれを手伝ってくれますか、私が働いているコードは以下です。ありがとう。

import java.io.File; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.StandardCopyOption; 

public class Application { 

static String[] saves = { 
     "C:\\Users\\Lucas\\Documents\\My Games\\Halo", 
     "C:\\Users\\Lucas\\Documents\\My Games\\Terraria", 
     "C:\\Users\\Lucas\\Documents\\My Games\\Borderlands 2", 
     "C:\\Users\\Lucas\\Documents\\My Games\\Rocket League" 
}; 

private static int i = 1; 

File source = new File(saves[i]); 

static File folder = new File("Saves\\"); 

File dest = new File(String.valueOf(folder) + "\\" + source.getName()); 

private void Start() throws IOException { 
    MakeDirectory(folder); 
    Copy(); 
} 

private void Copy() throws IOException { 
    copyFileUsingJava7Files(source, dest); 
    Add(); 
} 

private void Add() throws IOException { 
    i++; 
    System.out.println("Value of i = " + i); 
    System.out.println(""); 
} 

private static void copyFileUsingJava7Files(File source, File dest) 
     throws IOException { 

    if (!dest.exists()) { 
     System.out.println("Copying files from: " + "'" + source + "'"); 
     System.out.println(""); 
     copyFolder(source, dest); 

     System.out.println("File copied"); 
    } else { 
     copyFolder(source, dest); 
    } 
} 

private static void copyFolder(File source, File dest) throws IOException { 
    if (source.isDirectory()) { 
     if (!dest.exists()) { 
      dest.mkdir(); 
      System.out.println("Directory created :: " + dest); 
     } 

     String files[] = source.list(); 
     for (String file : files) { 
      File srcFile = new File(source, file); 
      File destFile = new File(dest, file); 

      copyFolder(srcFile, destFile); 
     } 
    } else { 
     if (source.lastModified() > dest.lastModified()) { 
      Files.copy(source.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING); 
      System.out.println("File copied :: " + dest); 
     } else { 
      System.out.println("A newer version exists of: " + "'" + dest + "'"); 
     } 
    } 
} 

private static void MakeDirectory(File folder) { 

    if (!folder.exists()) { 
     System.out.println("Creating directory: " + "'" + folder + "'"); 
     folder.mkdir(); 

     System.out.println("Directory created"); 
    } else { 
     System.out.println("Directory already exists: " + "'" + folder + "'"); 
    } 
} 

public static void main(String[] args) throws IOException { 
    Application app = new Application(); 

    int l; 
    for (l = 0; l < 3; l++) { 
     app.Start(); 
    } 
} 
} 
+2

ちょうどメモ。非大文字の名前メソッド: –

+0

私は、私は実際に、それを行うの習慣に入るべきであることを聞いてきました:ありがとうございました –

+0

私は静的メソッド/変数の必要性を理解しています。 –

答えて

0

あなたは毎回File sourceを再初期化する必要があります。そうでない場合、sourceは変更されません。

1

sourceフィールドを最初に設定した後で変更されているようには見えません。 2番目のファイルに設定していますが、後で変更しません。 sourceがちょうどFileであるため、iをインクリメントしてもsourceは自動的に更新されません。

さらに、i = 1から始まります。 Javaでは、配列のインデックスはゼロです。つまり、配列の最初の項目は実際には0という項目なので、代わりにi = 0で始める必要があります。

0

iは静的変数なので、すべてのオブジェクトは同じ変数を共有します。各app.Start()メソッドの実行中にiをインクリメントしているので、5回呼び出しの最後にその値は5になります。結果として、すべてのsys出力で出力が5になります。それは静的な点です。

関連する問題