黑马程序员技术交流社区
标题:
拷贝文件
[打印本页]
作者:
i'm_fine_
时间:
2013-10-26 22:48
标题:
拷贝文件
如何拷贝一个文件
作者:
张运
时间:
2013-10-26 22:57
import java.io.*;
class CopyText
{
public static void main(String[] args) throws IOException
{
copy();
}
public static void copy()
{
FileWriter fw = null;
FileReader fr = null;
try
{
fw = new FileWriter("SystemDemo_copy.txt");
fr = new FileReader("SystemDemo.java");
char[] buf = new char[1024];
int len = 0;
while((len=fr.read(buf))!=-1)
{
fw.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
if(fr!=null)
try
{
fr.close();
}
catch (IOException e)
{
}
if(fw!=null)
try
{
fw.close();
}
catch (IOException e)
{
}
}
}
}
复制代码
临时写的demo可以参考一下
作者:
周学彬
时间:
2013-10-26 23:01
给你提供一个最简单的方法:
import java.io.*;
public class CopyMp3Demo {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("E:\\cancion_copy_3.mp3");
FileInputStream fis = new FileInputStream("E:\\cancion.mp3");
InputStreamReader fisr = new InputStreamReader(fis);
OutputStreamWriter fosw = new OutputStreamWriter(fos);
int ch = -1;
while ((ch = fisr.read()) != -1) {
fosw.write(ch);
}
BufferedInputStream bufr = new BufferedInputStream(fis);
BufferedOutputStream bufw = new BufferedOutputStream(fos);
byte[] buf = new byte[1024];
int num = 0;
while ((num = bufr.read(buf)) != -1) {
bufw.write(buf, 0, num);
}
bufr.close();
bufw.close();
}
}
复制代码
这里要拷贝一个字节流文件。先创建一对字节流文件输入和输出的对象,然后传递相应的路径和文件名,并创建两个缓冲区数据流对象,将这两个字节流对象传递给两个缓冲区数据流类的构造函数。然后使用两个缓冲区数据类的方法去实现读取和写入的方法就行了
作者:
完美恋爱
时间:
2013-10-26 23:22
BufferedReader bufr = new BufferedReader(new FileReader("g:\\filewriter.txt"));
BufferedWriter bufw = new BufferedWriter(new FileWriter("d:\\copyfilewriter.txt"));
String line = null;
while((line = bufr.readLine())!=null)
{
burw.write(line);
bufw.newLine();
bufw.flush();
}
bufr.close();
bufw.close();
作者:
Yuan先生
时间:
2013-10-27 00:09
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args){
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(
new FileInputStream("src/com/itheima/FileCopy.java"));
out = new BufferedOutputStream(
new FileOutputStream("src/com/itheima/FileCopy.txt"));
byte[] data = new byte[1024];
while(true){
int len = in.read(data);
//out.flush();
if(len == -1)
break;
out.write(data, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
复制代码
作者:
起猿
时间:
2013-10-27 00:09
/*附上自己刚完成的题目,里面有些注释,不懂的可以给我留言*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
/**
* 第七题:使用带缓冲功能的字节流复制文件。
*
* 思路:
* 1、copyFile函数接收由调用者传递的两个参数(要复制的文件完整路径file和要复制到哪里的目录path)。
* 2、创建file和path的实例对象,并进行一系列检测,避免复制后覆盖同名文件。并创建目标文件的完整路径。
* 3、检测完毕后就创建输入流和输出流。为了提高效率,加入缓冲技术。
* 4、(附加)显示一些格式化信息、记录程序执行时间等。
* 5、因为是字节流,所以不能使用读一行的方法,只能一次读一个字节,效率低,如果文件不是很大,可以建立一个数组,把
* 输入流中的数据全部读到数组中,在把数组中的数据一次性写入输出流,但如果数据太大,会出现内存溢出,所以进行文件
* 大小的判断,小于等于200MB用新增数组的方法,大于200MB用一次读一个方法,这样可以尽量提高效率。
* 6、复制完成,关闭资源
*
* @author 李志朋
*/
public class Test7{
/**
* 复制文件
* @param file 需要复制文件的完整路径
* @param path 要复制到的目标目录
*/
public static void copyFile(String file,String path){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
File copyFile = new File(file);
File savePath = new File(path);
//如果要复制的文件不存在或者不是文件,发出提示并退出程序
if (!(copyFile.exists() && copyFile.isFile())){
System.out.println("无效的文件,请检查");
System.exit(0);
}
//如果要保存到的目录不存在,则创建该目录
if (!(savePath.isDirectory())){
System.out.println("你指定的目录不存在,将自动创建!");
savePath.mkdirs();
}
//创建目标文件完整路径。
File saveFile = new File(savePath+"\\"+copyFile.getName());
//如果saveFile是一个文件,说明有同名文件存在,则提示并退出程序,避免覆盖同名文件。
if (saveFile.isFile()){
System.out.println("注意!该目录下有同名文件。");
System.exit(0);
}
//创建输入流和输出流。
bis = new BufferedInputStream(new FileInputStream(copyFile));
bos = new BufferedOutputStream(new FileOutputStream(saveFile));
//获取输入流中的的字节数。
long len = bis.available();
//格式化显示文件大小,保留1位小数
DecimalFormat df = new DecimalFormat(".0");
String size = null;
if(len > 1024*1024*200){
System.out.println("要复制的文件超过200MB,不能复制!");
}else if(len > 1024*1024){
size = df.format(len/1024/1024) + " MB";
}else if(len > 1024){
size = df.format(len/1024) + " KB";
}else{
size = len + " 字节";
}
System.out.println("文件大小:" + size);
System.out.println("复制中...");
//记录复制开始时毫秒值
long start = System.currentTimeMillis();
//如果文件大于200MB,用一次读一个字节方式
//否则就用数组一次性读取方式复制
if(len > 1024*1024*200){
int by;
while((by=bis.read())!=-1){
bos.write(by);
}
}else{
//创建数组缓冲区,指定大小为输入流中的的字节数len。
byte[] bytes = new byte[(int)len];
bis.read(bytes); //从输入流中读取所有数据存储到数组中。
bos.write(bytes); //将数组中的所有数据写入的输出流缓冲区中。
}
//记录复制结束精辟时毫秒值
long end = System.currentTimeMillis();
System.out.println("复制完成");
System.out.println("耗时:"+(end-start)+"毫秒");
}
catch(IOException e){
throw new RuntimeException("复制文件失败");
}
//关闭流资源
finally{
try{
if (bis!=null){
bis.close();
}
}
catch(IOException e){
throw new RuntimeException("输出流关闭失败");
}
try{
if (bos!=null){
bos.close();
}
}
catch(IOException e){
throw new RuntimeException("输出流关闭失败");
}
}
}
/**
* 测试
* @param args
*/
public static void main(String[] args){
//源文件
//String copyFile = "E:\\abc.txt";
String copyFile = "E:\\音乐\\MV\\第一部 T-ara - Day By Day.kux";
//指定目标路径,没有责创建
String savePath = "F:\\abc";
copyFile(copyFile,savePath);
}
}
复制代码
作者:
wuchuang1992
时间:
2013-10-27 12:34
/* 如果aa.txt文件为空,此时不能进行复制
*字符流类型的文件复制
* 先从简单的入手
*/
import java.io.*;
public class FileCopy1
{
public static void main(String args[])
{
try
{
File f= new File("C:/aa.txt");
if(f.exists())
{
FileReader fr = new FileReader(f);
FileWriter fw = new FileWriter("C:/bb.txt");
char []b= new char[1024];
int len= fr.read(b);
fw.write(b,0,len);
fr.close();
fw.close();
System.out.println("文件复制成功!");
}
else
{System.out.println("文件不存在");}
}
catch(Exception e)
{System.out.println(e.getMessage());}
}
}
复制代码
作者:
To
时间:
2013-10-27 16:38
楼主你好,如果问题已解决请将帖子状态修改为提问结束,
如果未解决请继续追问,谢谢合作
修改方法请看解释帖:
http://bbs.itheima.com/thread-89313-1-1.html
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2