Java中实现复制文件或文件夹

文件操作工具类-java 文件夹的复制,文件的复制,文件的读取,文件的写入,文件夹的复制,文件的复制,文件的读取,文件的写入,文件夹的复制,文件的复制,文件的读取,文件的写入 立即下载

原文地址:http://blog.youkuaiyun.com/Dream_JavaWorld/article/details/3682075

拷贝一个文件的算法比较简单,当然,可以对它进行优化,比如使用缓冲流,提高读写数据的效率等。但是在复制文件夹时,则需要利用Flie类在目标文件夹中创建相应的目录,并且使用递归方法。

[java]  view plain copy print ?
  1. import java.io.*;  
  2. /** 
  3.  * 复制文件夹或文件夹 
  4.  */  
  5. public class CopyDirectory {  
  6.     // 源文件夹   
  7.     static String url1 = "f:/photos";  
  8.     // 目标文件夹   
  9.     static String url2 = "d:/tempPhotos";  
  10.     public static void main(String args[]) throws IOException {  
  11.         // 创建目标文件夹   
  12.         (new File(url2)).mkdirs();  
  13.         // 获取源文件夹当前下的文件或目录   
  14.         File[] file = (new File(url1)).listFiles();  
  15.         for (int i = 0; i < file.length; i++) {  
  16.             if (file[i].isFile()) {  
  17.                 // 复制文件   
  18.                 copyFile(file[i],new File(url2+file[i].getName()));  
  19.             }  
  20.             if (file[i].isDirectory()) {  
  21.                 // 复制目录   
  22.                 String sourceDir=url1+File.separator+file[i].getName();  
  23.                 String targetDir=url2+File.separator+file[i].getName();  
  24.                 copyDirectiory(sourceDir, targetDir);  
  25.             }  
  26.         }  
  27.     }  
  28. // 复制文件   
  29. public static void copyFile(File sourceFile,File targetFile)   
  30. throws IOException{  
  31.         // 新建文件输入流并对它进行缓冲   
  32.         FileInputStream input = new FileInputStream(sourceFile);  
  33.         BufferedInputStream inBuff=new BufferedInputStream(input);  
  34.   
  35.         // 新建文件输出流并对它进行缓冲   
  36.         FileOutputStream output = new FileOutputStream(targetFile);  
  37.         BufferedOutputStream outBuff=new BufferedOutputStream(output);  
  38.           
  39.         // 缓冲数组   
  40.         byte[] b = new byte[1024 * 5];  
  41.         int len;  
  42.         while ((len =inBuff.read(b)) != -1) {  
  43.             outBuff.write(b, 0, len);  
  44.         }  
  45.         // 刷新此缓冲的输出流   
  46.         outBuff.flush();  
  47.           
  48.         //关闭流   
  49.         inBuff.close();  
  50.         outBuff.close();  
  51.         output.close();  
  52.         input.close();  
  53.     }  
  54.     // 复制文件夹   
  55.     public static void copyDirectiory(String sourceDir, String targetDir)  
  56.             throws IOException {  
  57.         // 新建目标目录   
  58.         (new File(targetDir)).mkdirs();  
  59.         // 获取源文件夹当前下的文件或目录   
  60.         File[] file = (new File(sourceDir)).listFiles();  
  61.         for (int i = 0; i < file.length; i++) {  
  62.             if (file[i].isFile()) {  
  63.                 // 源文件   
  64.                 File sourceFile=file[i];  
  65.                 // 目标文件   
  66.                File targetFile=new   
  67. File(new File(targetDir).getAbsolutePath()  
  68. +File.separator+file[i].getName());  
  69.                 copyFile(sourceFile,targetFile);  
  70.             }  
  71.             if (file[i].isDirectory()) {  
  72.                 // 准备复制的源文件夹   
  73.                 String dir1=sourceDir + "/" + file[i].getName();  
  74.                 // 准备复制的目标文件夹   
  75.                 String dir2=targetDir + "/"+ file[i].getName();  
  76.                 copyDirectiory(dir1, dir2);  
  77.             }  
  78.         }  
  79.     }  
  80. }  

CentOS7 编译升级GCC至9.3.0 1)下载GCC9.2.0源码并解压 从GCC官方FTP下载地址可以看到GCC最新版本为9.3.0。由于官网下载速度慢,实际上用清华大学的镜像站进行下载,代码如下: wget -c /opt/tmp/ https://mirrors.tuna.tsinghua.edu.cn/gnu/gcc/gcc-9.3.0/gcc-9.3.0.tar.gz #考虑到安装失败的可能性,创建一个临时文件夹来安装GCC... 阅读详情

