Java 简单IO文件处理(一)

2014-11-24 08:49:46 ? 作者: ? 浏览: 7

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

-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: