亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java文件和目錄的增刪復(fù)制

 更新時(shí)間:2017年06月16日 08:33:23   作者:tlnshuju  
這篇文章主要為大家詳細(xì)介紹了java文件和目錄的增刪復(fù)制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在使用java進(jìn)行開(kāi)發(fā)時(shí)常常會(huì)用到文件和目錄的增刪復(fù)制等方法。我寫(xiě)了一個(gè)小工具類。和大家分享,希望大家指正:

package com.wangpeng.utill;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.PrintWriter;

/**
 * @author wangpeng
 * 
 */
public class ToolOfFile {

 /**
 * 創(chuàng)建目錄
 * 
 * @param folderPath
 *      目錄目錄
 * @throws Exception
 */
 public static void newFolder(String folderPath) throws Exception {
 try {
  java.io.File myFolder = new java.io.File(folderPath);
  if (!myFolder.exists()) {
  myFolder.mkdir();
  }
 } catch (Exception e) {
  throw e;
 }
 }

 /**
 * 創(chuàng)建文件
 * 
 * @param filePath
 *      文件路徑
 * @throws Exception
 */
 public static void newFile(String filePath) throws Exception {
 try {
  File myFile = new File(filePath);
  if (!myFile.exists()) {
  myFile.createNewFile();
  }
 } catch (Exception e) {
  throw e;
 }
 }

 /**
 * 創(chuàng)建文件,并寫(xiě)入內(nèi)容
 * 
 * @param filePath
 *      文件路徑
 * @param fileContent
 *      被寫(xiě)入的文件內(nèi)容
 * @throws Exception
 */
 public static void newFile(String filePath, String fileContent)
  throws Exception {
 // 用來(lái)寫(xiě)入字符文件的便捷類
 FileWriter fileWriter = null;
 // 向文本輸出流打印對(duì)象的格式化表示形式,使用指定文件創(chuàng)建不具有自己主動(dòng)行刷新的新
 PrintWriter printWriter = null;
 try {
  File myFile = new File(filePath);
  if (!myFile.exists()) {
  myFile.createNewFile();
  }

  fileWriter = new FileWriter(myFile);
  printWriter = new PrintWriter(fileWriter);

  printWriter.print(fileContent);
  printWriter.flush();
 } catch (Exception e) {
  throw e;
 } finally {
  if (printWriter != null) {
  printWriter.close();
  }
  if (fileWriter != null) {
  fileWriter.close();
  }
 }
 }

 /**
 * 復(fù)制文件
 * 
 * @param oldPath
 *      被拷貝的文件
 * @param newPath
 *      復(fù)制到的文件
 * @throws Exception
 */
 public static void copyFile(String oldPath, String newPath)
  throws Exception {
 InputStream inStream = null;
 FileOutputStream outStream = null;
 try {
  int byteread = 0;
  File oldfile = new File(oldPath);
  // 文件存在時(shí)
  if (oldfile.exists()) {
  inStream = new FileInputStream(oldfile);
  outStream = new FileOutputStream(newPath);

  byte[] buffer = new byte[1444];
  while ((byteread = inStream.read(buffer)) != -1) {
   outStream.write(buffer, 0, byteread);
  }
  outStream.flush();
  }
 } catch (Exception e) {
  throw e;
 } finally {
  if (outStream != null) {
  outStream.close();
  }
  if (inStream != null) {
  inStream.close();
  }
 }
 }

 /**
 * 復(fù)制文件
 * @param inStream 被拷貝的文件的輸入流
 * @param newPath 被復(fù)制到的目標(biāo)
 * @throws Exception
 */
 public static void copyFile(InputStream inStream, String newPath)
  throws Exception {
 FileOutputStream outStream = null;
 try {
  int byteread = 0;
  outStream = new FileOutputStream(newPath);
  byte[] buffer = new byte[1444];
  while ((byteread = inStream.read(buffer)) != -1) {
  outStream.write(buffer, 0, byteread);
  }
  outStream.flush();
 } catch (Exception e) {
  throw e;
 } finally {
  if (outStream != null) {
  outStream.close();
  }
  if (inStream != null) {
  inStream.close();
  }
 }
 }

 /**
 * 復(fù)制目錄
 * 
 * @param oldPath
 *      被復(fù)制的目錄路徑
 * @param newPath
 *      被復(fù)制到的目錄路徑
 * @throws Exception
 */
 public static void copyFolder(String oldPath, String newPath)
  throws Exception {
 try {
  (new File(newPath)).mkdirs(); // 假設(shè)目錄不存在 則建立新目錄
  File a = new File(oldPath);
  String[] file = a.list();
  File tempIn = null;
  for (int i = 0; i < file.length; i++) {
  if (oldPath.endsWith(File.separator)) {
   tempIn = new File(oldPath + file[i]);
  } else {
   tempIn = new File(oldPath + File.separator + file[i]);
  }

  if (tempIn.isFile()) {
   copyFile(tempIn.getAbsolutePath(),
    newPath + "/" + (tempIn.getName()).toString());
  } else if (tempIn.isDirectory()) {// 假設(shè)是子目錄
   copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
  }
  }
 } catch (Exception e) {
  throw e;
 }
 }

 /**
 * 刪除文件
 * 
 * @param filePathAndName
 */
 public static void delFileX(String filePathAndName) {
 File myDelFile = new File(filePathAndName);
 myDelFile.delete();
 }

 /**
 * 刪除目錄
 * 
 * @param path
 */
 public static void delForder(String path) {
 File delDir = new File(path);
 if (!delDir.exists()) {
  return;
 }
 if (!delDir.isDirectory()) {
  return;
 }
 String[] tempList = delDir.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();
  } else if (temp.isDirectory()) {
  // 刪除完里面全部?jī)?nèi)容
  delForder(path + "/" + tempList[i]);
  }
 }
 delDir.delete();
 }

 public static void main(String[] args) {
 String oldPath = "F:/test/aaa/";
 String newPath = "F:/test/bbb/";

 try {
  // ToolOfFile.newFolder("F:/test/hello/");
  // ToolOfFile.newFile("F:/test/hello/world.txt","我愛(ài)你,the world!
");
  ToolOfFile.copyFolder(oldPath, newPath);
  // ToolOfFile.delForder("F:/test/hello");
 } catch (Exception e) {
  e.printStackTrace();
 }
 System.out.println("OK");
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot框架打包體積簡(jiǎn)化過(guò)程圖解

    SpringBoot框架打包體積簡(jiǎn)化過(guò)程圖解

    這篇文章主要介紹了SpringBoot框架打包體積簡(jiǎn)化過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • 一文吃透Spring集成MyBatis

    一文吃透Spring集成MyBatis

    spring能集成很多的框架,是spring一個(gè)優(yōu)勢(shì)功能,通過(guò)集成功能,讓開(kāi)發(fā)人員使用其他框架更方便,本文將給大家詳細(xì)介紹Spring如何集成MyBatis,,需要的朋友可以參考下
    2023-05-05
  • Java中Thread和Runnable創(chuàng)建線程的方式對(duì)比

    Java中Thread和Runnable創(chuàng)建線程的方式對(duì)比

    本文主要介紹了Java中Thread和Runnable創(chuàng)建線程的方式對(duì)比,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Java多線程+鎖機(jī)制實(shí)現(xiàn)簡(jiǎn)單模擬搶票的項(xiàng)目實(shí)踐

    Java多線程+鎖機(jī)制實(shí)現(xiàn)簡(jiǎn)單模擬搶票的項(xiàng)目實(shí)踐

    鎖是一種同步機(jī)制,用于控制對(duì)共享資源的訪問(wèn),在線程獲取到鎖對(duì)象后,可以執(zhí)行搶票操作,本文主要介紹了Java多線程+鎖機(jī)制實(shí)現(xiàn)簡(jiǎn)單模擬搶票的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • 詳解spring boot rest例子

    詳解spring boot rest例子

    這篇文章主要介紹了詳解spring boot rest例子,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • Java設(shè)計(jì)模式之適配器模式的示例詳解

    Java設(shè)計(jì)模式之適配器模式的示例詳解

    適配器模式,即將某個(gè)類的接口轉(zhuǎn)換成客戶端期望的另一個(gè)接口的表示,主要目的是實(shí)現(xiàn)兼容性,讓原本因?yàn)榻涌诓黄ヅ?,沒(méi)辦法一起工作的兩個(gè)類,可以協(xié)同工作。本文將通過(guò)示例詳細(xì)介紹適配器模式,需要的可以參考一下
    2022-08-08
  • java IO實(shí)現(xiàn)電腦搜索、刪除功能的實(shí)例

    java IO實(shí)現(xiàn)電腦搜索、刪除功能的實(shí)例

    下面小編就為大家?guī)?lái)一篇java IO實(shí)現(xiàn)電腦搜索、刪除功能的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • java TreeMap源碼解析詳解

    java TreeMap源碼解析詳解

    這篇文章主要介紹了java TreeMap源碼解析詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 詳解Mybatis逆向工程中使用Mysql8.0版本驅(qū)動(dòng)遇到的問(wèn)題

    詳解Mybatis逆向工程中使用Mysql8.0版本驅(qū)動(dòng)遇到的問(wèn)題

    今天在使用 8.0.12 版的 mysql 驅(qū)動(dòng)時(shí)遇到了各種各樣的坑。這篇文章主要介紹了詳解Mybatis逆向工程中使用Mysql8.0版本驅(qū)動(dòng)遇到的問(wèn)題,感興趣的小伙伴們可以參考一下
    2018-10-10
  • IDEA插件之mybatisx?插件使用教程

    IDEA插件之mybatisx?插件使用教程

    這篇文章主要介紹了mybatisx?插件使用教程,包括插件安裝自動(dòng)生成代碼的相關(guān)知識(shí),本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05

最新評(píng)論