BufferedReaderおよびBufferedWriterインスタンス

2095 ワード

/**
 *  
 * @author samLee
 *
 */
public class FileUtil {
	
	private static FileReader fr = null;
	private static BufferedReader bf = null;
	private static FileWriter fw = null;
	private static BufferedWriter bw = null;
	private static File tempFile_1 =null;
	private static File tempFile_2 =null;
	
	/**
	 *  
	 * @param srcFile		 
	 * @param targetFile	 
	 * @throws Exception	 
	 */
	public static void copy(String srcFile,String targetFile) throws IOException{
		
		// , 
		String tempStr = targetFile.substring(0, targetFile.lastIndexOf("\\")+1);
		System.out.println(tempStr);
		tempFile_2 = new File(tempStr);
		
		if(!tempFile_2.exists()){
			tempFile_2.mkdirs();
		}
		
		// 
		fr = new FileReader(srcFile);
		bf = new BufferedReader(fr);
		fw = new FileWriter(targetFile);
		bw = new BufferedWriter(fw);
		
		tempFile_1 = new File(targetFile);
		// 
		String str =  bf.readLine();
		
		// 
		while(str!=null){
			bw.write(str);
			bw.newLine();
			bw.flush();
			str = bf.readLine();
		}
		
		
		if(tempFile_1.exists()){
			System.out.println(" ");
		}
	}
	
	/**
	 *  
	 */
	public static void close() throws IOException{
		bw.flush();
		bw.close();
		fw.close();
		bf.close();
		fr.close();
	}
}

 
public class FileTest {

	public static void main(String[] args) {
		// copy html , D 
		String srcFile = "D:\\example.html";
		
		// copy 
		String targetFile = "D:\\htmlDir\\example.html";
		
		try {
			FileUtil.copy(srcFile, targetFile);
			FileUtil.close();
		} catch (IOException e) {
			System.out.println(" :");
			e.printStackTrace();
		}
	}
}