A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 邱成 中级黑马   /  2012-7-31 12:59  /  1861 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner; public class CopyTest {
public static void main(String[] args) throws IOException {
Scanner input=new Scanner(System.in);
String str,straim;
System.out.print("请输入您要拷贝的源文件:");
str=input.next();
File file=new File(str);
if(file.exists()){
System.out.print("请输入您要拷贝的目标位置:");
straim=input.next();
}
else{
System.out.println("源文件未找到!");
return;
}
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
FileWriter fw=new FileWriter(straim);
BufferedWriter bw=new BufferedWriter(fw);
String line=br.readLine();
while(line!=null){
bw.write(line);
br.readLine();
}
bw.flush();
fw.flush();
fr.close();
br.close();
fw.close();
bw.close();
} }



6 个回复

倒序浏览
我改了一处,可以了,失败的原因是你br.readLine()后没有将读取到的数据交给line,line永远不为空,导致了死循环
  1. package test4;

  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.Scanner;

  9. public class Test3 {

  10.         public static void main(String[] args) throws IOException {
  11.                 Scanner input = new Scanner(System.in);
  12.                 String str, straim;
  13.                
  14.                 System.out.print("请输入您要拷贝的源文件:");
  15.                 str = input.next();
  16.                 File file = new File(str);
  17.                 if (file.exists()) {
  18.                         System.out.print("请输入您要拷贝的目标位置:");
  19.                         straim = input.next();
  20.                 } else {
  21.                         System.out.println("源文件未找到!");
  22.                         return;
  23.                 }
  24.                
  25.                 FileReader fr = new FileReader(file);
  26.                 BufferedReader br = new BufferedReader(fr);
  27.                 FileWriter fw = new FileWriter(straim);
  28.                 BufferedWriter bw = new BufferedWriter(fw);
  29.                
  30.                 String line = br.readLine();
  31.                 while (line != null) {
  32.                         bw.write(line);
  33.                         // 楼主你在这里出错了,br.readLine()读取数据后你什么都没做,你应该把数据交给line才对啊
  34.                         // br.readLine(); ——————》 改成下面的就可以了
  35.                         line = br.readLine();
  36.                 }
  37.                
  38.                 bw.flush();
  39.                 fw.flush();
  40.                 fr.close();
  41.                 br.close();
  42.                 fw.close();
  43.                 bw.close();
  44.         }
  45. }
