当前位置: 移动技术网 > IT编程>开发语言>Java > 基于java file 文件操作operate file of java的应用

基于java file 文件操作operate file of java的应用

2019年07月22日  | 移动技术网IT编程  | 我要评论
java文件操作
复制代码 代码如下:

package com.b510;

 import java.io.file;
 import java.io.fileinputstream;
 import java.io.fileoutputstream;
 import java.io.filewriter;
 import java.io.inputstream;
 import java.io.printwriter;

 /**
  *
  * @author hongten</br>
  *
  *         文件的操作
  *
  *         
  *
  */
 public class operatefiles {

     /**
      * @param args
 */
     public static void main(string[] args) {
         operatefiles operatefiles = new operatefiles();
         //新建一个文件夹
         operatefiles.newfolder("c:/hongten");
         //新建一个文件,同时向里面写入内容
         operatefiles.newfile("c:/hongten/hello.txt", "hello,i'm hongten.你好");
         //删除一个文件
         operatefiles.deletefile("c:/hongten/hello.txt");
         //删除一个文件夹
         operatefiles.deletefolder("c:/hongten");
         //复制文件夹
         operatefiles.copyfolder("c:/hongten", "e:/hongten");
         //提取文件的扩展名
         string expandedname=operatefiles.getexpandedname("c:/hongten/hello.txt");
         system.out.println(expandedname);
         //提取文件的路径
         system.out.println(operatefiles.getfilepath("c:/hongten/hello.txt"));
     }

     /**
      * 获得文件的扩展名
      * @param filepath 文件的路径 如:c:/hongten/hello.txt
      * @return 文件的扩展名 如:txt
 */
     public string getexpandedname(string filepath){
         return filepath.substring(filepath.lastindexof(".")+1);
     }
     /**
      * 获得文件的路径
      * @param file 文件的路径
      * @return 文件的路径
 */
     public string getfilepath(string file){
         return file.substring(0,file.lastindexof("/")); 
     }
     /**
      * 新建一个目录
      *
      * @param folderpath
      *            新建目录的路径 如:c:\\newfolder
 */
     public void newfolder(string folderpath) {
         try {
             file myfolderpath = new file(folderpath.tostring());
             if (!myfolderpath.exists()) {
                 myfolderpath.mkdir();
             }
         } catch (exception e) {
             system.out.println("新建目录操作出错");
             e.printstacktrace();
         }
     }

     /**
      * 新建一个文件
      *
      * @param filepath
      *            新建文件的目录 如:c:\\hongten.java
 */
     public void newfile(string filepath) {
         try {
             file myfilepathfile = new file(filepath.tostring());
             if (!myfilepathfile.exists()) {
                 myfilepathfile.createnewfile();
             }
         } catch (exception e) {
             system.out.println("新文件创建失败");
             e.printstacktrace();
         }
     }

     /**
      * 新建一个文件,同时向文件中写入内容
      *
      * @param filepath
      *            新建文件的目录 如:c:\\hongten.java
      * @param filecontent
      *            向文件中写入的内容
 */
     public void newfile(string filepath, string filecontent) {
         try {
             newfile(filepath);
             filewriter resultfile = new filewriter(filepath);
             printwriter myfile = new printwriter(resultfile);
             myfile.println(filecontent);
             resultfile.close();
         } catch (exception e) {
             system.out.println("新建文件操作出错");
             e.printstacktrace();
         }
     }

     /**
      * 删除一个文件
      *
      * @param filepath
      *            要删除文件的绝对路径 如:c:\\hongten\\hello.txt
 */
     public void deletefile(string filepath) {
         try {
             file predelfile = new file(filepath);
             if (predelfile.exists()) {
                 predelfile.delete();
             } else {
                 system.out.println(filepath + "不存在!");
             }
         } catch (exception e) {
             system.out.println("删除文件操作出错");
             e.printstacktrace();
         }
     }

     /**
      * 删除一个文件夹
      *
      * @param folderpath
      *            要删除的文件夹的绝对路径 如:c:\\hongten
 */
     public void deletefolder(string folderpath) {
         try {
             delallfiles(folderpath);
             file predelfoder = new file(folderpath);
             if (predelfoder.exists()) {
                 predelfoder.delete();
             } else {
                 system.out.println(folderpath + "不存在!");
             }
         } catch (exception e) {
             system.out.println("删除文件操作出错");
             e.printstacktrace();
         }
     }

     /**
      * 删除一个文件夹下的所有文件
      *
      * @param folderpath
      *            要删除的文件夹的绝对路径 如:c:\\hongten
 */
     public void delallfiles(string folderpath) {
         file file = new file(folderpath);
         if (!file.exists()) {
             return;
         }
         if (!file.isdirectory()) {
             return;
         }
         string[] templist = file.list();
         file temp = null;
         for (int i = 0; i < templist.length; i++) {
             if (folderpath.endswith(file.separator)) {
                 temp = new file(folderpath + templist[i]);
             } else {
                 temp = new file(folderpath + file.separator + templist[i]);
             }
             if (temp.isfile()) {
                 temp.delete();
             }
             if (temp.isdirectory()) {
                 delallfiles(folderpath + "/" + templist[i]);// 先删除文件夹里面的文件
                 deletefolder(folderpath + "/" + templist[i]);// 再删除空文件夹
             }
         }
     }

     /**
      * 单个文件的复制
      *
      * @param oldpath
      *            原路径 如:c:\\hello.java
      * @param newpath
      *            新路径 如:f:\\hello.java
 */
     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 = 0;
                 while ((byteread = instream.read(buffer)) != -1) {
                     bytesum += byteread; // 字节数 文件大小
                     fs.write(buffer, 0, byteread);
                 }
                 instream.close();
             }
         } catch (exception e) {
             system.out.println("复制单个文件操作出错");
             e.printstacktrace();

         }
     }

     /**
      * 文件夹的复制
      *
      * @param oldpath
      *            原文件夹路径 如: c:\\hello
      * @param newpath
      *            新文件夹路径 如: e:\\hello
 */
     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();

         }
     }

     /**
      * 移动单个文件
      *
      * @param oldpath
      *            源文件路径 如:c:\\hello.java
      * @param newpath
      *            新文件路径 如:e:\\hello.java
 */
     public void movefile(string oldpath, string newpath) {
         copyfile(oldpath, newpath);
         deletefile(oldpath);
     }

     /**
      * 移动文件夹
      *
      * @param oldpath
      *            原文件夹路径 如:c:\\hongten
      * @param newpath
      *            新文件夹路径 如:e:\\hongten
 */
     public void movefolder(string oldpath, string newpath) {
         copyfolder(oldpath, newpath);
         deletefolder(oldpath);
     }

     /**
      * 获得系统根目录绝对路径
      *
      * @return
 */
     public string getpath() {
         string syspath = this.getclass().getresource("/").getpath();
         // 对路径进行修改
         syspath = syspath.substring(1, syspath.length() - 16);
         return syspath;
     }

 }

现在有时间把这些东西整理出来,给大家分享一下……

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网