Home > DeveloperSection > Blogs > Read and Write Text file using Java

Read and Write Text file using Java


Java Java 
Ratings:
0 Comment(s)
 2076  View(s)
Rate this:

Read and Write Text file using Java

In this blog I am trying to make a small demo for read and write the text file using java.

Below code create a file with StudentInfo.txt named and save the data into the file.

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.*;

 import java.io.Console;

public class WriteStudentInformationFile {

                public static void main(String[] args) {

                                FileOutputStream fop = null;

                                File file;

                                try {

                                                file = new File("D:/StudentInfo.txt");

                                                fop = new FileOutputStream(file);

                                                // if file doesnt exists, then create it

                                                if (!file.exists()) {

                                                                file.createNewFile();

                                                }

                                                BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));

                                System.out.println("How many Records you want to Enter:: ");

                                                int desValue=Integer.parseInt(bufferRead.readLine());

                        String studentName=null;

                                        String studentRoll=null;

                                        String studentMarks=null;

                                                // get the content in bytes

                                               

                        Console console = System.console();

                                                for(int i=1;i<=desValue;i++)

                                                {

                                                                System.out.print("Student Name?:: ");

                                                                studentName = bufferRead.readLine();

                                System.out.print("Student Roll Number?:: ");

                                                                studentRoll = bufferRead.readLine();

                                System.out.print("Student Marks?:: ");

                                                                studentMarks = bufferRead.readLine();

                                FileWriter outFile = new FileWriter("D:/StudentInfo.txt", true);

                                                                try {

                                                                                 PrintWriter out1 = new PrintWriter(outFile);

                                                                                try {

                                                                                out1.append(studentName+"\t"+studentRoll+"\t"+studentMarks);

                                                                                out1.println();

                                                                                               

                                                                                 } finally {

                                                                                 out1.close();

                                                                                                 }

                                                                                } finally {

                                                                 outFile.close();

                                                                }

                                                }

 

                                                fop.flush();

                                                fop.close();

 

                                                System.out.println("Done!");

 

                                } catch (IOException e) {

                                                e.printStackTrace();

                                } finally {

                                                try {

                                                                if (fop != null) {

                                                                                fop.close();

                                                                }

                                                } catch (IOException e) {

                                                                e.printStackTrace();

                                                }

                                }

                }

}

Output: -

After run above code you can go on file location and you can see entered Student Information is is write in the text file.

After creating a text file using above line of code then use the below lines of code for read the StudentInfo.txt file.

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

 

public class ReadStudentInformationFile {

 

                public static void main(String[] args) {

 

                                BufferedReader bufferedReader = null;

 

                                try {

 

                                                String studentInfoRead;

 

                                                bufferedReader= new BufferedReader(new FileReader("D:\\StudentInfo.txt"));

 

                                                while ((studentInfoRead = bufferedReader.readLine()) != null) {

                                                                System.out.println(studentInfoRead);

                                                }

 

                                } catch (IOException e) {

                                                e.printStackTrace();

                                } finally {

                                                try {

                                                                if (bufferedReader != null)bufferedReader.close();

                                                } catch (IOException ex) {

                                                                ex.printStackTrace();

                                                }

                                }

 

                }

}

Output: -


Don't want to miss updates? Please click the below button!

Follow MindStick