`
iorit2003
  • 浏览: 138679 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

java file or folder

阅读更多

import java.io.*;
public class FileOperate {
    public FileOperate() {
    }

    /**
     * 新建目录
     * @param folderPath String 如 c:/fqf
     * @return boolean
     */
    public void newFolder(String folderPath) {
      try {
        String filePath = folderPath;
        filePath = filePath.toString();
        java.io.File myFilePath = new java.io.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 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 void delFile(String filePathAndName) {
      try {
        String filePath = filePathAndName;
        filePath = filePath.toString();
        java.io.File myDelFile = new java.io.File(filePath);
        myDelFile.delete();

      }
      catch (Exception e) {
        System.out.println("删除文件操作出错");
        e.printStackTrace();

      }

    }

    /**
     * 删除文件夹
     * @param filePathAndName String 文件夹路径及名称 如c:/fqf
     * @param fileContent String
     * @return boolean
     */
    public void delFolder(String folderPath) {
      try {
        delAllFile(folderPath); //删除完里面所有内容
        String filePath = folderPath;
        filePath = filePath.toString();
        java.io.File myFilePath = new java.io.File(filePath);
        myFilePath.delete(); //删除空文件夹

      }
      catch (Exception e) {
        System.out.println("删除文件夹操作出错");
        e.printStackTrace();

      }

    }

    /**
     * 删除文件夹里面的所有文件
     * @param path String 文件夹路径 如 c:/fqf
     */
    public 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 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 inStream
     * @param newPath
     */
    public void copyFile(InputStream inStream, String newPath) {
        try {
          int bytesum = 0;
          int byteread = 0;
         // File oldfile = new File(oldPath);
          if (inStream!=null) { //文件存在时
           // 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 outStream
     * @param fileName
     */
    public void downFile(OutputStream outStream,String fileName){
        try {
            File file=new File(fileName);
            InputStream inPut = new FileInputStream(file);
            byte[] buf = new byte[1024];
            int len = 0;
            while((len = inPut.read(buf)) >0)
            outStream.write(buf,0,len);
            inPut.close();
            outStream.close();
        } catch (IOException e) {
            System.out.println("下载单个文件操作出错");
            e.printStackTrace();
        }
        
      
    }
    /**
     * 复制整个文件夹内容
     * @param oldPath String 原文件路径 如:c:/fqf
     * @param newPath String 复制后路径 如:f:/fqf/ff
     * @return boolean
     */
    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 String 如:c:/fqf.txt
     * @param newPath String 如:d:/fqf.txt
     */
    public void moveFile(String oldPath, String newPath) {
      copyFile(oldPath, newPath);
      delFile(oldPath);

    }

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

    }
  }

 

 

分享到:
评论

相关推荐

    C# to Java 代码转换工具

    Running from the command line: C# to Java Converter can be launched directly for a specific project, folder, or file conversion from the command line. Command line.(命令行执行) 其他一些特点: 1. ...

    java反编工具java反编工具

    To run DJ Java Decompiler setup you must have MS Windows Installer v.1.00 or above installed. (see your \Windows\SYSTEM folder - MSIEXEC.EXE and MSI.DLL). This files are about 2MB. For this reason I ...

    javacv-platform-1.3.3-src

    An implementation of Java SE 7 or newer: OpenJDK http://openjdk.java.net/install/ or Sun JDK http://www.oracle.com/technetwork/java/javase/downloads/ or IBM JDK ...

    Java邮件开发Fundamentals of the JavaMail API

    addition, you will need a development environment such as the JDK 1.1.6+ or the Java 2 Platform, Standard Edition (J2SE) 1.2.x or 1.3.x. A general familiarity with object-oriented programming ...

    tModLoader.Windows.v0.11.6.2.7z

    To install tModLoader, extract the zip achive containing this README.txt file to a temporary folder and then simply run the tModLoaderInstaller.jar file in that folder. Java 1.8 or higher is required ...

    UE(官方下载)

    You can use these functions to insert a file into the current file, delete the active file, send the file through email, or insert a string into the file at every specified increment HTML preview ...

    将Java和Kotlin代码转换成JavaScript、c++、D、c#、PHP、AS3、Dart和Haxe,并在任何地方运行它。还可以使用您喜欢的语言中的JVM代码作为库。- jtransc / jtransc

    into a target programming language / executable bundling all the required dependencies in a single file or folder, without requiring a jitter or an external runtime. Why using JTransc? There are a lot...

    Fortify-SCA-扫描工具指导手册.pdf

    ●扫描目录方式: Audit workbench scan Folder 与其他工具集成: Scan with ANt, Makefile ●编译监控器方式: Fortify SCA Build Monitor FORTIFY Fortify SCA扫描的四个步骤 Fortify SCA扫描总共可以分为四个步骤: ...

    KCFinder(CKEditor的文件管理器) v2.5.1

    KCFinder Features:01 Ajax engine with JSON responses02 Select multiple files with the Ctrl/Command key03 Download multiple files or a folder as single ZIP file04 Clipboard for copying and moving ...

    apktool documentation

    After you find a framework file you could pull it via adb pull /path/to/file or use a file manager application. After you have the file locally, pay attention to how Apktool installs it. The number ...

    Cisco Packet Tracer7.3.0插件(ActivityGrader_x86_7.3.0.0.zip)

    1.8 or above (http://java.com/getjava/) Packet Tracer 7.0 or above Upgrading Activity Grader ------------------------- If there is a previous version of Activity Grader installed, ...

    Cisco Packet Tracer7.3.0插件(ActivityGrader_x86_64_7.3.0.0.zip)

    1.8 or above (http://java.com/getjava/) Packet Tracer 7.0 or above Upgrading Activity Grader ------------------------- If there is a previous version of Activity Grader installed, ...

    路由计算器

    In a terminal make your way to the folder where the Runner.java and RouteCalculator.java are2. Type "Javac *.java" in that folder containing the files a. If you wish to add additional files in this ...

    burp suite 1.7.26 破解版 永不过期 burploader unlimited

    windows_if_you_dont_wanna_install_vcredist is for anyone who don't wana install vcredist, please chose the file for x64 or x86, rename to vcruntime140.dll and copy to BurpUnlimited.jar's folder ...

    开发多媒体播放器

    private void fileOrDir(String path) { File file = new File(path); if (file.isDirectory()) { getFileDir(file.getPath()); } else { openFile(path); } } /** * 取得文件结构的方法 */ ...

    Ajax in action 英文版配书源码.rar

    file in the 'refactored' folder has '_refactored' appended to the title. &lt;br&gt;As a bonus, chapter 10 also has a debug version of the refactored code, that uses the debug console described in ...

    nao机器人java语音源码

    /// Loads a set of voice parameters defined in a xml file contained in the preferences folder.The name of the xml file must begin with ALTextToSpeech_Voice_ /// /// &lt;param name="pPreferenceName"&gt; ...

    pdf2txt:将.pdf转换为.txt文件

    path the path for a folder or a .pdf file -h, --help display the help menu -o, --output=&lt;output&gt; the output folder Copyright(c) 2021 ISELab Dearborn 例如: 您可以传递一个文件夹作为输入 $ java -jar ...

    Equaliser:检查XML文件中的输入,并标记违反一组有效模式的元素+属性组合

    : 均衡器 程序是一个Java命令行工具,用于根据一组提供的模式检查一组XML文件中的所有元素和属性组合... 命令行语法为:java -jar Equaliser.jar [-R] [-l] [-p pat-file] [-r补救文件] xml-file-or-folder 地点 默认

    SocketIO包括客户端和服务器端代码

    1. Build or install Netty-socketio lib to your maven repository. `mvn clean install` 2. Switch to /server folder and build server by maven. 3. Run server by command `mvn exec:java` 4. Run ...

Global site tag (gtag.js) - Google Analytics