• 文件的操作


    package com.green.project.update.io;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.FilenameFilter;
    import java.io.InputStream;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;

    public class FileOperator {
    /**
    * 新建目录
    *
    * @param folderPath
    * String 如 c:/fqf
    * @return boolean
    */
    public static void newFolder(String folderPath) {
    try {
    String filePath = folderPath;
    filePath = filePath.toString();
    File myFilePath = new File(filePath);
    if (!myFilePath.exists()) {
    myFilePath.mkdir();
    }
    } catch (Exception e) {
    System.out.println("新建目录操作出错");
    e.printStackTrace();
    }
    }

    /**
    * 新建文件
    *
    * @param filePathAndName
    * String 文件路径及名称 如c:/fqf.txt
    * @param fileContent
    * String 文件内容
    * @return boolean
    */
    public static 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 static 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 static 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 static 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 static 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 static 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
    * String 如:c:/fqf.txt
    * @param newPath
    * String 如:d:/fqf.txt
    */
    public static void moveFile(String oldPath, String newPath) {
    copyFile(oldPath, newPath);
    delFile(oldPath);

    }

    /**
    * 移动文件到指定目录
    *
    * @param oldPath
    * String 如:c:/fqf.txt
    * @param newPath
    * String 如:d:/fqf.txt
    */
    public static void moveFolder(String oldPath, String newPath) {
    copyFolder(oldPath, newPath);
    delFolder(oldPath);

    }

    /*
    * 获取文件夹下所有文件
    *
    * @param 文件名
    *
    * @return 所有文件列表
    */
    public static List<String> files(String path) {
    File file = new File(path);
    if (!file.isDirectory()) {
    return null;
    }
    List<String> files = new ArrayList<String>();
    for (File s : file.listFiles()) {
    if (s.isFile()) {
    files.add(s.getName());
    }

    }
    return files;
    }

    /*
    * 获取文件夹下所有文件
    *
    * @param 文件名
    *
    * @return 所有文件列表
    */
    public static List<String> filesAndDirs(String path) {
    File file = new File(path);
    if (!file.isDirectory()) {
    return null;
    }
    List<String> files = new ArrayList<String>();
    for (File s : file.listFiles()) {
    files.add(s.getName());

    }
    return files;
    }

    /*
    * 获取文件夹下指定文件
    *
    * @param 文件名
    *
    * @param 后缀名
    *
    * @return 所有文件列表
    */
    public static List<String> findsBySuffix(String path, final String suffix) {
    File file = new File(path);
    if (!file.isDirectory()) {
    return null;
    }
    List<String> files = new ArrayList<String>();
    for (File s : file.listFiles()) {
    if (s.getName().endsWith(suffix)) {
    files.add(s.getName());
    }
    }
    return files;
    }

    /**
    * 复制整个文件夹的内容(含自身)
    *
    * @param oldPath
    * 准备拷贝的目录
    * @param newPath
    * 指定绝对路径的新目录
    * @return
    */
    public static void copyFolderWithSelf(String oldPath, String newPath) {
    try {
    new File(newPath).mkdirs(); // 如果文件夹不存在 则建立新文件夹
    File dir = new File(oldPath);
    // 目标
    newPath += File.separator + dir.getName();
    File moveDir = new File(newPath);
    if (dir.isDirectory()) {
    if (!moveDir.exists()) {
    moveDir.mkdirs();
    }
    }
    String[] file = dir.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()) { // 如果是子文件夹
    copyFolderWithSelf(oldPath + "/" + file[i], newPath);
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    /**
    * 复制单个文件(可更名复制)
    *
    * @param oldPathFile
    * 准备复制的文件源
    * @param newPathFile
    * 拷贝到新绝对路径带文件名(注:目录路径需带文件名)
    * @return
    */
    public static void CopySingleFile(String oldPathFile, String newPathFile) {
    try {
    int bytesum = 0;
    int byteread = 0;
    File oldfile = new File(oldPathFile);
    if (oldfile.exists()) { // 文件存在时
    InputStream inStream = new FileInputStream(oldPathFile); // 读入原文件
    FileOutputStream fs = new FileOutputStream(newPathFile);
    byte[] buffer = new byte[1444];
    while ((byteread = inStream.read(buffer)) != -1) {
    bytesum += byteread; // 字节数 文件大小
    // System.out.println(bytesum);
    fs.write(buffer, 0, byteread);
    }
    inStream.close();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    /**
    * 复制单个文件(原名复制)
    *
    * @param oldPathFile
    * 准备复制的文件源
    * @param newPathFile
    * 拷贝到新绝对路径带文件名(注:目录路径需带文件名)
    * @return
    */
    public static void CopySingleFileTo(String oldPathFile, String targetPath) {
    try {
    int bytesum = 0;
    int byteread = 0;
    File oldfile = new File(oldPathFile);
    String targetfile = targetPath + File.separator + oldfile.getName();
    if (oldfile.exists()) { // 文件存在时
    InputStream inStream = new FileInputStream(oldPathFile); // 读入原文件
    FileOutputStream fs = new FileOutputStream(targetfile);
    byte[] buffer = new byte[1444];
    while ((byteread = inStream.read(buffer)) != -1) {
    bytesum += byteread; // 字节数 文件大小
    // System.out.println(bytesum);
    fs.write(buffer, 0, byteread);
    }
    inStream.close();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    /**
    * 移动指定文件或文件夹(包括所有文件和子文件夹)
    *
    * @param fromDir
    * 要移动的文件或文件夹
    * @param toDir
    * 目标文件夹
    * @throws Exception
    */
    public static void MoveFolderAndFileWithSelf(String from, String to)
    throws Exception {
    try {
    File dir = new File(from);
    // 目标
    to += File.separator + dir.getName();
    File moveDir = new File(to);
    if (dir.isDirectory()) {
    if (!moveDir.exists()) {
    moveDir.mkdirs();
    }
    } else {
    File tofile = new File(to);
    dir.renameTo(tofile);
    return;
    }

    // System.out.println("dir.isDirectory()"+dir.isDirectory());
    // System.out.println("dir.isFile():"+dir.isFile());

    // 文件一览
    File[] files = dir.listFiles();
    if (files == null)
    return;

    // 文件移动
    for (int i = 0; i < files.length; i++) {
    System.out.println("文件名:" + files[i].getName());
    if (files[i].isDirectory()) {
    MoveFolderAndFileWithSelf(files[i].getPath(), to);
    // 成功,删除原文件
    files[i].delete();
    }
    File moveFile = new File(moveDir.getPath() + File.separator
    + files[i].getName());
    // 目标文件夹下存在的话,删除
    if (moveFile.exists()) {
    moveFile.delete();
    }
    files[i].renameTo(moveFile);
    }
    dir.delete();
    } catch (Exception e) {
    throw e;
    }
    }
    //递归删除文件夹
    public static void deleteFile(String path) {
    File file=new File(path);
    if (file.exists()) {//判断文件是否存在
    if (file.isFile()) {//判断是否是文件
    file.delete();//删除文件
    } else if (file.isDirectory()) {//否则如果它是一个目录
    File[] files = file.listFiles();//声明目录下所有的文件 files[];
    for (int i = 0;i < files.length;i ++) {//遍历目录下所有的文件
    deleteFile(files[i].getAbsolutePath());//把每个文件用这个方法进行迭代
    }
    file.delete();//删除文件夹
    }
    } else {
    System.out.println("所删除的文件不存在");
    }
    }
    /*
    * 判断文件或文件夹是否存在
    *
    * @param:绝对路径
    *
    * @param: true,存在。false 不存在
    */
    public static boolean isExist(String path){
    File file=new File(path);
    if(file.exists())
    return true;
    return false;
    }
    }

  • 相关阅读:
    游戏开发系统功能(9)
    游戏开发创建游戏世界(8)
    游戏开发沟通和开会技巧(7)
    游戏开发策划工作(6)
    游戏开发了解测试工作(4)
    Django:学习笔记(9)——用户身份认证
    Django:学习笔记(8)——视图
    Django:学习笔记(7)——模型进阶
    Django:学习笔记(6)——模型
    Django:学习笔记(5)——会话
  • 原文地址:https://www.cnblogs.com/maybo/p/5182465.html
Copyright © 2020-2023  润新知