---
title: "Read and Write Text file using Java"  
description: "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 sa"  
author: "Vijay Shukla"  
published: 2013-10-04  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/602/read-and-write-text-file-using-java  
category: "java"  
tags: ["java"]  
reading_time: 11 minutes  

---

# Read and Write Text file using Java

In this [blog](https://www.mindstick.com/articles/12705/myths-and-misconception-about-blog) I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to make a small [demo](https://www.mindstick.com/forum/157370/how-to-swap-neighbor-characters-in-string-e-g-string-demo-would-be-edom) for read and write the [text file](https://www.mindstick.com/forum/2174/how-do-i-locate-a-particular-word-in-a-text-file) using java.\

Below [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) create a file with StudentInfo.txt [named](https://answers.mindstick.com/qa/44918/what-is-a-no-day-yet-named-motion) and save the [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) 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: -

![Read and Write Text file using Java](https://www.mindstick.com/blogs/d793b3a8-e370-4c9e-8cef-8e39736840df/images/658bc936-a812-4baa-a413-8952043ccad8.png)

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: -

![Read and Write Text file using Java](https://www.mindstick.com/blogs/d793b3a8-e370-4c9e-8cef-8e39736840df/images/94afb173-845e-40f9-9582-a9a3ef4461e4.png)

---

Original Source: https://www.mindstick.com/blog/602/read-and-write-text-file-using-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
