将内容从一个文件复制到另一个文件(如果存在则不要覆盖)

时间:2016-12-20 15:13:11

标签: java

我编写了一个java代码,用于将内容从1个文件复制到其他文件。这里所说的是,如果文件存在,它就不会被覆盖。我已经使用了这种情况,所以如果它存在它不会覆盖但它会删除第二个文件的整个内容...请帮助我使用代码。我在这里分享了问题和代码。请帮助!!

问题:

java程序,它将源文件和目标文件作为命令行参数输入。它将源文件内容复制到目标文件。如果源文件不存在,则应提供相应的消息使用。如果目标文件不存在,则应创建它。如果存在,程序应该询问“是否要覆盖?(是/否”)。 在用户选择的基础上,应采取适当的措施。

JAVA CODE:

package com.files.file_handle;
import java.io.Closeable;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class FileCopy { 
    public static void main(String[] args) throws IOException { 
        Scanner s=new Scanner(System.in); 
        FileReader fr = null; 
        FileWriter fw = null; 
        try { 
            System.out.println("enter a source file which exists"); 
            String file1=s.next(); 
            fr = new FileReader(file1); 
            System.out.println("enter a destination file"); 
            String file2=s.next();

            File f2=new File(file2); 
            if(!f2.exists()) { 
                fw = new FileWriter(file2); 
                f2.createNewFile(); 
                int c = fr.read(); 
                while(c!=-1) { 
                    fw.write(c); 
                    c = fr.read(); 
                } 
                System.out.println("file copied successfully"); 
            } else { 
                fw = new FileWriter(file2); 
                System.out.println("do you want to overwrite? enter 'yes' or 'no'..."); 
                char ans = s.next().charAt(0);

                if(ans=='N'||ans=='n') { 
                    System.out.println("couldnot enter data"); 
                } else { 
                    int c = fr.read(); 
                    while(c!=-1) { 
                        fw.write(c); 
                        c = fr.read(); 
                    } 
                    System.out.println("file updated successfully"); 
                } 
            } 
        } catch(IOException e) { 
            System.out.println("file coudn't be found"); 
        } finally { 
            close(fr); 
            close(fw); 
        } 
    } 
    public static void close(Closeable stream) { 
        try { 
            if (stream != null) { 
                stream.close(); 
            } 
        } catch(IOException e) { //... } 
    } 
} 

1 个答案:

答案 0 :(得分:1)

以下代码运行正常,问题是在写入模式下打开文件时,其内容将自动清除。

import java.io.Closeable;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class FileCopy { 
public static void main(String[] args) throws IOException { 
    Scanner s=new Scanner(System.in); 
    FileReader fr = null; 
    FileWriter fw = null; 
    try { 
        System.out.println("enter a source file which exists"); 
        String file1=s.next(); 
        fr = new FileReader(file1); 
        System.out.println("enter a destination file"); 
        String file2=s.next();

        File f2=new File(file2); 
        if(!f2.exists()) { 
            fw = new FileWriter(file2); 
            f2.createNewFile(); 
            int c = fr.read(); 
            while(c!=-1) { 
                fw.write(c); 
                c = fr.read(); 
            } 
            fr.close();
            System.out.println("file copied successfully"); 
        } else { 

            System.out.println("do you want to overwrite? enter 'yes' or 'no'..."); 
            char ans = s.next().charAt(0);

            if(ans=='N'||ans=='n') { 
                fr.close();
            //  fw.close();
                System.out.println("couldnot enter data"); 
            } else { 
                  fw = new FileWriter(file2); 
                int c = fr.read(); 
                while(c!=-1) { 
                    fw.write(c); 
                    c = fr.read(); 
                } 
                fr.close();
                System.out.println("file updated successfully"); 
            } 
        } 
    } catch(IOException e) { 
        System.out.println("file coudn't be found"); 
    } finally { 
        close(fr); 
        close(fw); 
        //fw.close();
    } 
} 
public static void close(Closeable stream) { 
    try { 
        if (stream != null) { 
            stream.close(); 
        } 
    } catch(IOException e) { //... 
        e.printStackTrace();
        } 
    }
} 
相关问题