0

This is the code using for file copying input file to output file.

import java.io.*;

public class ReadandWrite {

    public static void main(String[]  args) throws IOException {

        FileReader fr = new FileReader("D:\\writeIntoTextFile.txt");
        BufferedReader br = new BufferedReader(fr);
        BufferedWriter bw = new BufferedWriter( new FileWriter("D:\\writeIntoTextFile1.txt"));
        bw.write(br.readLine());
        bw.close();
    }
}

The output i am getting only first line from Input file.

Input file(writeIntoTextFile)

Output File(writeIntoTextFile1)

Holger
  • 285,553
  • 42
  • 434
  • 765
Venkat P
  • 1
  • 1
  • please suggest i want use that code only i am thinking that while i can use it but getting error – Venkat P Aug 18 '21 at 11:34
  • 3
    `readLine` reads just one line. – palindrom Aug 18 '21 at 11:37
  • 1
    You call `br.readLine()` just once. To read the entire file you have to use `br.readLine()` until `br.readLine()` returns `null` – KunLun Aug 18 '21 at 11:38
  • 1
    See https://stackoverflow.com/questions/5868369/how-can-i-read-a-large-text-file-line-by-line-using-java – İsmail Y. Aug 18 '21 at 11:40
  • Depending on your Java version, you might want to consider using the stream returned by `br.lines()`. It could feel much more straight forward to you to consume all the lines, one after each other instead of needing to get each line in (as suggested) a loop. – Bluddymarri Aug 18 '21 at 11:41
  • i am using java 11 and public static void main(String[] args) throws IOException { FileReader fr = new FileReader("D:\\writeIntoTextFile.txt"); BufferedReader br = new BufferedReader(fr); BufferedWriter bw = new BufferedWriter( new FileWriter("D:\\writeIntoTextFile1.txt")); bw.write(br.readLine()); bw.write(br.readLine()); bw.write(br.readLine()); bw.write(br.readLine()); bw.write(br.readLine()); bw.close(); } } but how can i write in reapted code in loop i am not getting logic – Venkat P Aug 18 '21 at 11:51
  • 1
    Hmm. All you're doing is copying a file. So why don't you just do `Files.copy`? – g00se Aug 18 '21 at 12:11

1 Answers1

2

readLine() read just one line at once.

To read all lines you have to call readLine() until it returns null

BufferedReader br = new BufferedReader(new FileReader("D:\\writeIntoTextFile.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\writeIntoTextFile1.txt"));

try{
            
    String line;
    while((line = br.readLine()) != null){
        bw.write(line);
        bw.newLine();
    }

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

br.close();
bw.close();

Also there can be other option: lines() which returns a Stream of all lines: Stream<String>

In this case you have to deal only with writing lines, without checking when you reach the end of file.

BufferedReader br = new BufferedReader(new FileReader("D:\\writeIntoTextFile.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\writeIntoTextFile1.txt"));

br.lines().forEach(line -> {
    try{
        bw.write(line);
        bw.newLine();
    }catch(Exception e){
        e.printStackTrace();
    }
});

br.close();
bw.close();
KunLun
  • 3,109
  • 3
  • 18
  • 65
  • 2
    For completion: Since Java 10 there is a `reader.transferTo(writer)` which accepts a writer as parameter and will transfer all the content of the read file to the writer. – Bluddymarri Aug 18 '21 at 12:08