有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我编写了一个java代码,将内容从一个文件复制到另一个文件。这里说的是,如果文件存在,就不应该重写。我使用了这个案例,所以如果它存在,它不会覆盖,但会删除第二个文件的全部内容。。。请帮我查一下密码。我在这里分享了这个问题和代码。请帮忙

问题:

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

JAVA代码:

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) 个答案

  1. # 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();
            } 
        }
    }