您的位置:程序门 -> java -> j2se / 基础类



j2se--用java流如何将一个文件拷贝到别的地方-->清高手指教


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


j2se--用java流如何将一个文件拷贝到别的地方-->清高手指教
发表于:2007-03-16 21:48:56 楼主
请教高手如何用java流将一个文件拷贝到别的地方

我在jdk中没有看到关于copy的方法...

发表于:2007-03-16 22:19:511楼 得分:0
拷贝,自己实现吧
就是读出再写入
发表于:2007-03-16 22:53:132楼 得分:0
就是读出再写入

就是把一个文件的内容读出来在写到另个文件里吗

这不叫拷贝吧..

发表于:2007-03-17 00:08:463楼 得分:0
是叫copy啊,从输入流读入在输出到指定的文件就是copy啊
发表于:2007-03-17 00:14:114楼 得分:0
哦,是的,拷贝的可以是目录,这里有个很好的程序,供参考,有次看到的,


controlfile.java               拷贝莫路径下的所有文件      
       
    import       java.io.*;      
    import       java.text.*;      
    import       java.util.*;      
       
    /**      
        *       @author       ljt      
        *      
        *       to       change       this       generated       comment       edit       the       template       variable       "typecomment ":      
        *       window> preferences> java> templates.      
        *       to       enable       and       disable       the       creation       of       type       comments       go       to      
        *       window> preferences> java> code       generation.      
        */      
    public       class       controlfile       {      
    //已有文件的路径,需要备份到那个路径的名字      
    string       filepath,       aimfilepath;      
    //保存已有文件路径下的所有的文件的名字       存放string      
    vector       vec;      
    public       controlfile()       {      
    filepath       =       " ";      
    aimfilepath       =       " ";      
    vec       =       new       vector();      
    }      
    public       controlfile(string       filepath,       string       aimfilepath)       {      
    this.filepath       =       filepath;      
    this.aimfilepath       =       aimfilepath;      
    vec       =       new       vector();      
    }      
    //得到目录下所有文件的名字      
    private       void       getfilename()       {      
    file       f       =       new       file(filepath);      
    string       str[]       =       f.list();      
    for       (int       i       =       0;       i       <       str.length;       i++)       {      
    vec.addelement(str[i]);      
    }      
    }      
       
    //       文件的拷贝:::测试成功      
    private       boolean       bakfile(string       filename)       {      
    try       {      
    //读文件          
    filereader       raf       =       new       filereader(filepath       +       filename);      
    string       detail       =       " ";      
    bufferedreader       buff       =       new       bufferedreader(raf);      
    string       temp       =       buff.readline();      
    while       (temp       !=       null)       {      
    detail       +=       temp       +       "\n ";      
    temp       =       buff.readline();      
    }      
    raf.close();      
    system.out.println(detail);      
    //写文件      
    file       file       =       new       file(aimfilepath       +       filename);      
    printwriter       out       =       new       printwriter(new       filewriter(file));      
    out.print(detail);      
    out.close();      
       
    }       catch       (filenotfoundexception       e)       {      
    system.out.println( "文件没有找到 ");      
    }       catch       (ioexception       e)       {      
    system.out.println( "copyfile       出错 ");      
    }      
    return       true;      
    }      
    public       static       void       main(string[]       args)       {      
    controlfile       confile       =      
    new       controlfile( "d:\\readfile\\ ",       "d:\\work\\bakfile\\ ");      
    confile.getfilename();      
    vector       ve       =       new       vector();      
    ve       =       confile.vec;      
    if       (ve       !=       null)      
    for       (int       i       =       0;       i       <       ve.size();       i++)       {      
    system.out.println((string)       ve.elementat(i));      
    confile.bakfile((string)       ve.elementat(i));      
    }      
    }      
    }      


发表于:2007-03-17 00:14:235楼 得分:0
public   static   void   main(string[]   args)   {
                file   oldfile   =   new   file( "c:/oldfile.txt ");
                file   newfile   =   new   file( "c:/newfile.txt ");
                try   {
                        fileinputstream   inputstream   =   new   fileinputstream(oldfile);
                        fileoutputstream   outputstream   =   new   fileoutputstream(newfile);
                        byte[]   bytearr   =   new   byte[512];
                        while   (inputstream.read(bytearr)   >   0)   {
                                outputstream.write(bytearr);
                                outputstream.flush();
                        }
                        outputstream.close();
                        inputstream.close();
                }   catch   (exception   ex)   {
                        ex.printstacktrace();
                }
        }
发表于:2007-03-17 12:55:116楼 得分:0
用通道不就得了?好多人问....
package   test;

import   java.io.file;
import   java.io.fileinputstream;
import   java.io.filenotfoundexception;
import   java.io.fileoutputstream;
import   java.io.ioexception;
import   java.nio.channels.filechannel;
import   java.nio.channels.writablebytechannel;

public   class   copyfile   {
public   static   void   main   (string   args[])
{
file   f=   new   file   ( "d://emule-0.47c-verycd1215-setup.exe ");
try   {
fileinputstream   fin   =   new   fileinputstream   (f);
filechannel   fc   =   fin.getchannel();
fileoutputstream   fout   =   new   fileoutputstream( "d://1.exe ");
writablebytechannel   to   =   fout.getchannel();
fc.transferto(0,   fc.size(),to   );
}   catch   (filenotfoundexception   e)   {
system.err.println( "file   not   found! ");
}   catch   (ioexception   e)   {
system.err.println( "there   are   errors   on   processing   ");
}

}
}
发表于:2007-03-17 17:12:207楼 得分:0
刚刚写了个程序:

/**
  *   @param   args
  *   @throws   ioexception  
  *   @throws   ioexception
  *   function:   实现文件的copy功能,把read.txt的内容拷贝到text.txt文件
  */
public   static   void   main(string[]   args)   throws   ioexception   {
//   todo   auto-generated   method   stub

file   read   =   new   file( "d://read.txt ");
system.out.println(read.length());
fileinputstream   readstream     =   new   fileinputstream(read);

byte[]   b   =   new   byte[(int)read.length()];
readstream.read(b);

file   s   =   new   file( "d://text.txt ");

fileoutputstream   writestream   =   new   fileoutputstream(s);

writestream.write(b);
readstream.close();
writestream.close();
writestream.flush();

}
发表于:2007-03-17 17:13:488楼 得分:0
给完整的代码吧!
import   java.io.file;
import   java.io.fileoutputstream;
import   java.io.ioexception;
import   java.io.fileinputstream;

public   class   fileoutputtest   {

/**
  *   @param   args
  *   @throws   ioexception  
  *   @throws   ioexception
  *   function:   实现文件的copy功能,把read.txt的内容拷贝到text.txt文件
  */
public   static   void   main(string[]   args)   throws   ioexception   {
//   todo   auto-generated   method   stub

file   read   =   new   file( "d://read.txt ");
system.out.println(read.length());
fileinputstream   readstream     =   new   fileinputstream(read);

byte[]   b   =   new   byte[(int)read.length()];
readstream.read(b);

file   s   =   new   file( "d://text.txt ");

fileoutputstream   writestream   =   new   fileoutputstream(s);

writestream.write(b);
readstream.close();
writestream.close();
writestream.flush();

}
发表于:2007-03-22 18:04:159楼 得分:0
mark
发表于:2007-03-22 18:23:1710楼 得分:0
大部分人平时用io都喜欢用fileoutputstream和fileinputstream吗


快速检索

最新资讯
热门点击