日期:2014-05-19  浏览次数:20634 次

IO文件处理
/**
 * 创建一个文件 如果存在,不做任何操作.如果不存在创建文件夹
 * 
 * @param filePath
 *            文件路径
 * @throws IOException
 *             IO异常
 */
public static void newFile(String filePath) throws IOException {
// 取得最后一个分隔符的位置
int position = filePath.lastIndexOf("\\");
// 取得文件夹路径
String folderPath = filePath.substring(0, position);
newFolder(folderPath);
// 实例化File类
File file = new File(filePath);
file.createNewFile();
}

/**
 * 删除一个文件
 * 
 * @param filePath
 *            文件路径
 */
public static void deleteFile(String filePath) {
// 实例化File类
File file = new File(filePath);
if (!file.isDirectory()) {
file.delete();
}
}

/**
 * 文件拷贝
 * 
 * @param srcPath
 *            源文件
 * @param destPath
 *            目标文件
 * @throws IOException
 *             IO异常
 */
public static void copyFile(String srcPath, String destPath)
throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 打开输入流
fis = new FileInputStream(new File(srcPath));
// 打开输出流
fos = new FileOutputStream(new File(destPath));
// 用输入流中的方法读文件
int fisContent = fis.read();
// 用输出流的方法写文件
if (fisContent != -1) {
fos.write(fisContent);
fisContent = fis.read();
}
// 把输出流中的东西强制刷到destPath
fos.flush();
} finally {
// 倒叙关闭输入流、输出流
if (fos != null) {
fos.close();
}
if (fis != null) {
fis.close();
}
}
}

/**
 * 文件拷贝(缓冲区实现)
 * 
 * @param srcPath
 *            源文件
 * @param destPath
 *            目标文件
 * @throws IOException
 *             IO异常
 */
public static void copyFileByBuffered(String srcPath, String destPath)
throws IOException {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 创建字节缓存输入流
bis = new BufferedInputStream(
new FileInputStream(new File(srcPath)));
// 创建字节缓存输出流
bos = new BufferedOutputStream(new FileOutputStream(new File(
destPath)));
// 用缓存输入流读
byte[] buffer&nb