栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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;

 
 public class OperateFiles {

    
     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"));
     }

    
     public String getExpandedName(String filePath){
         return filePath.substring(filePath.lastIndexOf(".")+1);
     }
    
     public String getFilePath(String file){
         return file.substring(0,file.lastIndexOf("/")); 
     }
    
     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();
         }
     }

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

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

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

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

    
     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]);// 再删除空文件夹
             }
         }
     }

    
     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();

         }
     }

    
     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 void moveFile(String oldPath, String newPath) {
         copyFile(oldPath, newPath);
         deleteFile(oldPath);
     }

    
     public void moveFolder(String oldPath, String newPath) {
         copyFolder(oldPath, newPath);
         deleteFolder(oldPath);
     }

    
     public String getPath() {
         String sysPath = this.getClass().getResource("/").getPath();
         // 对路径进行修改
         sysPath = sysPath.substring(1, sysPath.length() - 16);
         return sysPath;
     }

 }

现在有时间把这些东西整理出来,给大家分享一下……
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/153265.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号