首页 > 其他分享 >文件操作工具类

文件操作工具类

时间:2023-06-11 16:23:50浏览次数:17  
标签:文件 String newName import File 操作 new 工具 null

import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {

    /*判断文件是否存在*/
    public static boolean isExists(String filePath) {
        File file = new File(filePath);
        return file.exists();
    }

    /*判断是否是文件夹*/
    public static boolean isDir(String path) {
        File file = new File(path);
        if (file.exists()) {
            return file.isDirectory();
        } else {
            return false;
        }
    }

    /**
     * 文件或者目录重命名
     *
     * @param oldFilePath 旧文件路径
     * @param newName     新的文件名,可以是单个文件名和绝对路径
     * @return
     */
    public static boolean renameTo(String oldFilePath, String newName) {
        try {
            File oldFile = new File(oldFilePath);
            //若文件存在
            if (oldFile.exists()) {
                //判断是全路径还是文件名
                if (newName.indexOf("/") < 0 && newName.indexOf("\\") < 0) {
                    //单文件名,判断是windows还是Linux系统
                    String absolutePath = oldFile.getAbsolutePath();
                    if (newName.indexOf("/") > 0) {
                        //Linux系统
                        newName = absolutePath.substring(0, absolutePath.lastIndexOf("/") + 1) + newName;
                    } else {
                        newName = absolutePath.substring(0, absolutePath.lastIndexOf("\\") + 1) + newName;
                    }
                }
                File file = new File(newName);
                //判断重命名后的文件是否存在
                if (file.exists()) {
                    System.out.println("该文件已存在,不能重命名");
                } else {
                    //不存在,重命名
                    return oldFile.renameTo(file);
                }
            } else {
                System.out.println("原该文件不存在,不能重命名");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    /*文件拷贝操作*/
    public static void copy(String sourceFile, String targetFile) {
        File source = new File(sourceFile);
        File target = new File(targetFile);
        target.getParentFile().mkdirs();
        FileInputStream fis = null;
        FileOutputStream fos = null;
        FileChannel in = null;
        FileChannel out = null;
        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(target);
            in = fis.getChannel();//得到对应的文件通道
            out = fos.getChannel();//得到对应的文件通道
            in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /*读取Text文件操作*/
    public static String readText(String filePath) {
        String lines = "";
        try {
            FileReader fileReader = new FileReader(filePath);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                lines += line + "\n";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lines;
    }

    /*写入Text文件操作
    * isAppend:是否追加写入
    */
    public static void writeText(String filePath, String content, boolean isAppend) {
        FileOutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        BufferedWriter bufferedWriter = null;
        try {
            outputStream = new FileOutputStream(filePath, isAppend);
            outputStreamWriter = new OutputStreamWriter(outputStream);
            bufferedWriter = new BufferedWriter(outputStreamWriter);
            bufferedWriter.write(content);
            bufferedWriter.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedWriter != null) {
                    bufferedWriter.close();
                }
                if (outputStreamWriter != null) {
                    outputStreamWriter.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}

 

标签:文件,String,newName,import,File,操作,new,工具,null
From: https://www.cnblogs.com/ixtao/p/17473096.html

相关文章

  • Net Core 使用Mongodb操作文件(上传,下载)
    NetCore使用Mongodb操作文件(上传,下载)1.Mongodb GridFS文件操作帮助类。GridFS介绍 https://baike.baidu.com/item/GridFS/6342715?fr=aladdinDLL源码:https://gitee.com/chenjianhua1985/mongodb-client-encapsulationDLL文件:链接:https://pan.baidu.com/s/1SpWDtaXjavalJ......
  • Laravel 框架使用外部的js、css等文件
    Laravel框架使用外部的js、css等文件阅读有道云笔记https://note.youdao.com/s/d1ZQ9AC8Laravel项目的web虚拟主机指定的目录(即网址的根目录),项目的入口文件笔系统的静态资源目录(css、img、js、uploads)后期使用的外部静态文件都需要放到Public目录下,图中所示,可以想像成views......
  • WFP必须掌握的技能之自定义控件——实战:自制上传文件显示进度按钮
    自定义控件在WPF开发中是很常见的,有时候某些控件需要契合业务或者美化统一样式,这时候就需要对控件做出一些改造。目录按钮设置圆角按钮上传文件相关定义测试代码话不多说直接看效果默认效果:上传效果:按钮设置圆角因为按钮本身没有CornerRadius属性,所以只能重写Butto......
  • WEB漏洞—文件上传之黑白名单绕过
    文件上传常见验证---后缀名,类型,文件头等1. 后缀名:黑名单,白名单后缀名是直接的验证,分为黑名单(asp,php,jsp,aspx,cgi,war…)和白名单(jpg,png,zip,rar,gif…)。黑名单是不允许那些格式的文件上传,缺陷:添加文件的后缀,如PHP5等;白名单是只允许那些格式的文件上传,其余文件则不允许上传......
  • 【Java技术专题】「Guava开发指南」手把手教你如何进行使用Guava工具箱进行开发系统实
    异常传播有时候,您可能需要重新抛出捕获到的异常。这种情况通常发生在捕获到Error或RuntimeException时,因为您可能没有预料到这些异常,但在声明捕获Throwable和Exception时,它们也被包含在内了。为了解决这个问题,Guava提供了多种方法来判断异常类型并重新抛出异常。例如:try{......
  • java 聚合项目--pom.xml配置文件
    java聚合项目1.parent--pom.xml配置文件4.0.0<groupId>org.test.java.vip.train</groupId><artifactId>test-train-parent</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules>&l......
  • FFmpeg操作电脑摄像头实验
    最近在学习音视频相关知识,参考雷神(leixiaohua1020)的博客做了一些小实验,记录备忘。一、记录几条命令1、列出电脑支持的音视频设备ffmpeg-list_devicestrue-fdshow-idummy输出如下(节选了关键内容):[dshow@0000025feeb5d840]DirectShowvideodevices(somemaybebot......
  • Linux平台下的ELF文件结构探索
    本文以目标文件的结构为引子,通过探索在Linux环境下,一个具体的目标文件的结构来窥探ELF文件的结构。了解ELF文件的结构,对于加深对链接的理解、认识操作系统背后机理都有很大好处。编译和链接在正式讨论目标文件的结构之前,需要先对一个C/C++程序从源代码到可执行程序的构建过程有所了......
  • linux学习笔记:网路诊断工具-mtr命令
    网络诊断工具-mtr命令最近在面试的过程中,被问到了mtr命令,一脸懵逼,据面试官了解,该命令在公司里是经常使用的,借此我也来学习一下!网络诊断的背景网络诊断工具包括ping,traceroute和mtr,它们使用Internet控制消息协议(ICMP)数据包来测试Internet上两点之间的连接和传输。当用户在Intern......
  • VC内存泄露检查工具:Visual Leak Detector
    灵活自由是C/C++语言的一大特色,而这也为C/C++程序员出了一个难题。当程序越来越复杂时,内存的管理也会变得越加复杂,稍有不慎就会出现内存问题。内存泄漏是最常见的内存问题之一。内存泄漏如果不是很严重,在短时间内对程序不会有太大的影响,这也使得内存泄漏问题有很强的隐蔽性,不容......