资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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程序能自动复制U盘上的文件,给代码.

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Scanner;

public class KKKKKKKKKKK {

public static void main(String[] args) throws Exception {

Scanner scan = new Scanner(System.in);

System.out.println("请输入U盘路径:");

String uDisk = scan.nextLine();

File file = new File(uDisk);

if (file.exists() file.isDirectory()) {

System.out.println("请输入目标路径:");

String targetFolder = scan.nextLine();

File target = new File(targetFolder);

if (!target.exists()) {

if (!target.mkdir()) {

throw new Exception("创建目标目录失败");

}

} else {

if (!target.isDirectory()) {

throw new Exception("与目标目录同名的文件已经存在");

}

}

File temp[] = file.listFiles();

if (temp != null temp.length 0) {

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

if (!temp[i].isDirectory()) {

String fileName = temp[i].getName();

File t = new File(targetFolder+File.separator+fileName);

if (!t.createNewFile()) {

throw new Exception("创建输出文件失败");

}

FileOutputStream out = new FileOutputStream(t);

FileInputStream in = new FileInputStream(temp[i]);

byte[] buffer = new byte[256];

while (in.read(buffer) 0) {

out.write(buffer);

}

}

}

}

}

}

}

另外如果你的U盘上有目录,并且也希望考过去的话,要加一个递归函数 ,命令函输入U盘所在的盘符:比如:e:\\,目标目录比如:c:\\abc

急求:JAVA编写复制文件夹的代码

一个简单的方式就是调用cmd命令,使用windows自带的功能来替你完成这个功能

我给你写个例子

import java.io.*;

public class test{

public static void main(String[] args){

BufferedReader in = null;

try{

// 这里你就当作操作对dos一样好了 不过cmd /c 一定不要动

Process pro = Runtime.getRuntime().exec("cmd /c copy d:\\ReadMe.txt e:\\");

in = new BufferedReader(new InputStreamReader(pro.getInputStream()));

String str;

while((str = in.readLine()) != null){

System.out.println(str);

}

}catch(Exception e){

e.printStackTrace();

}finally{

if(in != null){

try{

in.close();

}catch(IOException i){

i.printStackTrace();

}

}

}

}

}

使用Java语言如何实现快速文件复制

使用Java语言如何实现快速文件复制:

代码:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.channels.FileChannel;

public class Test {

public static void main(String[] args){

long start = System.currentTimeMillis();

FileInputStream fileInputStream = null;

FileOutputStream fileOutputStream = null;

FileChannel inFileChannel = null;

FileChannel outFileChannel = null;

try {

fileInputStream = new FileInputStream(new File("C:\\from\\不是闹着玩的.flv"));

fileOutputStream = new FileOutputStream(new File("C:\\to\\不是闹着玩的.flv"));

inFileChannel = fileInputStream.getChannel();

outFileChannel = fileOutputStream.getChannel();

inFileChannel.transferTo(0, inFileChannel.size(), outFileChannel);//连接两个通道,从in通道读取数据写入out通道。

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if(fileInputStream != null){

fileInputStream.close();

}

if(inFileChannel != null){

inFileChannel.close();

}

if(fileOutputStream != null){

fileOutputStream.close();

}

if(outFileChannel != null){

outFileChannel.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

long end = System.currentTimeMillis();

System.out.println("视频文件从“from”文件夹复制到“to”文件需要" + (end - start) + "毫秒。");

}

}

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

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


网页题目:Java复制文件程序代码 编写程序实现文件的复制
URL网址:http://cdkjz.cn/article/hjijpg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220