`

JAVA对文件操作常用方法

阅读更多
public class FileOperate {   
    public FileOperate() {   
    }   
  
    /**  
     * 新建目录  
     *   
     * @param folderPath  
     *            String 如 c:/fqf  
     * @return boolean  
     */  
    public boolean newFolder(String folderPath) {   
        boolean iSucess = false;   
        try {   
            String filePath = folderPath;   
            filePath = filePath.toString();   
            File myFilePath = new File(filePath);   
            if (!myFilePath.exists()) {   
                if (myFilePath.mkdir())   
                    iSucess = true;   
            }   
        } catch (Exception e) {   
            System.out.println("新建目录操作出错");   
            e.printStackTrace();   
        }   
        return iSucess;   
    }   
  
    /**  
     * 新建文件  
     *   
     * @param filePathAndName  
     *            String 文件路径及名称 如c:/fqf.txt  
     * @param fileContent  
     *            String 文件内容  
     * @return boolean  
     */  
    public void newFile(String filePathAndName, String fileContent) {   
  
        try {   
            String filePath = filePathAndName;   
            filePath = filePath.toString();   
            File myFilePath = new File(filePath);   
            if (!myFilePath.exists()) {   
                myFilePath.createNewFile();   
            }   
            FileWriter resultFile = new FileWriter(myFilePath);   
            PrintWriter myFile = new PrintWriter(resultFile);   
            String strContent = fileContent;   
            myFile.println(strContent);   
            resultFile.close();   
  
        } catch (Exception e) {   
            System.out.println("新建目录操作出错");   
            e.printStackTrace();   
  
        }   
  
    }   
  
    /**  
     * 删除文件  
     *   
     * @param filePathAndName  
     *            String 文件路径及名称 如c:/fqf.txt  
     * @param fileContent  
     *            String  
     * @return boolean  
     */  
    public void delFile(String filePathAndName) {   
        try {   
            String filePath = filePathAndName;   
            filePath = filePath.toString();   
            File myDelFile = new File(filePath);   
            myDelFile.delete();   
  
        } catch (Exception e) {   
            System.out.println("删除文件操作出错");   
            e.printStackTrace();   
  
        }   
  
    }   
  
    /**  
     * 删除文件夹  
     *   
     * @param filePathAndName  
     *            String 文件夹路径及名称 如c:/fqf  
     * @param fileContent  
     *            String  
     * @return boolean  
     */  
    public void delFolder(String folderPath) {   
        try {   
            delAllFile(folderPath); // 删除完里面所有内容   
            String filePath = folderPath;   
            filePath = filePath.toString();   
            File myFilePath = new File(filePath);   
            myFilePath.delete(); // 删除空文件夹   
  
        } catch (Exception e) {   
            System.out.println("删除文件夹操作出错");   
            e.printStackTrace();   
  
        }   
  
    }   
  
    /**  
     * 删除文件夹里面的所有文件  
     *   
     * @param path  
     *            String 文件夹路径 如 c:/fqf  
     */  
    public void delAllFile(String path) {   
        File file = new File(path);   
        if (!file.exists()) {   
            return;   
        }   
        if (!file.isDirectory()) {   
            return;   
        }   
        String[] tempList = file.list();   
        File temp = null;   
        for (int i = 0; i < tempList.length; i++) {   
            if (path.endsWith(File.separator)) {   
                temp = new File(path + tempList[i]);   
            } else {   
                temp = new File(path + File.separator + tempList[i]);   
            }   
            if (temp.isFile()) {   
                temp.delete();   
            }   
            if (temp.isDirectory()) {   
                delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件   
                delFolder(path + "/" + tempList[i]);// 再删除空文件夹   
            }   
        }   
    }   
  
    /**  
     * 复制单个文件  
     *   
     * @param oldPath  
     *            String 原文件路径 如:c:/fqf.txt  
     * @param newPath  
     *            String 复制后路径 如:f:/fqf.txt  
     * @return boolean  
     */  
    public void copyFile(String oldPath, String newPath) {   
        try {   
            int bytesum = 0;   
            int byteread = 0;   
            File oldfile = new File(oldPath);   
            if (oldfile.exists()) { // 文件存在时   
                InputStream inStream = new FileInputStream(oldPath); // 读入原文件   
                FileOutputStream fs = new FileOutputStream(newPath);   
                byte[] buffer = new byte[1444];   
                int length;   
                while ((byteread = inStream.read(buffer)) != -1) {   
                    bytesum += byteread; // 字节数 文件大小   
                    System.out.println(bytesum);   
                    fs.write(buffer, 0, byteread);   
                }   
                inStream.close();   
            }   
        } catch (Exception e) {   
            System.out.println("复制单个文件操作出错");   
            e.printStackTrace();   
  
        }   
  
    }   
  
    /**  
     * 复制整个文件夹内容  
     *   
     * @param oldPath  
     *            String 原文件路径 如:c:/fqf  
     * @param newPath  
     *            String 复制后路径 如:f:/fqf/ff  
     * @return boolean  
     */  
    public void copyFolder(String oldPath, String newPath) {   
  
        try {   
            (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹   
            File a = new File(oldPath);   
            String[] file = a.list();   
            File temp = null;   
            for (int i = 0; i < file.length; i++) {   
                if (oldPath.endsWith(File.separator)) {   
                    temp = new File(oldPath + file[i]);   
                } else {   
                    temp = new File(oldPath + File.separator + file[i]);   
                }   
  
                if (temp.isFile()) {   
                    FileInputStream input = new FileInputStream(temp);   
                    FileOutputStream output = new FileOutputStream(newPath   
                            + "/" + (temp.getName()).toString());   
                    byte[] b = new byte[1024 * 5];   
                    int len;   
                    while ((len = input.read(b)) != -1) {   
                        output.write(b, 0, len);   
                    }   
                    output.flush();   
                    output.close();   
                    input.close();   
                }   
                if (temp.isDirectory()) {// 如果是子文件夹   
                    copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);   
                }   
            }   
        } catch (Exception e) {   
            System.out.println("复制整个文件夹内容操作出错");   
            e.printStackTrace();   
  
        }   
  
    }   
  
    public List<String> GetListFileName(String path){      
           
        File parentFile=new File(path);    
           
        File[] childrenFile=parentFile.listFiles();    
           
        ArrayList<String> txtFile=new ArrayList<String>();   
           
        if(childrenFile!=null&&childrenFile.length>0){     
               
                for(int   i=0;i<childrenFile.length;i++){     
                        if(childrenFile[i].getName().endsWith(".txt") || childrenFile[i].getName().endsWith(".min") )      
                                txtFile.add(childrenFile[i].toString());        
                }      
        }      
        return txtFile;      
      }    
       
    /**  
     * 读取目录下的文件列表中的内容  
     *   
     * @param fileList 例如:C:/IP下的所有.TXT  
     * @return   
     */  
    public List<String> ReadFileList(List<String> fileList)   
    {   
        int count = 0 ;   
        List<String> listContent = new ArrayList<String>();   
  
        for(String file : fileList)   
        {   
            count ++;   
            try {   
                   
                 FileReader read = new FileReader(new File(file));   
                 StringBuffer sb = new StringBuffer();   
                 char ch[] = new char[1024*1024];   
                 int d = read.read(ch);   
                    
                 while(d!=-1){   
                    String str = new String(ch,0,d);   
                    sb.append(str);   
                    d = read.read(ch);   
                    System.out.println(sb);   
                    listContent.add(sb.toString());   
                  }   
             } catch (FileNotFoundException e) {   
                  e.printStackTrace();   
             } catch (IOException e) {   
                e.printStackTrace();   
             }   
        }   
        return listContent;   
    }   
    /**  
     * 移动文件到指定目录  
     *   
     * @param oldPath  
     *            String 如:c:/fqf.txt  
     * @param newPath  
     *            String 如:d:/fqf.txt  
     */  
    public void moveFile(String oldPath, String newPath) {   
        copyFile(oldPath, newPath);   
        delFile(oldPath);   
  
    }   
  
    /**  
     * 移动文件到指定目录  
     *   
     * @param oldPath  
     *            String 如:c:/fqf.txt  
     * @param newPath  
     *            String 如:d:/fqf.txt  
     */  
    public void moveFolder(String oldPath, String newPath) {   
        copyFolder(oldPath, newPath);   
        //delFolder(oldPath);   
  
    }   
  
    public static void main(String[] args) {   
        FileOperate oper = new FileOperate();   
        oper.moveFolder("E:/IP", "E:/Oracle10G/周宇/ipconfig");   
    }   
}  

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiaoyu411502/archive/2009/08/30/4496506.aspx

 

分享到:
评论

相关推荐

    基于JAVA的常用文件操作方法

    NULL 博文链接:https://chong0660.iteye.com/blog/2367232

    java基本的文件操作

    java语言中的基本的文件操作介绍,附源码

    JAVA 文件常用流操作.zip

    JAVA 文件常用流操作 包括文件的创建,重命名,删除等等。字节流,字符流,缓存流,数据流,打印流,内存流等等

    java处理文件常用操作

    在Java后端中处理文件通常涉及到文件的读取、写入、删除以及可能的文件流处理等操作。

    java-文件工具,可以查看文件类型,文件魔数,可以判断是否是视频文件,音乐文件,图片文件等等

    java文件的工具类,封装了常用的操作,尤其针对文件的实际类型,通过获取文件的byte,来查看文件起始字节的魔数值,通过魔数值来判断文件的类型,工具集合了常用的文件类型对应的魔数,也封装了文件类型的判断方法

    Java文件操作方法总结

    Java文件操作中的一些常用方法的总结,可以参考参考啦!

    JAVA对数字证书的常用操作

    阅读提示:本文介绍JAVA对数字证书的常用操作 一需要包含的包 import java.security.*; import java.io.*; import java.util.*; import java.security.*; import java.security.cert.*; import sun.security.x509....

    Java常用文件处理类

    该类主要对常见的一些文件操作进行了封装,如读写文件(UTF-8)、复制文件、删除文件,创建目录等

    Java常用FTP文件操作说明Apache.FTPClient,ftp4j,jftp

    Java常用FTP 文件操作 说明 Apache FTPClient ftp4j jftp java中实现ftp 文件上传 文件下载

    java常用命令及常用选项

    java常用命令及常用选项:包含了对java文件的一般的命令行操作

    java应用ftp操作文件

    最近在做ftp文件的上传与下载,基于此,整理了一下资料。本来想采用java自带的方法,可是看了一下jdk1.6与1.7的实现方法有点区别,于是采用了Apache下的框架实现的。。。

    java常用代码

    工程简单的介绍了java常用类,并用这些类进行一些简单的操作 让初学者更好的了解java这门语言的特性。 1.StringAndInt.java 字符与整型的相互转换 2.WriteFile.java 简单的IO读写文件 3.CurrentMethod.java 获取当前...

    Java 功能丰富的文件操作类.rar

    与大家分享一个功能丰富的Java文件操作类,类中封装了一些常用的文件操作,大部分涉及文件的修改、内容替换等。类中的方法都是静态方法,不需要生成此类的实例, 为避免生成此类的实例,构造方法被申明为private类型...

    java 操作Zip文件(压缩、解压、加密).zip

    java 操作Zip文件(压缩、解压、加密) zip4j-1.3.2.jar ant-1.10.6.jar

    Java CSV文件读取

    Java读取excel,Excel是我们平时工作中比较常用的用于存储二维表数据的,JAVA也可以直接对Excel进行操作。

    java文件上传和下载

    使用jdk7及以后的文件操作,包含了常用类、常用方法的介绍和文件上传和下载的代码

    JAVAWEB各种配置文件加常用操作

    JAVAWEB各种配置文件加常用操作(spring strut2 jpa hibernate jdbc jndi springMVC velocity ant log4j ehcache等各种配置文件及常用操作),相信对你的开发一定会有很大的帮助!

    实验二:熟悉常用的HDFS操作

    A.2实验二:熟悉常用的HDFS操作 ...(3)熟悉HDFS操作常用的Java API。 A.2.2 实验平台 (1)操作系统:Linux(建议Ubuntu 16.04)。(2) Hadoop版本:2.7.1。 (3)JDK版本:1.7或以上版本。(4) Java IDE:Eclipse。

    《Java文件操作大全》电子书

    《Java文件操作大全》电子书 本文汇集常用文件操作方法,包括文件的建立/检查与删除,目录的建立/检查与删除,取出目录中文件,文件属性的取得,逐行读取数据等等。

    Java实验6 输入输出流与文件操作.doc

    文件操作是Java编程中常用的技术之一,用于读写文件、存储数据和实现数据的持久化。下面将对Java中的输入输出流和文件操作进行详细的解释和分析。 一、Java中的输入输出流 Java中的输入输出流是指从源头读取数据并...

Global site tag (gtag.js) - Google Analytics