ファイル共通アクション


import java.io.*;

public class FileOperate {
	public FileOperate() {
	}

	/**
	 *     
	 * 
	 * @param folderPath
	 *            String   c:/fqf
	 */
	public static void newFolder(String folderPath) {
		try {
			java.io.File myFilePath = new java.io.File(folderPath);
			if (!myFilePath.exists()) {
				myFilePath.mkdir();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 *     
	 * 
	 * @param filePathAndName
	 *            String          c:/fqf.txt
	 * @param fileContent
	 *            String     
	 */
	public static void newFile(String filePath, String fileContent) {
		try {
			File myFilePath = new File(filePath);
			if (!myFilePath.exists()) {
				myFilePath.createNewFile();
			}
			FileWriter resultFile = new FileWriter(myFilePath);
			PrintWriter myFile = new PrintWriter(resultFile);
			String strContent = fileContent;
			myFile.println(strContent);
			resultFile.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 *     
	 * 
	 * @param filePathAndName
	 *            String          c:/fqf.txt
	 */
	public static void delFile(String filePath) {
		try {
			filePath = filePath.toString();
			File myDelFile = new File(filePath);
			myDelFile.delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 *      
	 * 
	 * @param folderPath
	 *            String           c:/fqf
	 */
	public static void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); //          
			java.io.File myFilePath = new java.io.File(folderPath);
			myFilePath.delete(); //       

		} catch (Exception e) {
			System.out.println("         ");
			e.printStackTrace();

		}

	}

	/**
	 *             
	 * 
	 * @param path
	 *            String         c:/fqf
	 */
	public static void delAllFile(String path) {
		File file = new File(path);
		if (!file.exists()) {
			return;
		}
		if (!file.isDirectory()) {
			return;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				delAllFile(path + "/" + tempList[i]);//            
				delFolder(path + "/" + tempList[i]);//        
			}
		}
	}

	/**
	 *       
	 * 
	 * @param oldPath
	 *            String        :c:/fqf.txt
	 * @param newPath
	 *            String        :f:/fqf.txt   f:/aa.txt
	 * @return boolean
	 */
	public static void copyFile(String oldPath, String newPath) {
		try {
			int bytesum = 0;
			int byteread = 0;
			File oldfile = new File(oldPath);
			if (oldfile.exists()) { //      
				InputStream inStream = new FileInputStream(oldPath); //      
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread; //         
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
			}
		} catch (Exception e) {
			System.out.println("          ");
			e.printStackTrace();

		}

	}

	/**
	 *          
	 * 
	 * @param oldPath
	 *            String        :c:/fqf
	 * @param newPath
	 *            String        :f:/fqf/ff
	 * @return boolean
	 */
	public static void copyFolder(String oldPath, String newPath) {

		try {
			(new File(newPath)).mkdirs(); //                 
			File a = new File(oldPath);
			String[] file = a.list();
			File temp = null;
			for (int i = 0; i < file.length; i++) {
				if (oldPath.endsWith(File.separator)) {
					temp = new File(oldPath + file[i]);
				} else {
					temp = new File(oldPath + File.separator + file[i]);
				}

				if (temp.isFile()) {
					FileInputStream input = new FileInputStream(temp);
					FileOutputStream output = new FileOutputStream(newPath
							+ "/" + (temp.getName()).toString());
					byte[] b = new byte[1024 * 5];
					int len;
					while ((len = input.read(b)) != -1) {
						output.write(b, 0, len);
					}
					output.flush();
					output.close();
					input.close();
				}
				if (temp.isDirectory()) {//        
					copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
				}
			}
		} catch (Exception e) {
			System.out.println("             ");
			e.printStackTrace();

		}

	}

	/**
	 *          
	 * 
	 * @param oldPath
	 *            String  :c:/fqf.txt
	 * @param newPath
	 *            String  :d:/fqf.txt
	 */
	public static void moveFile(String oldPath, String newPath) {
		copyFile(oldPath, newPath);
		delFile(oldPath);
	}

	/**
	 *          
	 * 
	 * @param oldPath
	 *            String  :c:/fqf
	 * @param newPath
	 *            String  :d:/fqf
	 */
	public static void moveFolder(String oldPath, String newPath) {
		copyFolder(oldPath, newPath);
		delFolder(oldPath);
	}
	
	public static void main(String[] args) {
	/*	newFolder("E:/text");
		newFile("E:/text/test.txt","hello every body");
		delFile("E:/text/test.txt");
		delFolder("E:/text");
		copyFile("D:/yy.txt", "E:/aa.txt");
		copyFolder("D:/Test","E:/Test");
		moveFile("D:/Test/Test.xap","E:/aa.xap");
		moveFolder("D:/Test","E:/aa/test");*/
	}
}