复制代码
回复 使用道具 举报
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner; public class Test {
public static void main(String[] args) throws IOException {
Scanner input=new Scanner(System.in);
String str,straim;
System.out.print("请输入您要拷贝的源文件:");
str=input.next();
File file=new File(str);
if(file.exists()){
System.out.print("请输入您要拷贝的目标位置:");
straim=input.next();
}
else{
System.out.println("源文件未找到!");
return;
}
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
FileWriter fw=new FileWriter(straim);
BufferedWriter bw=new BufferedWriter(fw);
String line=null;
while((line=br.readLine())!=null){//你的程序这里是个死循环,它读了第一行后就反复的去写
bw.write(line);
bw.newLine();
}
bw.flush();
fr.close();
br.close();
fw.close();
bw.close();
}
}
回复 使用道具 举报
这个程序只能复制文本的文件,下面是修改后的代码



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class CopyTest {
        public static void main(String[] args) throws IOException {
                Scanner input = new Scanner(System.in);
                String str, straim;
                System.out.print("请输入您要拷贝的源文件:");
                str = input.next();
                File file = null;//由于可能是不存在的,需要赋值,所以在外面创建为空,里面直接赋值。
                while (true) {
                        file = new File(str);//这是重新给file赋值的动作
                        if (file.exists()) {
                                System.out.print("请输入您要拷贝的目标位置:");
                                straim = input.next();
                                break;//如果正确,结束循环。
                        } else {
                                System.out.println("源文件未找到!请重新输入");// 每次输入目标错误都要求重新输入,直到正确。
                                str = input.next();//这是重新赋值的动作。
                        }
                }
                FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                FileWriter fw = new FileWriter(straim);
                BufferedWriter bw = new BufferedWriter(fw);
                String line = null; // 这里设置为null
                while ((line = br.readLine()) != null)// line应该每次在这里都要重新赋值为br.readLine(),并判断不能为空
                {
                        bw.write(line);
                        bw.newLine(); // 每一行复制完毕都要新起一行
                        // br.readLine();
                        bw.flush(); // 在循环里刷新
                }
                // fw.flush(); 缓冲流在操作输出流,所以fw不需要刷新
                // fr.close(); 关闭缓冲流即可,不需要。
                bw.close(); // 先关输出流
                br.close(); // 后关输入流
                // fw.close(); 关闭缓冲流即可,不需要。
                System.out.println("感谢使用,复制完成");
        }
}
回复 使用道具 举报
直接修改成这样算了
  1. import java.io.*;
  2. import java.util.*;
  3. class MyBufferInputStream
  4. {
  5.         private InputStream in;
  6.         private byte buf[]=new byte[1024*20];
  7.         private int pos=0,count=0;
  8.         MyBufferInputStream(InputStream in)
  9.         {
  10.         this.in=in;
  11.         }
  12.         //一次读一个字节,从缓冲区(字节数组)获取
  13.         public int myRead() throws IOException
  14.         {
  15.                 if(count==0)
  16.                 {
  17.                         count=in.read(buf);
  18.                         if(count<0)
  19.                                 return -1;
  20.                         pos=0;
  21.                         byte b=buf[pos];
  22.                         count--;
  23.                         pos++;
  24.                         return b&255;
  25.                 }
  26.                 else if(count>0)
  27.                 {
  28.                         byte b=buf[pos];
  29.                         count--;
  30.                         pos++;
  31.                         return b&0xff;
  32.                        
  33.                 }
  34.                 return -1;
  35.         }
  36.         public void MyClose() throws IOException
  37.         {
  38.                 in.close();
  39.                
  40.         }
  41. }

  42. class  FileCopy
  43. {
  44.         //字节流的方式不仅可以拷贝普通文件,还可以拷贝任意的二进制文件例如exe文件
  45.         public static void main(String[] args)  throws IOException
  46.         {        System.out.printf("请输入源文件,与目的地:");
  47.                 Scanner input=new Scanner(System.in);
  48.                 String name=input.next();
  49.                 String name1=input.next();
  50.                
  51.                 long start=System.currentTimeMillis();
  52.                 FileCopyBuffer(name,name1);//FileCopyBuffer(源文件,目标位置)
  53.                 long end=System.currentTimeMillis();
  54.                 System.out.println((end-start)+"毫秒");
  55.         }
  56.         public static void FileCopyBuffer(String name,String name2) throws IOException
  57.         {
  58.                 MyBufferInputStream fr= new MyBufferInputStream(new FileInputStream(name));
  59.                 BufferedOutputStream fw= new BufferedOutputStream(new FileOutputStream(name2));
  60.                 int byte1=0;
  61.                 while((byte1=fr.myRead())!=-1)
  62.                 {
  63.                         fw.write(byte1);
  64.                 }
  65.                 fr.MyClose();
  66.                 fw.close();

  67.         }
  68.        
  69.        
  70. }
复制代码
回复 使用道具 举报
楼主我给你改正确了,你看我的截图注释。。。。。。。

dd.jpg (91.09 KB, 下载次数: 23)

修改正确的截图,请看注释行

修改正确的截图,请看注释行
回复 使用道具 举报
import java.io.*;

public class Copy{

                public static void main(String[] args){
               
                try{
                                File sourceFile=new File("E:/java练习/HalfFind.java");
                                if(!sourceFile.exists()){
                                        System.out.println("source file not exist");
                                }
                                File destinationFile=new File("E:/java练习/HalfFindCopy.java");
                                if(destinationFile.exists()){
                                        System.out.println("destination file already exist");
                                }
                                BufferedInputStream input=
                                new BufferedInputStream(new FileInputStream(sourceFile));
                                BufferedOutputStream output=
                                new BufferedOutputStream(new FileOutputStream(destinationFile));
                                int r;
                                while((r=input.read())!=-1){
                                        output.write((byte)r);
                                }
                                input.close();
                                output.close();
                                System.out.println("Copy Finish");
                        }catch(IOException e){
                                e.printStackTrace();
                }
       
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马