网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java代码实现复制文件,JAVA实现文件复制

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

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

创新互联公司是一家专注于网站设计、成都网站设计与策划设计,喀左网站建设哪家好?创新互联公司做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:喀左等地区。喀左做网站价格咨询:18980820575

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

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class Copy {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

if(args.length!=2){

System.out.print("没有输入正确数目的参数,程序退出!");

System.exit(0);

}

File fileS = new File("./"+args[0]);

File fileD = new File("./"+args[1]);

if(fileD.exists())System.out.println("目标文件 "+args[1]+" 已存在!");

byte[] temp = new byte[50];

int totalSize = 0;

try {

FileInputStream fr = new FileInputStream(fileS);

FileOutputStream fo = new FileOutputStream(fileD);

int length = 0;

while((length = fr.read(temp, 0, temp.length)) != -1){

totalSize += length;

fo.write(temp, 0, length);

}

System.out.println("文件 "+args[0]+" 有 "+totalSize+" 个字节");

System.out.println("复制完成!");

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("源文件 "+args[0]+" 不存在!");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

java中怎么复制一个文件的内容

主要是用到java里面的i/o流。代码例子如下:

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

/**

* java读写文件,复制文件

* 读取d:/1.txt文件内容,写入f:/text.txt文件中.

* @author young

*

*/

public class FileWriterTest {

// 读写文件

public static void rwFile(){

FileWriter fw = null;

BufferedReader br = null;

try {

fw = new FileWriter("f:\\text.txt", true);

br = new BufferedReader(new InputStreamReader(

new FileInputStream("d:\\1.txt"), "UTF-8"));

String line = null;

while ((line = br.readLine()) != null) {

System.out.println("文件内容: " + line);

fw.write(line);

fw.flush();

}

br.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (fw != null) {

try {

fw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

public static void main(String[] args) {

rwFile();

}

}

首先在D盘新建文件1.txt,输入任意内容。然后执行java代码即可。

使用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如何拷贝文件到另一个目录下

下面列举出4种方式:

1、使用FileStreams复制

这是最经典的方式将一个文件的内容复制到另一个文件中。 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B。正如你所看到的我们执行几个读和写操作try的数据,所以这应该是一个低效率的,下一个方法我们将看到新的方式。 这是第一个方法的代码:

2、使用FileChannel复制

Java NIO包括transferFrom方法,根据文档应该比文件流复制的速度更快。 这是第二种方法的代码:

3、使用Commons IO复制

Apache Commons IO提供拷贝文件方法在其FileUtils类,可用于复制一个文件到另一个地方。它非常方便使用Apache Commons FileUtils类时,您已经使用您的项目。基本上,这个类使用Java NIO FileChannel内部。 这是第三种方法的代码:

4、使用Java7的Files类复制

如果你有一些经验在Java 7中你可能会知道,可以使用复制方法的Files类文件,从一个文件复制到另一个文件。 这是第四个方法的代码:


网站名称:java代码实现复制文件,JAVA实现文件复制
网站URL:http://bjjierui.cn/article/hsiocj.html

其他资讯