`
eriol
  • 浏览: 401139 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java中的文件操作

    博客分类:
  • Java
阅读更多

使用Java实现一些文件操作。

 

import java.io.*;

public class FileOperator {
	
	public FileOperator() {
	}
	
	/**
	 * Create new folder
	 * @param folderPath String as c:/test
	 */
	public void newFolder(String folderPath) {
		try {
			File myFilePath = new File(folderPath);
			if (!myFilePath.exists()) {
				myFilePath.mkdir();
			}
		} catch (Exception e) {
			System.out.println("error in creating folder");
			e.printStackTrace();
		}
	}
	
	/**
	 * Create new file
	 * @param filePath String as c:/test.txt
	 * @param fileContent
	 */
	public void newFile(String filePath, String fileContent) {
		try {
			File file = new File(filePath);
			if (!file.exists()) {
				file.createNewFile();
			}
			
			FileWriter writer = new FileWriter(filePath);
			PrintWriter out = new PrintWriter(writer);
			out.println(fileContent);
			writer.close();
		} catch (Exception e) {
			System.out.println("error in creating new file");
			e.printStackTrace();
		}
	}
	
	/**
	 * Delete a file
	 * @param filePath
	 */
	public void delFile(String filePath) {
		try {
			File file = new File(filePath);
			file.delete();
		} catch (Exception e) {
			System.out.println("error in deleting file");
			e.printStackTrace();
		}
	}
	
	/**
	 * Delete folder
	 * @param folderPath
	 */
	public void delFolder(String folderPath) {
		try {
			delAllFile(folderPath);	//删除完里面所有内容
			File folder = new File(folderPath);
			folder.delete();	//删除空文件夹
		} catch (Exception e) {
			System.out.println("error in deleting folder");
			e.printStackTrace();
		}
	}
	
	/**
	 * Delete all files
	 * @param folderPath
	 */
	public void delAllFile(String folderPath) {
		try {
			File folder = new File(folderPath);
			if (!folder.exists()) {
				return;
			}
			if (!folder.isDirectory()) {
				return;
			}
			String[] fileList = folder.list();
			String path = null;
			if (!folderPath.endsWith(File.separator)) {
				path = folderPath + File.separator;
			}
			for (int i = 0; i < fileList.length; i++) {
				File file = new File(path + fileList[i]);
				if (file.isFile()) {
					file.delete();
				} else if (file.isDirectory()) {
					delAllFile(path + fileList[i]);	//先删除文件夹里面的文件
					delFolder(path + fileList[i]);	//再删除空文件夹
				}
			}
		} catch (Exception e) {
			System.out.println("error in deleting all files");
			e.printStackTrace();
		}
	}
	
	/**
	 * Copy a file
	 * @param sourcePath
	 * @param destPath
	 */
	public void copyFile(String sourcePath, String destPath) {
		try {
			int bytesum = 0;
			int byteread = 0;
			File file = new File(sourcePath);
			if (file.exists()) {	 //文件存在时
				InputStream in = new FileInputStream(sourcePath);
				FileOutputStream out = new FileOutputStream(destPath);
				byte[] buffer = new byte[1024];
				while ((byteread = in.read(buffer)) != -1) {
					bytesum += byteread;	//字节数 文件大小
					System.out.println(bytesum);
					out.write(buffer, 0, byteread);
				}
				out.flush();
				out.close();
				in.close();
			}
		} catch (Exception e) {
			System.out.println("error in copy file");
			e.printStackTrace();
		}
	}
	
	/**
	 * Copy folder
	 * @param sourcePath
	 * @param destPath
	 */
	public void copyFolder(String sourcePath, String destPath) {
		try {
			new File(destPath).mkdir();
			File folder = new File(sourcePath);
			String[] fileList = folder.list();
			String source = null;
			String dest = null;
			if (!sourcePath.endsWith(File.separator))
					source = sourcePath + File.separator;
			if (!destPath.endsWith(File.separator))
				dest = destPath + File.separator;
			for (int i = 0; i < fileList.length; i++) {
				File file = new File(source + fileList[i]);
				if (file.isFile()) {
					copyFile(source+fileList[i], dest+fileList[i]);
				} else if (file.isDirectory()) {
					copyFolder(source+fileList[i], dest+fileList[i]);
				}
			}
		} catch (Exception e) {
			System.out.println("error in copying folder");
			e.printStackTrace();
		}
	}
	
	/**
	 * Move file
	 * @param sourcePath
	 * @param destPath
	 */
	public void moveFile(String sourcePath, String destPath) {
		copyFile(sourcePath, destPath);
		delFile(sourcePath);
	}
	
	/**
	 * Move folder
	 * @param sourcePath
	 * @param destPath
	 */
	public void moveFolder(String sourcePath, String destPath) {
		copyFolder(sourcePath, destPath);
		delFolder(sourcePath);
	}
	
	/**
	 * displayFiles
	 * @param path
	 */
	public void displayFiles(String path) {
		File file = new File(path);
		if (file.isFile()) {
			System.out.println(file.getName());
		} else {
			System.out.println("<DIR> " + file.getName());
			String[] fileList = file.list();
			String pathName = file.getAbsolutePath();
			for (int i = 0; i < fileList.length; i++) {
				displayFiles(pathName + File.separator + fileList[i]);
			}
		}
	}
	
	/**
	 * Read a file by char
	 * @param filePath
	 */
	public void readFile(String filePath) {
		try {
			InputStream in = new FileInputStream(filePath);
			byte b; 
			while ((b = (byte)in.read()) != -1) {
				System.out.print((char)b);
			}
		} catch (Exception e) {
			System.out.println("error in read file");
			e.printStackTrace();
		}
	}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics