簡體   English   中英

將內容從一個文件復制到另一個文件(如果存在則不要覆蓋)

[英]copy contents from one file to another file(dont overwrite if exists)

我編寫了一個 java 代碼來將內容從 1 個文件復制到另一個文件。 這里所說的是,如果文件存在,則不應覆蓋它。 我已經使用過這種情況,所以如果它存在,它不會覆蓋但它會刪除第二個文件的全部內容......請幫助我編寫代碼。 我在這里分享了問題和代碼。 請幫助!!

題:

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) { //... } 
    } 
} 

以下代碼完美運行,問題是在寫入模式下打開文件時,其內容將被自動清除。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM