博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java实现文件拷贝
阅读量:4036 次
发布时间:2019-05-24

本文共 3552 字,大约阅读时间需要 11 分钟。

实现文件对拷,开始是写了一个函数,但一想,要复用就要以类为单位,故写成一个文件类,现只提供一个文件到文件的拷贝函数,以后再进行扩充!(在下面已经提交了改进后的代码)
/*
 * myFile.java
 *
 * Created on 2006年4月22日, 下午7:22
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package myUrl;
import java.io.*;
/**
 *
 * @author flying
 */
public class myFile {
   
    /** Creates a new instance of myFile */
    public myFile() {
    }
    public void SaveFileToFile(String F1,String F2){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try{
            fis = new FileInputStream(new File(F1));    //建立文件输入流
           
            File file = new File(F2);
            fos = new FileOutputStream(F2);
           
            int r;
            while((r=fis.read())!=-1){
                fos.write((byte)r);
            }
        }
        catch(FileNotFoundException ex){
            System.out.println("Source File not found:"+F1);
        }
        catch(IOException ex){
            System.out.println(ex.getMessage());
        }
        finally{
            try{
                if(fis!=null) fis.close();
                if(fos!=null) fos.close();
            }
            catch(IOException ex){
                System.out.println(ex);
            }
        }
    }
   
}
 
进一步改进,试用了好几种方法,留下了三种方法供参考:
 
package myUrl;
import java.io.*;
/**
 *
 * @author flying
 */
public class myFile {
   
    final int bufSize = 4096;
   
    /** Creates a new instance of myFile */
    public myFile() {
    }
   
    public String displayFile(String file){
        String str="";
        String inLine="";
        BufferedReader infile = null;
        try{
            infile=new BufferedReader(new FileReader(file));
            while((inLine=infile.readLine())!=null){
                str=str+(inLine + '/n');
            }
        }
        catch(FileNotFoundException ex){
            System.out.println("Source File not found:"+file);
        }
        catch(IOException ex){
            System.out.println(ex.getMessage());
        }
        finally{
            try{
                if(infile!=null) infile.close();
            }
            catch(IOException ex){
                System.out.println(ex);
            }
        }
        return str;
    }
   
    public void saveFileToFileText(String F1,String F2){       //处理文本文件没有问题,但是处理.mdb出现了问题.可能就是分隔符引起的.
        String inLine="";
        BufferedReader infile = null;
        BufferedWriter outfile = null;
        try{
            infile=new BufferedReader(new FileReader(F1));
            outfile=new BufferedWriter(new FileWriter(F2));
            while((inLine=infile.readLine())!=null){
                outfile.write(inLine);
                outfile.newLine();
            }
        }
        catch(FileNotFoundException ex){
            System.out.println("Source File not found:"+F1);
        }
        catch(IOException ex){
            System.out.println(ex.getMessage());
        }
        finally{
            try{
                if(infile!=null) infile.close();        //一定要进行文件的关闭,否则在新文件会是空的!
                if(outfile!=null) outfile.close();
            }
            catch(IOException ex){
                System.out.println(ex);
            }
        }
    }
   
    public void saveFileToFile(String F1,String F2){            //实现文件对拷,从F1拷贝到F2,若F2存在则会被覆盖;适用于任何文件.
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try{
            fis = new FileInputStream(new File(F1));    //建立文件输入流
           
            File file = new File(F2);
            fos = new FileOutputStream(F2);
            byte[] buffer=new byte[bufSize];
            int len;
            while((len=fis.read(buffer))!=-1){
                fos.write(buffer,0,len); 
            }
        }
        catch(FileNotFoundException ex){
            System.out.println("Source File not found:"+F1);
        }
        catch(IOException ex){
            System.out.println(ex.getMessage());
        }
        finally{
            try{
                if(fis!=null) fis.close();      //一定要进行文件的关闭,否则在新文件会是空的!
                if(fos!=null) fos.close();
            }
            catch(IOException ex){
                System.out.println(ex);
            }
        }
    }
   
    public void saveFileToFileOneByte(String F1,String F2){            //实现文件对拷,从F1拷贝到F2,若F2存在则会被覆盖;适用于任何文件.
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try{
            fis = new FileInputStream(new File(F1));    //建立文件输入流
           
            File file = new File(F2);
            fos = new FileOutputStream(F2);
           
            int r;
            while((r=fis.read())!=-1){
                fos.write((byte)r);
            }
        }
        catch(FileNotFoundException ex){
            System.out.println("Source File not found:"+F1);
        }
        catch(IOException ex){
            System.out.println(ex.getMessage());
        }
        finally{
            try{
                if(fis!=null) fis.close();      //一定要进行文件的关闭,否则在新文件会是空的!
                if(fos!=null) fos.close();
            }
            catch(IOException ex){
                System.out.println(ex);
            }
        }
    }
   
}

转载地址:http://qocdi.baihongyu.com/

你可能感兴趣的文章
opencv学习——在MFC中读取和显示图像
查看>>
retext出现Could not parse file contents, check if you have the necessary module installed解决方案
查看>>
pyQt不同窗体间的值传递(一)——对话框关闭时返回值给主窗口
查看>>
linux mint下使用外部SMTP(如网易yeah.net)发邮件
查看>>
北京联通华为光猫HG8346R破解改桥接
查看>>
python使用win32*模块模拟人工操作——城通网盘下载器(一)
查看>>
python append 与浅拷贝
查看>>
Matlab与CUDA C的混合编程配置出现的问题及解决方案
查看>>
python自动化工具之pywinauto(零)
查看>>
python一句话之利用文件对话框获取文件路径
查看>>
PaperDownloader——文献命名6起来
查看>>
PaperDownloader 1.5.1——更加人性化的文献下载命名解决方案
查看>>
如何将PaperDownloader下载的文献存放到任意位置
查看>>
C/C++中关于动态生成一维数组和二维数组的学习
查看>>
JVM最简生存指南
查看>>
漂亮的代码,糟糕的行为——解决Java运行时的内存问题
查看>>
Java的对象驻留
查看>>
logback高级特性使用(二) 自定义Pattern模板
查看>>
JVM并发机制探讨—内存模型、内存可见性和指令重排序
查看>>
可扩展、高可用服务网络设计方案
查看>>