Java 简单IO文件处理
package com.java.file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* operate file
* @author Administrator
*
*/
public class OperateFile {
/**
* create this file
* @param filePath
* @throws IOException
*/
public static void createFile(String filePath) throws IOException{
int lastPoint = filePath.lastIndexOf("\\");
if(lastPoint==-1){
throw new IOException("this filePath is not true !!");
}
String folderPath = filePath.substring(0, lastPoint);
File folderFile = new File(folderPath);
if(folderFile.exists()){
File newFile = new File(filePath);
if(!newFile.exists()){
newFile.createNewFile();
}else{
throw new IOException("the file is exists !!");
}
}else{
throw new IOException("application cann't find the folder!!");
}
System.out.println("execute createFile(filePath) success!!");
}
/**
* delete this file
* @param filePath
* @throws IOException
*/
public static void deleteFile(String filePath) throws IOException{
String folderPath = filePath.substring(0, filePath.lastIndexOf("\\"));
File folderFile = new File(folderPath);
if(folderFile.exists()){
File file = new File(filePath);
if(file.exists()){
file.delete();
}else{
throw new IOException("the file is not exists !!");
}
}else{
throw new IOException("application cann't find the folder!!");
}
System.out.println("execute deleteFile(filePath) success!!");
}
/**
* copy file to target file
* @param srcPath
* @param targetPath
* @throws IOException
*/
public static void copyFile(String srcPath, String targetPath) throws IOException{
File srcFile = new File(srcPath);
if(srcFile.exists()){
//create fileInputDtream read file
FileInputStream fileInputStream = new FileInputStream(srcFile);
File targetFile = new File(targetPath);
if(!targetFile.exists()){
createFile(targetPath);
}
//create fileOutputStream write file
FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
int content = fileInputStream.read();
while(content!=-1){
fileOutputStream.write(content);
content = fileInputStream.read();
}
fileOutputStream.flush();
fileOutputStream.close();
fileInputStream.close();
System.out.println("execute copyFile(srcPath, targetPath) success!!");
}else {
throw new IOException("the src file is not exists!!");
}
}
/**
* copy file to target file by buffered
* @param srcPath
* @param targetPath
* @throws IOException
*/
public static void copyFileByBuffered(String srcPath, String targetPath) throws IOException{
File srcFile = new File(srcPath);
if(srcFile.exists()){
//create fileInputDtream read file
FileInputStream fileInputStream = new FileInputStream(srcFile);
//create bufferedInputStream
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
File targetFile = new File(targetPath);
if(!targetFile.exists()){
createFile(targetPath);
}
//create fileOutputStream write file
FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
//create bufferedOutputStream
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
int content = bufferedInputStream.rea
