手机版
你好,游客 登录 注册
背景:
阅读新闻

Java 简单IO文件处理

[日期:2012-12-26] 来源:Linux社区  作者:ch656409110 [字体: ]

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.read();
   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();
  }
 }
}

linux
相关资讯       Java编程  Java IO 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款