java拷贝文件,java拷贝文件并创建目录

本文目录一览:

利用JAVA语言编写一个 名为copy的程序 实现文件的拷贝功能,应该怎样做?

import java.io.File;\x0d\x0aimport java.io.FileInputStream;\x0d\x0aimport java.io.FileNotFoundException;\x0d\x0aimport java.io.FileOutputStream;\x0d\x0aimport java.io.IOException;\x0d\x0apublic class Copy {\x0d\x0a/**\x0d\x0a* @param args\x0d\x0a*/\x0d\x0apublic static void main(String[] args) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0aif(args.length!=2){\x0d\x0aSystem.out.print(“没有输入正确数目的参数,程序退出!”);\x0d\x0aSystem.exit(0);\x0d\x0a}\x0d\x0aFile fileS = new File(“./”+args[0]);\x0d\x0aFile fileD = new File(“./”+args[1]);\x0d\x0aif(fileD.exists())System.out.println(“目标文件 “+args[1]+” 已存在!”);\x0d\x0abyte[] temp = new byte[50];\x0d\x0aint totalSize = 0;\x0d\x0atry {\x0d\x0aFileInputStream fr = new FileInputStream(fileS);\x0d\x0aFileOutputStream fo = new FileOutputStream(fileD);\x0d\x0aint length = 0;\x0d\x0awhile((length = fr.read(temp, 0, temp.length)) != -1){\x0d\x0atotalSize += length;\x0d\x0afo.write(temp, 0, length);\x0d\x0a}\x0d\x0aSystem.out.println(“文件 “+args[0]+” 有 “+totalSize+” 个字节”);\x0d\x0aSystem.out.println(“复制完成!”);\x0d\x0a} catch (FileNotFoundException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0aSystem.out.println(“源文件 “+args[0]+” 不存在!”);\x0d\x0a} catch (IOException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}

java怎么实现文件拷贝功能

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class CopyFile {

public boolean copy(String file1,String file2) {

File in=new File(file1);

File out=new File(file2);

if(!in.exists()){

System.out.println(in.getAbsolutePath()+”源文件路径错误!!!”);

return false;

}

else {

System.out.println(“源文件路径”+in.getAbsolutePath());

System.out.println(“目标路径”+out.getAbsolutePath());

}

if(!out.exists())

out.mkdirs();

File[] file=in.listFiles();

FileInputStream fin=null;

FileOutputStream fout=null;

for(int i=0;ifile.length;i++){

if(file[i].isFile()){

try {

fin=new FileInputStream(file[i]);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(“in.name=”+file[i].getName());

try {

fout=new FileOutputStream(new File(file2+”/”+file[i].getName()));

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(file2);

int c;

byte[] b=new byte[1024*5];

try {

while((c=fin.read(b))!=-1){

fout.write(b, 0, c);

System.out.println(“复制文件中!”);

}

——————————注意

fin.close();

fout.flush();

fout.close();

——————————–

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

——————————-注释掉

// return true;

}

else copy(file1+”/”+file[i].getName(),file2+”/”+file[i].getName());

}

return false;

}

public static void main(String[] args) {

CopyFile copyFile = new CopyFile();

copyFile.copy(“E:\\study\\opngl”, “E:\\opengl”);

}

}

java 如何拷贝

package czday0017;

import java.io.*;

import java.util.*;

/** IO 工具类 */

public class IO {

/**

* 获取目录的全部文件

* @param dir

* @return

*/

public static ListFile listFile(File dir){

return null;

}

/**

* 获取目录的全部文件, 指定扩展名的文件

* @param dir

* @return

*/

public static ListFile listFile(

File dir, String ext){

return null;

}

/**

* 递归获取目录的全部文件

* @param dir

* @return

*/

public static ListFile listAll(

File dir){

return null;

}

/**

* 递归获取目录的全部文件, 指定扩展名的文件

* @param dir

* @return

*/

public static ListFile listAll(

File dir, String ext){

return null;

}

/**

* 复制文件

*/

public static void cp(String from, String to)

throws IOException {

cp(new File(from), new File(to));

}

/**

* 复制文件

*/

public static void cp(File from, File to)

throws IOException {

cp(new FileInputStream(from),

new FileOutputStream(to));

}

/**

* 复制文件

*/

public static void cp(InputStream in,

OutputStream out)

throws IOException {

//1K byte 的缓冲区!

byte[] buf = new byte[1024];

int count;

while((count=in.read(buf))!=-1){

System.out.println(count);

out.write(buf, 0, count);

}

in.close();

out.close();

}

/**

* 从流中读取一行文本, 读取到一行的结束为止

* @param in

* @return 一行文本

*/

public static String readLine(

InputStream in, String charset)

throws IOException{

byte[] buf = {};

int b;

while(true){

b = in.read();

if(b==’\n’ || b==’\r’ || b==-1){//编码是否是回车换行

break;

}

buf=Arrays.copyOf(buf, buf.length+1);

buf[buf.length-1]=(byte)b;

}

if(buf.length==0 b==-1)

return null;

return new String(buf,charset);

}

/**

* 读取文件的全部内容到一个byte数组

* 可以缓存一个”小”文件到堆内存中

*/

public static byte[] read(String filename)

throws IOException{

return read(new File(filename));

}

/**

* 读取文件的全部内容到一个byte数组

* 可以缓存一个”小”文件到堆内存中

*/

public static byte[] read(File file)

throws IOException{

//用RAF打开文件

RandomAccessFile raf =

new RandomAccessFile(file, “r”);

//安装文件的长度开辟 缓冲区数组(byte数组)

byte[] buf = new byte[(int)raf.length()];

//读取文件的缓冲区

raf.read(buf);

//关闭文件(RAF)

raf.close();

//返回缓冲区数组引用.

return buf;

}

/**

* 读取文件的全部内容到一个byte数组

* 可以缓存一个”小”文件到堆内存中

* 如: 文件内容: ABC中 读取为: {41, 42, 43, d6, d0}

*/

public static byte[] read(InputStream in)

throws IOException{

byte[] ary = new byte[in.available()];

in.read(ary);

in.close();

return ary;

}

/**

* 连接byte 数组的全部内容为字符串,

* 以hex(十六进制)形式连接

* 如: 数组{0x41, 0x42, 0x43, 0xd6, 0xd0}

* 结果: “[41, 42, 43, d6, d0]”

*/

public static String join(byte[] ary){

if(ary==null || ary.length==0)

return “[]”;

StringBuilder buf =

new StringBuilder();

buf.append(“[“).append(

leftPad(Integer.toHexString(ary[0]0xff),’0′,2));

for(int i=1; iary.length; i++){

String hex=Integer.toHexString(ary[i]0xff);

buf.append(“,”).append(leftPad(hex,’0′,2));

}

buf.append(“]”);

return buf.toString();

}

public static String toBinString(byte[] ary){

if(ary==null || ary.length==0)

return “[]”;

StringBuilder buf =

new StringBuilder();

buf.append(“[“).append(

leftPad(Integer.toBinaryString(ary[0]0xff),’0′,8));

for(int i=1; iary.length; i++){

String hex=Integer.toBinaryString(ary[i]0xff);

buf.append(“,”).append(leftPad(hex,’0′,8));

}

buf.append(“]”);

return buf.toString();

}

/**

* 实现leftPad功能, 对字符串实现左填充

* @param str 被填充字符串: 5

* @param ch 填充字符: #

* @param length 填充以后的长度: 8

* @return “#######5”

*/

public static String leftPad(

String str, char ch, int length){

if(str.length() == length){

return str;

}

char[] chs = new char[length];

Arrays.fill(chs, ch);

System.arraycopy(str.toCharArray(), 0, chs,

length – str.length(), str.length());

return new String(chs);

}

/**

* 将text追加到文件 filename的尾部

* 使用系统默认文本编码

*/

public static void println (

String filename, String text)

throws IOException{

println(new File(filename),text);

}

public static void println(

File file, String text)throws IOException{

OutputStream out = new FileOutputStream(file,true);

println(out, text);

out.close();

}

/**

* 向流中输出一行字符串, 使用默认编码

* 不关闭流

* @param out 目标流

* @param text 文本

* @throws IOException

*/

public static void println(

OutputStream out, String text)throws IOException{

out.write(text.getBytes());

out.write(‘\n’);

}

/**

* 向流中输出一行字符串, 使用指定的编码

* 不关闭流

* @param out 目标流

* @param text 文本

* @param charset 指定的编码

* @throws IOException

*/

public static void println(

OutputStream out, String text, String charset)throws IOException{

out.write(text.getBytes(charset));

out.write(‘\n’);

}

/**

* 文件切分

* @param file

* @param size

* @throws IOException

*/

public static void spilt(String file,int size) throws IOException{

if(size=0){

throw new IllegalArgumentException(“干吗啊!输入有误阿!”);

}

int idx = 0;//文件序号

InputStream in = new BufferedInputStream(new FileInputStream(file));

OutputStream out = new BufferedOutputStream(new FileOutputStream(file+”.”+idx++));

int b;

int count = 0;

while((b = in.read())!=-1){

out.write(b);

count++;

if(count%(size*1024)==0 ){

out.close();

out = new BufferedOutputStream(new FileOutputStream(file+”.”+idx++));

}

}

in.close();

out.close();

}

/**

* 将文件进行连接

* @param filename是一个文件名;如:file.dat.0

*/

public static void join(String file)throws IOException{

String filename = file.substring(0, file.lastIndexOf(“.”));

String num = file.substring(file.lastIndexOf(“.”)+1);

int idx = Integer.parseInt(num);

OutputStream out = new BufferedOutputStream(new FileOutputStream(filename));

File f = new File(filename+”.”+idx++);

while(f.exists()){

InputStream in = new BufferedInputStream(new FileInputStream(f));

cp(in,out);

in.close();

f = new File(filename+”.”+idx++);

}

out.close();

}

/**

* 序列化对象

*/

public static byte[] Serialize(Serializable obj)throws IOException{

return null;

}

public static Object unSerializable(byte[] data)throws IOException{

return null;

}

public static Object clone(Serializable obj)throws IOException{

return unSerializable(Serialize(obj)) ;

}

}

//使用cp工具!

Java怎么实现文件拷贝

工具/原料

一台配置了java环境的电脑

一款适合自己的开发集成环境,这里用的是eclipse Kepler

文件拷贝DEMO

1.首先,理清思路,然后我们再动手操作。

拷贝,有源文件,和目的文件。

如果原文件不存在,提示,报错。

如果目的文件不存在,创建空文件并被覆盖。

如果目的地址,也即目的路径不存在,创建路径。

拷贝,输入流,输出流,关闭流。

拷贝前输出文件大小,计算拷贝大小,比较并核实。输出。

2.首先呢,先判断传参是否完整。

如果不够两个参数,或者多于两个参数,提示错误。

如果目标文件不存在,创建 空文件继续复制。

3.在开始前,输出被拷贝的源文件的大小。

4.获得文件名称,即短名。也即路径下的文件全名(包括文件扩展名)。

5.拷贝的关键,这里用的简单的缓冲流。从源文件到目的文件。

number of bytes copied 即是对拷贝长度的累计,直到拷贝完成,输出。

6.将步骤二中的判断并拷贝文件的代码写在一个main函数中,

执行拷贝,拷贝完成。结果拷贝大小和源文件大小一致,成功。

7.在执行前,记得输入参数。

如果是使用命令提示符,执行 javac CopyFile.java 之后,

执行 java CopyFile [源文件长名] [目的文件长名]

如果是使用的eclipse,在运行前设置一下运行参数,完成后点击运行,如下图。

P.S. 这里面的所谓“长名”是指完整绝对路径+文件名+文件类型扩展名

这里的源文件及目的文件的名称分别为:

E:/IP_Data.rar 和 D:/testFiles/IP_Data.rar

END

怎样用java程序实现文件拷贝

通过输入输出流解决此问题,具体的可以查看JDK的API,实在不会的话,百度一下应该都有一堆这方面的代码。

java如何拷贝文件到另一个目录下

/**

*

复制单个文件

*

@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

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();

}

}

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/308529.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2025-01-03 14:49
下一篇 2025-01-03 14:49

相关推荐

  • Java JsonPath 效率优化指南

    本篇文章将深入探讨Java JsonPath的效率问题,并提供一些优化方案。 一、JsonPath 简介 JsonPath是一个可用于从JSON数据中获取信息的库。它提供了一种DS…

    编程 2025-04-29
  • java client.getacsresponse 编译报错解决方法

    java client.getacsresponse 编译报错是Java编程过程中常见的错误,常见的原因是代码的语法错误、类库依赖问题和编译环境的配置问题。下面将从多个方面进行分析…

    编程 2025-04-29
  • Java Bean加载过程

    Java Bean加载过程涉及到类加载器、反射机制和Java虚拟机的执行过程。在本文中,将从这三个方面详细阐述Java Bean加载的过程。 一、类加载器 类加载器是Java虚拟机…

    编程 2025-04-29
  • Java腾讯云音视频对接

    本文旨在从多个方面详细阐述Java腾讯云音视频对接,提供完整的代码示例。 一、腾讯云音视频介绍 腾讯云音视频服务(Cloud Tencent Real-Time Communica…

    编程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介绍

    本文将详细介绍Java Milvus SearchParam withoutFields的相关知识和用法。 一、什么是Java Milvus SearchParam without…

    编程 2025-04-29
  • Rclone复制所有目录

    如果你需要将本地文件复制到云端,或者从云端复制文件到本地,Rclone是一个非常好的选择。本文将介绍如何使用Rclone复制所有目录。 一、安装Rclone 首先,你需要在你的机器…

    编程 2025-04-29
  • vue下载无后缀名的文件被加上后缀.txt,有后缀名的文件下载正常问题的解决

    本文旨在解决vue下载无后缀名的文件被加上后缀.txt,有后缀名的文件下载正常的问题,提供完整的代码示例供参考。 一、分析问题 首先,需了解vue中下载文件的情况。一般情况下,我们…

    编程 2025-04-29
  • 如何在Java中拼接OBJ格式的文件并生成完整的图像

    OBJ格式是一种用于表示3D对象的标准格式,通常由一组顶点、面和纹理映射坐标组成。在本文中,我们将讨论如何将多个OBJ文件拼接在一起,生成一个完整的3D模型。 一、读取OBJ文件 …

    编程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java语言中的一个版本,于2014年3月18日发布。本文将从多个方面对Java 8中某一周的周一进行详细的阐述。 一、数组处理 Java 8新特性之一是Stream…

    编程 2025-04-29
  • 为什么用cmd运行Java时需要在文件内打开cmd为中心

    在Java开发中,我们经常会使用cmd在命令行窗口运行程序。然而,有时候我们会发现,在运行Java程序时,需要在文件内打开cmd为中心,这让很多开发者感到疑惑,那么,为什么会出现这…

    编程 2025-04-29

发表回复

登录后才能评论