import java.io.BufferedInputStream;
public class testNouhinTool {
/**
* @param args
*/
public static void main(String[] args) {
String ORG_DIRECTORY = "F:\\testNouhiuTool";
String ORG_PATH = "F:\\";
String DEST_PATH = "E:\\";
String READ_FILE = "F:\\test.txt";
// Create Directory
createDirectory(ORG_DIRECTORY, ORG_PATH, DEST_PATH);
// Copy File
copyAllFileInOrgdirectory(ORG_DIRECTORY, ORG_PATH, DEST_PATH, READ_FILE);
}
private static void copyAllFileInOrgdirectory(String ORG_DIRECTORY,
String ORG_PATH, String DEST_PATH, String READ_FILE) {
String testcase = "";
String testcaseFolder = "";
boolean fileFoundFlg = false;
boolean testcaseFoundFlg = false;
boolean folderFoundFlg = false;
try {
BufferedReader bf = new BufferedReader(new FileReader(READ_FILE));
String line = bf.readLine();
while(line != null) {
// System.out.println(line + " is in file");
if (line.indexOf("\\") != -1) {
line = line.substring(line.lastIndexOf("\\")+1);
} else if (line.indexOf("/") != -1) {
line = line.substring(line.lastIndexOf("/")+1);
}
if (isRule(line)) {
testcase = line.substring(0, line.indexOf(".")).concat("Test.java");
testcaseFolder = line.substring(0, line.indexOf("."));
}
File findParentPath = new File(ORG_DIRECTORY);
Stack<File> findByStack = new Stack<File>();
findByStack.add(findParentPath);
while(!findByStack.isEmpty()){
File tmpFilePath = findByStack.pop();
File[] tmpFileNameArray = tmpFilePath.listFiles();
for(File tmpFileName : tmpFileNameArray){
if(tmpFileName.isDirectory()){
findByStack.push(tmpFileName);
if (!"".equals(testcaseFolder) && tmpFileName.getName().equals(testcaseFolder)) {
// System.out.println(tmpFileName.getName() + " is data directory");
Stack<File> dataStack = new Stack<File>();
dataStack.add(tmpFileName);
while (!dataStack.isEmpty()) {
File dataFolderPath = dataStack.pop();
File[] dataFiles = dataFolderPath.listFiles();
for (File dataFile : dataFiles) {
if (dataFile.isDirectory()) {
dataStack.push(dataFile);
} else if (dataFile.isFile()) {
copyFile(ORG_PATH, DEST_PATH, dataFile);
}
}
}
folderFoundFlg = true;
}
} else if (tmpFileName.isFile()){
// find rulefile
if(tmpFileName.getName().equals(line)){
copyFile(ORG_PATH, DEST_PATH, tmpFileName);
fileFoundFlg = true;
}
// find testcase
if(tmpFileName.getName().equals(testcase)){
copyFile(ORG_PATH, DEST_PATH, tmpFileName);
testcaseFoundFlg = true;
}
}
}
}
if (isRule(line)) {
if (!testcaseFoundFlg) {
System.out.println(testcase + " is not in directory");
}
if (!folderFoundFlg) {
System.out.println(testcaseFolder + " data folder is not in directory");
}
testcase = "";
testcaseFolder = "";
}
if (!fileFoundFlg) {
System.out.println(line + " is not in directory");
}
fileFoundFlg = false;
testcaseFoundFlg = false;
folderFoundFlg = false;
line = bf.readLine();
}
bf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void createDirectory(String ORG_DIRECTORY, String ORG_PATH,
String DEST_PATH) {
File path = new File(ORG_DIRECTORY);
Stack<File> tmpStack = new Stack<File>();
tmpStack.add(path);
while(!tmpStack.isEmpty()){
File tmpStackFile = tmpStack.pop();
if(tmpStackFile.isDirectory()){
// System.out.println(tmpFile.getAbsolutePath());
if(!new File(tmpStackFile.getAbsolutePath().replace(ORG_PATH, DEST_PATH)).exists()){
new File(tmpStackFile.getAbsolutePath().replace(ORG_PATH, DEST_PATH)).mkdirs();
}
}
File[] files = tmpStackFile.listFiles();
for (File tmpFile : files ) {
if(tmpFile.isDirectory()){
// System.out.println(files[i].getAbsolutePath());
tmpStack.push(tmpFile);
}
}
}
}
private static void copyFile(String ORG_PATH, String DEST_PATH,
File tmpFileName) throws FileNotFoundException, IOException {
// System.out.println(tmpFileName.getName() + " is in directory");
// tmpFileName.renameTo(
// new File(tmpFileName.getAbsolutePath().replace("F:", "E:\\testNouhiuTool")
BufferedInputStream bin = new BufferedInputStream(
new FileInputStream(tmpFileName));
BufferedOutputStream bout = new BufferedOutputStream(
new FileOutputStream(
new File(tmpFileName.getAbsolutePath().replace(ORG_PATH, DEST_PATH))));
int b = 0;
while ((b = bin.read()) != -1) {
bout.write(b);
}
bout.flush();
bin.close();
bout.close();
}
private static boolean isRule(String line) {
return (line.indexOf("CHRCAL") != -1 || line.indexOf("CHRCHK") != -1 || line.indexOf("CHRSEL") != -1) &&
line.indexOf("IF") == -1 && line.indexOf("Cib") == -1;
}
}