Java 简单IO文件处理(二)

2014-11-24 08:49:46 来源: 作者: 浏览: 2
d();
while(content!=-1){
bufferedOutputStream.write(content);
content = bufferedInputStream.read();
}
fileOutputStream.flush();

bufferedOutputStream.close();
bufferedInputStream.close();

fileOutputStream.close();
fileInputStream.close();
System.out.println("execute copyFileByBuffered(srcPath, targetPath) success!!");
}else {
throw new IOException("the src file is not exists!!");
}
}

/**
* copy file to target file by buffered ,then delete the file
* @param srcPath
* @param targetPath
* @throws IOException
*/
public static void cutFileByBuffered(String srcPath, String targetPath) throws IOException{
copyFileByBuffered(srcPath, targetPath);
deleteFile(srcPath);
System.out.println("execute cutFileByBuffered(srcPath, targetPath) success!!");
}

/**
* create this folder
* @param folderPath
* @throws IOException
*/
public static void createFolder(String folderPath) throws IOException{
File folderFile = new File(folderPath);
if(!folderFile.exists()){
//folderFile.mkdir();
//mkdirs 会创建指定路径的多个不存在文件夹,但是mkdir只会创建最底层的文件夹,如果其父目录不存在 则什么都不做
folderFile.mkdirs();
}else{
throw new IOException("this folder is exists!!!");
}
System.out.println("execute createFolder(folderPath) success!!");
}

/**
* copy folder to target folder
* @param srcFolderPath
* @param targetFolderPath
* @throws IOException
*/
public static void copyFolder(String srcFolderPath, String targetFolderPath) throws IOException{
File srcFolder = new File(srcFolderPath);
if(srcFolder.exists()){
createFolder(targetFolderPath);
String [] folderStrs = srcFolder.list();
for (int i = 0; i < folderStrs.length; i++) {
String subSrcFolderPath = srcFolderPath + File.separator + folderStrs[i];
File subSrcFolder = new File(subSrcFolderPath);

String subTargetFilePath = targetFolderPath + File.separator + folderStrs[i];
if(subSrcFolder.isDirectory()){
copyFolder(subSrcFolderPath, subTargetFilePath);
}else{
copyFileByBuffered(subSrcFolderPath, subTargetFilePath);
}
}
}else{
throw new IOException("this src folder is not exists!!!");
}
System.out.println("execute copyFolder(srcFolderPath, String targetFolderPath) success!!");
}

/**
* delete this folder
* @param folderPath
* @throws IOException
*/
public static void deleteFolder(String folderPath) throws IOException{
File folderFolder = new File(folderPath);
if(folderFolder.exists()){
String [] folderStrs = folderFolder.list();
for (int i = 0; i < folderStrs.length; i++) {
String subFolderPath = folderPath + File.separator + folderStrs[i];
File subFolder = new File(subFolderPath);
if(subFolder.isDirectory()){
deleteFolder(subFolderPath);
}else{
subFolder.delete();
}
}
folderFolder.delete();
}else{
throw new IOException("this src folder is not exists!!!");
}
System.out.println("execute deleteFolder(folderPath) success!!");
}

public static void main(String[] args) {
try {
//OperateFile.deleteFile("D:\\a");
//OperateFile.copyFile("D:\\a1.txt", "D:\\b.txt");
//OperateFile.copyFileByBuffered("D:\\a.txt", "D:\\b.txt");
//OperateFile.cutFileByBuffered("D:\\a.txt", "D:\\b.txt");
//OperateFile.createFolder("D:\\aaa.txt");
//OperateFile.copyFolder("d:\\a","d:\\newa");
OperateFile.deleteFolder("d:\\newa");

} catch (Exception e) {
e.printStackTrace();
}
-->

评论

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