相关推荐

PostgreSQL隐式类型转换全解析:告别operator does not exist

强类型数据库是现代数据系统的基石,它在带来严谨性的同时,也常常让开发者在类型不匹配报错前束手无策。当一条SQL因bigint与text比较而抛出operator does not exist时,根源往往在于对数据库隐式转换规则的误解。PostgreSQL通过pg_cast系统表将转换划分为隐式、赋值与显式三个级别,并依据操作符解析逻辑决定是否自动提升类型。理解这些机制,不仅能快速定位JOIN、UNION、函数调用等场景下的类型错误,还能帮助开发者合理使用EXPLAIN VERBOSE、pg_typeof等工

weixin_29587015的博客 304

java将指定目录下的文件复制到目标文件夹

讲解递归和java的基础文件操作

猪头泽兜兜的 5535

常用的集成TTL门电路

本篇主要分析了常用的逻辑门电路与非门,详细分析了电源原理。值得收藏。

ManagerUser的博客 5086

Java复制文件或目录方法

【代码】Java复制文件或目录方法。

//Case 2651

从 Classic ABAP 到 ABAP Cloud:一份面向开发者的 SAP 学习路线图与实战指南

ABAP技术演进路线:从语法到架构思维 本文系统梳理了现代ABAP开发者的能力成长路径。在S/4HANA与云时代,ABAP已从传统过程式语言转变为支持面向对象、测试驱动和云原生开发的现代技术栈。学习路线应围绕四大核心维度展开:1) 语言基础(ABAP OO/Modern ABAP);2) 工程化实践(ABAP Unit/ATC/Clean ABAP);3) 数据建模(CDS/AMDP);4) 服务化架构(Gateway/OData/Fiori)。文章强调以交付形态倒推技术栈,通过真实案例展示了如何将零散技术

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 360

UG NX二次开发基础篇-外部模式与DLL内部模式(二 DLL内部模式)

通常情况下,我们希望我们的外挂,在UG启动的时候,就自动加载我们的外挂,并以菜单和工具条的方式,显示在UG上面。当然,这也是市面上所有外挂的操作,因为您不可能要求你的用户,以手工加载的方式,去调用你的外挂吧。在上一篇中,有粉丝留言说,启动 bat 文件,启动EXE外挂,会提示无法找到DLL的错误,这是因为UG的环境变量设置有问题,这里,我们把BAT文件作一个修改,直接改为实际目录。EXE外部模式,还有很多部分需要添加的,如参数的传递、输出消息的显示、UG操作的消息显示,等等。Dll执行结束后,会调用。

u014657217的博客 1653

Java 实现文件复制及文件夹复制

在Java中,有多种方法可以实现文件的复制。以下是几种常用的方式:使用字节流进行复制:通过FileInputStream和FileOutputStream分别创建源文件和目标文件的输入输出流,然后通过循环读取源文件内容,并将数据写入目标文件中进行复制。可以使用字节数组作为缓冲区,提高复制效率。需要手动处理流的打开、关闭和异常处理。使用字符流进行复制:通过FileReader和FileWriter分别创建源文件和目标文件的字符流,然后通过循环读取源文件内容,并将数据写入目标文件中进行复制。

qq_57570052的博客 6961

Allan方差这个工具,怎么把它用的更顺手?(附Allan方差、动态Allan方差代码)

本文详细说明了Allan方差和动态Allan方差的计算过程,并通过递归运算和时间序列选择两种算法提高动态Allan方差的计算效率。文末提供了快速动态Allan方差计算代码。

victoryqian528的博客 372

java复制文件或文件夹_Java中实现复制文件或文件夹

