Java read from one file and write into another file using methods

write a program to copy one file to another file in java
java write text file line by line
write a program in java to create, write, modify, read operations on a text file.
java read from a file and write to another file
write a java program that accepts the input from the keyboard and writes it to a text file
java write to text file
java 8 write to file
creating reading and updating a sequential access file in java

I am learning Java and working on File IO and right now stuck in reading text from One file and write in another file. I am using two different methods first one for reading and displaying text in console from file #1 and using another method to write in file#2.

I can successfully read and display contents from File#1 but not sure how to write the text in file#2.

Here is the code which I have written so far:

import java.io.*;
public class ReadnWrite {
    public static void readFile() throws IOException {
        BufferedReader inputStream = new BufferedReader(new FileReader(
                "original.txt"));
        String count;
        while ((count = inputStream.readLine()) != null) {
            System.out.println(count);
        }
        inputStream.close();
    }
    public static void writeFile() throws IOException{
        BufferedWriter outputStream = new BufferedWriter(new FileWriter(
        "numbers.txt"));

//Not sure what comes here
    }
    public static void main(String[] args) throws IOException {
        checkFileExists();
        readFile();
    }
}

This is just for my own learning as there are lots of examples to read and write without using different methods but I want tolearn how I can achieve through different methods.

Any help will be highly appreciated.

Regards,

You can write to another file using: outputStream.write(). And when you are done just outputStream.flush() and outputStream.close().

Edit:

public void readAndWriteFromfile() throws IOException {
    BufferedReader inputStream = new BufferedReader(new FileReader(
            "original.txt"));
     File UIFile = new File("numbers.txt");
        // if File doesnt exists, then create it
        if (!UIFile.exists()) {
            UIFile.createNewFile();
        }
    FileWriter filewriter = new FileWriter(UIFile.getAbsoluteFile());
    BufferedWriter outputStream= new BufferedWriter(filewriter);
    String count;
    while ((count = inputStream.readLine()) != null) {
        outputStream.write(count);
    }
    outputStream.flush();
    outputStream.close();
    inputStream.close();

Java transfer content from one file to another, Java transfer content from one file to another It simply read the input file line by line, and then output each line to the other file. BufferedReader; import java.io. null) { //Process each line and add output to Dest.txt file out.write(aLine); out. Problem Description. How to copy one file into another file ? Solution. This example shows how to copy contents of one file into another file using read & write methods of BufferedWriter class.

Here is my way how to copy files:

BufferedReader br = null;
BufferedWriter bw = null;

try {
    br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("origin.txt"))));
    bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("target.txt"))));

    int i;
    do {
        i = br.read();
        if (i != -1) {
            bw.write(i);
        }
    } while (i != -1);

} catch (IOException e) {
    System.err.println("error during copying: "+ e.getMessage());
} finally {
    try {
        if (br != null) br.close();
        if (bw != null) bw.close();
    } catch (IOException e) {
        System.err.println("error during closing: "+ e.getMessage());
    }
}

