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