拷贝一个文件的算法比较简单,当然,可以对它进行优化,比如使用缓冲流,提高读写数据的效率等。但是在复制文件夹时,则需要利用Flie类在目标文件夹中创建相应的目录,并且使用递归方法。[java] view plaincopyprint?import java.io.*;/*** 复制文件夹或文件夹*/public class CopyDirectory {// 源文件夹static String ur...

weixin_39590868的博客 455

十三种通信接口芯片——《器件手册--通信接口芯片》

通信接口芯片用于实现差分信号的传输,具有抗干扰能力强、传输距离远等优点。RS485支持多点通信,可连接多个收发器,而RS422支持单向多点通信。广泛应用于工业自动化、过程控制、仪器仪表、楼宇自动化(如暖通空调、保安系统)、电机控制等领域。ST3485EBDRTP8485ESR用于实现串行通信,支持点对点通信,传输距离相对较短,数据速率较低。常用于计算机与外设(如打印机、调制解调器)之间的通信。如芯力特的SIT3232,可作为ST3232的替代品。

XU157303764的博客 4271

淋巴结病理数字玻片读取与处理代码

Whole-slide images are generally stored in a multi-resolution pyramid structure. 首先安装openslide模块 pip install openslide-python import os.path as osp import openslide from pathlib import Path...

猫头军师兵书 1275

Android拷贝文件夹和文件方法

实现安卓拷贝文件、文件夹、创建文件、读取文件内容

qq_24688461的博客 1127

卸载python弹框报错:Could not set file security for file ‘D:\Config.Msi\abd5.rbf‘. Error: 5. Verify...如何解决?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 6478

java复制文件夹到指定目录_详细介绍Java实现文件或文件夹的复制到指定目录的实例...

本篇文章主要介绍了Java实现文件或文件夹的复制到指定目录实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。整理文档,搜刮出一个Java实现文件或文件夹的复制到指定目录的代码,稍微整理精简一下做下分享。import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;public class ...

weixin_33968957的博客 1403

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

android-Java中实现复制文件或文件夹 - 随心

import java.io.*;   /**   * 复制文件夹或文件夹   */   public class CopyDirectory {       // 源文件夹        static String url1 = "f:/photos";       // 目标文件夹        static String url2 = "d:/tempPhotos";

Super_Level的专栏 1万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

java 保存文件到指定目录下_Java实现文件或文件夹的复制到指定目录实例

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;public class Test {private static int a = 5;public static void main(String[] args) {//需要复制的目标文件或目标文件夹String pathname ...

weixin_30579719的博客 3514

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

java写文件到目录_Java实现文件或文件夹的复制到指定目录实例

整理文档,搜刮出一个Java实现文件或文件夹的复制到指定目录的代码,稍微整理精简一下做下分享。import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;public class Test {private static int a = 5;public static void main(St...

weixin_31885123的博客 871

Java实现文件或文件夹的复制到指定目录

此程序可以将指定的文件夹及其子目录复制到指定文件下去。 可玩性较强! import java.io.FileOutputStream; import java.io.FileInputStream; import java.util.Scanner; import java.io.*; public class Copy{ public static void main(String[] args) throws IOException { Scanner sc =new Scanne

Harry博客 8475

java中如何复制文件_Java中实现复制文件或文件夹

[java] view plaincopyprint?import java.io.;/复制文件夹或文件夹/public classCopyDirectory {//源文件夹static String url1 = f:/photos;//目标文件夹static String url2 = d:/tempPhotos;public static void main(String args[]) t...

weixin_34662764的博客 489

java复制文件或文件夹_java中复制文件或文件夹实现

Java中实现复制文件或文件夹拷贝一个文件的算法比较简单,当然,可以对它进行优化,比如使用缓冲流,提高读写数据的效率等。但是在复制文件夹时,则需要利用Flie类在目标文件夹中创建相应的目录,并且使用递归方法。import java.io.*;/*** 复制文件夹或文件夹*/public class CopyDirectory {// 源文件夹static String url1 = "f:/pho...

weixin_35355560的博客 382

java中怎么复制文件夹_Java中实现复制文件或文件夹

直接代码,import java.io.*;/*** 复制文件夹或文件夹*/public class CopyDirectory {// 源文件夹static String url1 = "f:/photos";// 目标文件夹static String url2 = "d:/tempPhotos";public static void main(String args[]) throws IOE...

weixin_42370434的博客 190

java怎么复制单击文件夹_Java中实现复制文件或文件夹

[java] view plaincopyprint?import java.io.;/复制文件夹或文件夹/public classCopyDirectory {//源文件夹static String url1 = f:/photos;//目标文件夹static String url2 = d:/tempPhotos;public static void main(String args[]) t...

weixin_32768455的博客 79

java把文件复制到文件夹_Java实现文件或文件夹的复制到指定目录

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;public class Test {private static int a = 5;public static void main(String[] args) {//需要复制的目标文件或目标文件夹String pathname ...

weixin_36121078的博客 876
上一篇: keystore相关 (java)
下一篇: Q&A for cert
androidboy365
博客等级 码龄16年 35粉丝 32原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值