How to Copy a File to another File in Java, In this tutorial we will see how to copy the content of one file to another file in input stream to * output stream using read and write methods */ while ((length  For my assignment, I have to read from a file with 25 numbers, then sort it in order, then write it to another file. I can't think of a way to pass the array in my code (to the the string of an array) to write the file in order and for it to write the numbers to a different file.

I'd use a BufferedReader that wraps a FileReader and a BufferedWriter that wraps a FileWriter.

Since you want to do it using two different methods you have to store the data in a List<String> data to pass it between the methods.

public class ReadnWrite {

    public static List<String> readFile() throws IOException {
        try(BufferedReader br = new BufferedReader(new FileReader("original.txt"))){
            List<String> listOfData = new ArrayList<>();
            String d;
            while((d = br.readLine()) != null){
                listOfData.add(d);
            }
            return listOfData;
        }
    }

    public static void writeFile(List<String> listOfData) throws IOException{
        try(BufferedWriter bw = new BufferedWriter(new FileWriter("numbers.txt"))){
            for(String str: listOfData){
                bw.write(str);
                bw.newLine();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        List<String> data = readFile();
        writeFile(data);
    }
}

As an alternative, if you don't have to do operations on the data between read and write I'd also do it in one method.

public class CopyFileBufferedRW {

    public static void main(String[] args) {
        File originalFile = new File("original.txt");
        File newFile = new File(originalFile.getParent(), "numbers.txt");

        try (BufferedReader br = new BufferedReader(new FileReader(originalFile));
             BufferedWriter bw = new BufferedWriter(new FileWriter(newFile))) {
            String s;
            while ((s = br.readLine()) != null) {
                bw.write(s);
                bw.newLine();
            }
        } catch (IOException e) {
            System.err.println("error during copying: " + e.getMessage());
        }
    }
}

How to Read and Write Text File in Java, Read/Write text files. Read text file line by line. List files and directories. Copy File or Directory. The main difference between these two packages is that the read () and write () methods of Java IO are a blocking calls. By this we mean that the thread calling one of these methods will be blocked until the data has been read or written to the file. On the other hand, in the case of NIO, the methods are non-blocking.

Reading and Writing Files in Java, In this tutorial, we'll be reading from and writing to files in Java using class to read data because more data can been read from the file in one read() call, Files: This is another utility class which has several methods to read and write files  The main logic of copying file is to read the file associated to FileInputStream variable and write the read contents into the file associated with FileOutputStream variable. Methods used in the program. int read(); Reads a byte of data. Present in FileInputStream.

Reading and writing files in Java (Input/Output) - Tutorial, Java Input Output. This tutorial explains how to read and write files via Java. The Files.lines method allows read a file line by line, offering a stream. The next example extracts a line starting with "Bundle-Version:" from a file called MANIFEST. Create a new Java project called com.vogella.java.files. Write To a File. In the following example, we use the FileWriter class together with its write() method to write some text to the file we created in the example above. . Note that when you are done writing to the file, you should close it with the close() me

Java program to read a text file and write to another file., In this example we are going to develop a Java program to read a text file and write to another file. We will simple read one text file and write to another text file. And to read the data we have use the read() method of FileInputStream class. Have a single file writer thread which keeps reading from blocking queue and writing to a file. All other 20 threads will just put the data in blocking queue. This will prevent contention between 20 writer threads.

Comments
  • You want to read from original.txt and write everything to numbers.txt?
  • possible duplicate of How to Write text file Java
  • O comon man, use your common sense. The link is using the code written inside Main method. I can easily achieve that thing but I want to use two different methods without using Reader object in write method.
  • #Farhang Amary, kindly read through the question properly before posting any possible duplicate question. I have gone through the link and that is using writing to a file in Main() and also I do not want to write any stupid "HELLO WORLD" text by myself. I want to write whatever I am reading from file#1 (original.txt) and write it into file#2(numbers.txt).
  • Thanks #jnd but if I try by just giving outputStream.write(); java gives me error that Missing argument to match .write( ) and also how does java know that what I want to save in the file. I believe I have to put all the text from original.txt file in to an object or an array and then write the contents of array/ object in outputStream.write(); in order to write in numbers.txt file.
  • You put what you want to write as the arguement. For example outputStream.write("hello"); or outputStream.write(15);
  • #jnd I think you didn't get my question as I am retrieving text from file#1 and writing in file#2. Its not that I want to write anything from myself inside quotes...
  • Thanks #jnd for the reply. It is almost similar to what I wanted. I have achieved my desired my results through your code but can I ask you a question that e.g. If my program asks the user to press 1 to read from file and 2 to write to a new file and do I have to use this BufferedReader method and read the file if user press 2 or is there a way that if user has pressed 1 and the program has saved all the file contents in an array/ arraylist and when user has pressed 2 it writes everything from that array/arraylist into file#2.
  • You can do either way. You would just make your read method return an array of strings and then pass that to your write method for it to write.