Skip to content Skip to sidebar Skip to footer

How to Read Data From File Into Arraylist in Java

 File handling plays a major role in doing so as the first essential step is writing content to a file. For this is ane must know how to write content in a file using the  FileWriter class. The secondary step is reading content from a file and print the same. For this, 1 must have good easily on File Reader course to do so.

Now in order to read content from one file and write it into some other file, it is already discussed achieving the same how to write content on a file and too how to read contents from a file. Now, the time to combine both of them. Now we will utilize FileReader class to read the contents from a course and the FileWriter class to write information technology on another file.

Methods: In order to read contents from a file and write it into some other file, i must have to know how to read a file or write a file.

  1. Using the variable
  2. Without using any variable

Method 1: Using the variable

Example i:

Java

import java.io.FileReader;

import coffee.io.FileWriter;

import coffee.io.IOException;

class GFG {

public static void main(String[] args)

{

try {

FileReader fr = new FileReader( "gfgInput.txt" );

FileWriter fw = new FileWriter( "gfgOutput.txt" );

Cord str = "" ;

int i;

while ((i = fr.read()) != - 1 ) {

str += ( char )i;

}

Organisation.out.println(str);

fw.write(str);

fr.close();

fw.close();

System.out.println(

"File reading and writing both done" );

}

catch (IOException e) {

System.out.println(

"At that place are some IOException" );

}

}

}

Output: Every bit this code is accessing internal storage to relieve that file, so it wouldn't run on the compiler so the output is hard-coded below as shown

The program prints the content in that file, and and so in the next line, information technology will print File reading and writing done(if there is no error occurred), and the contents of the input file will be written in the new output file. If at that place is some error, so it will impress There are some IOException.

Method ii: Without using whatever variable

In the previous program, we were storing all the contents of the input file in a variable, and so we were writing the string in the output file. At present nosotros tin can directly shop those characters in the output file directly.

Coffee

import coffee.io.FileWriter;

import coffee.io.IOException;

class GFG {

public static void chief(String[] args)

{

endeavour {

FileWriter fw = new FileWriter( "gfg.txt" );

fw.write( "We love GeeksForGeeks" );

fw.close();

System.out.println( "\nFile write done" );

}

catch (IOException e) {

Organisation.out.println(

"There are some IOException" );

}

}

}

Output: Every bit this code is accessing internal storage to save that file, so it wouldn't run on the compiler so the output is difficult-coded below as shown

As output the program will print FIle write done(if there is no error), and volition create a file with the same name given given as file proper noun, i.e, 'gfg.text'


avilaflized.blogspot.com

Source: https://www.geeksforgeeks.org/java-program-to-read-content-from-one-file-and-write-it-into-another-file/

Post a Comment for "How to Read Data From File Into Arraylist in Java"