我想设计一个备份程序,就是定时一台机器上,把文件,主要是一下word ,pdf文件备份到另一台机器上,我应该怎么做??给点提示把???

解决方案 »

  1.   

    使用流 
    FileInputStream
    FileOutputStream
    在java.io包
    例如 
    package untitled10;import java.io.*;
    public class stream {
        File F;//F=new File("文件路径") 路径中\用\\表示
        byte[] readfile(File fileInput) throws FileNotFoundException, IOException {
            
            FileInputStream fis = new FileInputStream(fileInput); //读取文件
            BufferedInputStream bis = new BufferedInputStream(fis); //在流中加入缓冲
            byte[] filearray = new byte[100000]; //根据你的文件大小设置字节数组大小
            int size =bis.read(filearray); //将读取的数据保存到字节数足并返回读取字节数 size
            byte[] returnarray = new byte[size];
            for(int i = 0 ; i<size;i++){
                returnarray[i]=filearray[i];
            }
            return returnarray;
        }    void writefile(byte[] filearray,File fileOutput) throws FileNotFoundException, IOException {
            FileOutputStream fos = new FileOutputStream(fileOutput); //读取文件
            BufferedOutputStream bos = new BufferedOutputStream(fos); //在流中加入缓冲
            bos.write(filearray);
        }
        public static void main(String [] args) throws FileNotFoundException,
                IOException {
            File fin = new File("C:\\abc.doc");
            File fout = new File("C:\\bcd.doc");
            stream test =new stream();
            
            test.writefile(test.readfile(fin),fout);
        }
    }文件路径可以添URL  或者在备份服务器写一个服务器程序  用filearray构造一个可序列化类的实例通过Socket传递这个实例在备份服务器保存文件
      

  2.   

    在两个函数结尾各加入bis.close bos.close关闭流  否则多次读取可能出现问题