File I/O: Reading from one file and writing to another (Java)

前端 未结 5 1186
闹比i
闹比i 2021-01-05 02:36

I\'m currently working on a lab in my cpe class and we have to create a simple program that scans in strings from a .txt file and prints them to a different .txt file. So fa

5条回答
  •  春和景丽
    2021-01-05 03:11

    public void readwrite() throws IOException 
    {
        // Reading data from file
        File f1=new File("D:/read.txt");
        FileReader fr=new FileReader(f1);
        BufferedReader br=new BufferedReader(fr);
    
        String s = br.readLine();
    
        // Writing data
        File f2=new File("D:/write.txt");
        FileWriter fw=new FileWriter(f2);
        BufferedWriter bw=new BufferedWriter(fw);
        while(s!=null)
        {
            bw.write(s);
            bw.newLine();
            System.out.println(s);
            s=br.readLine();
    
        }
        bw.flush();
        bw.close();
    
    }
    

提交回复
热议问题