最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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è)小工具類(lèi)。和大家分享,希望大家指正:

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ě)入字符文件的便捷類(lèi)
 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中使用緩存的示例代碼

    Springboot中使用緩存的示例代碼

    這篇文章主要介紹了Springboot中使用緩存的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • 在Java的MyBatis框架中建立接口進(jìn)行CRUD操作的方法

    在Java的MyBatis框架中建立接口進(jìn)行CRUD操作的方法

    這篇文章主要介紹了在Java的MyBatis框架中建立接口進(jìn)行CRUD操作的方法,CRUD是指在做計(jì)算處理時(shí)的增加(Create)、重新取得數(shù)據(jù)(Retrieve)、更新(Update)和刪除(Delete)幾個(gè)單詞的首字母簡(jiǎn)寫(xiě),需要的朋友可以參考下
    2016-04-04
  • 詳解Java如何判斷一個(gè)對(duì)象是否為空

    詳解Java如何判斷一個(gè)對(duì)象是否為空

    我們?cè)趧傞_(kāi)始學(xué)習(xí)Java的時(shí)候,遇到過(guò)最多的異常肯定是臭名昭著的空指針異常(NullPointerException),可以說(shuō)它陪伴了我們整個(gè)初學(xué)階段,那么如何優(yōu)雅的判斷一個(gè)對(duì)象是否為空并且減少空指針異常呢,
    2024-01-01
  • Spring在代碼中獲取bean的幾種方式詳解

    Spring在代碼中獲取bean的幾種方式詳解

    這篇文章主要介紹了Spring在代碼中獲取bean的幾種方式詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • SpringDataJPA在Entity中常用的注解介紹

    SpringDataJPA在Entity中常用的注解介紹

    這篇文章主要介紹了SpringDataJPA在Entity中常用的注解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java 多線程傳值的四種方法

    Java 多線程傳值的四種方法

    這篇文章主要介紹了Java 多線程傳值的四種方法,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-09-09
  • java并發(fā)訪問(wèn)重復(fù)請(qǐng)求過(guò)濾問(wèn)題

    java并發(fā)訪問(wèn)重復(fù)請(qǐng)求過(guò)濾問(wèn)題

    本篇文章給大家分享了關(guān)于java并發(fā)訪問(wèn)重復(fù)請(qǐng)求過(guò)濾的相關(guān)問(wèn)題以及解決方法,對(duì)此有需要的朋友參考學(xué)習(xí)下。
    2018-05-05
  • SpringBoot2.X Devtools熱部署實(shí)現(xiàn)解析

    SpringBoot2.X Devtools熱部署實(shí)現(xiàn)解析

    這篇文章主要介紹了SpringBoot2.X Devtools熱部署實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • SpringBoot項(xiàng)目為何引入大量的starter?如何自定義starter?

    SpringBoot項(xiàng)目為何引入大量的starter?如何自定義starter?

    這篇文章主要介紹了SpringBoot項(xiàng)目為何引入大量的starter?如何自定義starter?文章基于這兩個(gè)問(wèn)題展開(kāi)全文,需要的小伙伴可以參考一下
    2022-04-04
  • Spring 中的 ResourceLoader實(shí)例詳解

    Spring 中的 ResourceLoader實(shí)例詳解

    Spring框架提供了ResourceLoader接口,用于加載資源文件,DefaultResourceLoader是其基本實(shí)現(xiàn),只能加載單個(gè)資源,而ResourcePatternResolver繼承自ResourceLoader,增加了按模式加載多個(gè)資源的能力,感興趣的朋友一起看看吧
    2024-11-11

最新評(píng)論

阆中市| 金阳县| 黄冈市| 宜兴市| 措勤县| 南涧| 灵石县| 凭祥市| 佛坪县| 衡南县| 蓝山县| 晋江市| 荣成市| 个旧市| 和政县| 长顺县| 册亨县| 融水| 陈巴尔虎旗| 宁德市| 浦北县| 澳门| 江城| 洛宁县| 运城市| 广平县| 宁国市| 长兴县| 忻城县| 北宁市| 临邑县| 安多县| 昌乐县| 乌恰县| 东至县| 长治市| 高安市| 溧水县| 磴口县| 东丰县| 福建省|