我是初学  想写一个java程序把一个指定文件内容拷贝到一个新的文件中我看了File还有文件描述符对象等内容 可是还是不会用 哪位高手能给个代码做例子我参考下?多谢!!

解决方案 »

  1.   

    import java.io.*;
    class Token {
    public String readFile(String s){//读取文件
    String data = null;
    String ss="";
    try{
    BufferedReader br = new BufferedReader
    (new InputStreamReader(new FileInputStream(s)));
    while((data = br.readLine())!=null){
    //System.out.println(data);
    ss=ss+data+"\r\n";
    }
    //System.out.println(ss);
    br.close();


    }catch(Exception e){
    e.printStackTrace();

    }

    return ss;

    }

    public void writeFile(String s){//读取文件
    try{
    OutputStreamWriter osw = 
                    new OutputStreamWriter
                    (new FileOutputStream("2.txt"));
                    osw.write(s,0,s.length());
                    osw.flush();
                    osw.close();
                    
                    }catch(Exception e){
                     e.printStackTrace();
                    
                    
                    
                     }


    }

    public static void main(String[] args){
    Token tk=new Token();
    tk.writeFile(tk.readFile("1.txt"));

    }



    }
      

  2.   

    try{    //先把内容读取到data
    BufferedReader br = new BufferedReader
    (new InputStreamReader(new FileInputStream(s)));
    while((data = br.readLine())!=null){
    //System.out.println(data);
    ss=ss+data+"\r\n";
    }
    //System.out.println(ss);
    br.close();


    }catch(Exception e){
    e.printStackTrace();

    }
    try{            //把data写到dest.txt文件中
    OutputStreamWriter osw = 
                    new OutputStreamWriter
                    (new FileOutputStream("dest.txt"));
                    osw.write(data,0,data.length());
                    osw.flush();
                    osw.close();
                    
                    }catch(Exception e){
                     e.printStackTrace();
                    
                    
                    
                     }
      

  3.   

    For Texe file:
        
    public static void copyFile(String oFileName, String tFileName, boolean addLF) throws IOException{
    File of = new File(oFileName);
    if(of.exists()){
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        new FileInputStream(of)));
                String line = reader.readLine();
                
                File tf = new File(tFileName);
    if(of.exists()){
    tf.delete();
    }
    tf.createNewFile();
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream(tf)));
                while (line != null) {
                 writer.write(line);
                 if(addLF){
                 writer.write(0x0d);
                 writer.write(0x0a);
                 }
                    line = reader.readLine();
                }
                reader.close();
                writer.close();
                System.out.println(oFileName + " has been copyed to " + tFileName);
    }
    else {
    System.out.println(oFileName + " file not exist!");
    }
    }
      

  4.   

    /**
     * oldFilePath 原文件地址
     * newFilePath 新文件地址
     * 以上两个地址必须是存在的
     **/
    private void writeNewFile(String oldFilePath, String newFilePath) throws
          Exception {
        File oldFile = new File(oldFilePath);
        File newFile = new File(newFilePath);
        
        FileInputStream fis = new FileInputStream(oldFile);
        FileOutputStream fos = new FileOutputStream(newFile,false);    byte[] by = new byte[1024];
        int flag = fis.read(by);
        while(flag != -1){
          fos.write(by);
          flag = fis.read(by);
        }
        fos.flush();
        
        fos.close();
        fis.close();
      }
      

  5.   

    Another method:
    public static boolean copyFile2(String spath, String tpath)
        {
            if (spath == null || spath.trim().length() == 0 || tpath == null || tpath.trim().length() == 0)
                return false;
            try
            {
                File sfile = new File(spath);
                String filename = sfile.getName();            File tfile = new File(tpath, filename);            RandomAccessFile rdtfile = new RandomAccessFile(tfile, "rw");            RandomAccessFile rdsfile = new RandomAccessFile(sfile, "r");
                byte[] contents = new byte[(int) rdsfile.length()];
                rdsfile.read(contents);
                rdsfile.close();            rdtfile.write(contents);
                rdtfile.close();
                return true;
            } catch (Exception e)
            {
                System.out.println("Copy file error:" + e.toString());
                return false;
            }
        }
      

  6.   

    不好意思,刚才那个错了。
    /**
     * oldFilePath 原文件地址
     * newFilePath 新文件地址
     * 以上两个地址必须是存在的
     **/
      private void writeNewFile(String oldFilePath, String newFilePath) throws
          Exception {
        File oldFile = new File(oldFilePath);
        File newFile = new File(newFilePath);
        
        FileInputStream fis = new FileInputStream(oldFile);
        FileOutputStream fos = new FileOutputStream(newFile,false);
        byte[] by = new byte[1024];
        int flag = fis.read(by);
        while(flag != -1){
          fos.write(by,0,flag);
          flag = fis.read(by);
        }
        fos.flush();
        
        fos.close();
        fis.close();
      }