Press enter to see results or esc to cancel.


How to create or read TXT / NotePad File using JAVA

In this video I have shown how to create and read text or Notepad file using java.

Create java Text file,
Update the path of output file as per your need.

Write_TXT_File.java

package chillyfacts.com;
import java.io.PrintWriter;
public class Write_TXT_File {
    public static void main(String[] args) {
    PrintWriter writer;
    try {
 writer = new PrintWriter("C:\\Users\\MIRITPC\\Desktop\\jas\\test1.txt", "UTF-8");
       writer.println("Line 111111");
       writer.println("line 22222");
       writer.println("line 33333");
       writer.println("line 44444");
       writer.close();
       System.out.println("finished");
     } catch (Exception e) {
       e.printStackTrace();
     }
    }
}

To read text file from you can use below code.
Read_TXT_File.java

package chillyfacts.com;
import java.io.BufferedReader;
import java.io.FileReader;
public class Read_TXT_File {
    public static void main(String[] args) {
        try {
            BufferedReader br = null;
            FileReader fr = null;
            fr = new FileReader("C:\\Users\\MIRITPC\\Desktop\\jas\\test1.txt");
            br = new BufferedReader(fr);
            String sCurrentLine = "";
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Console out put.

Line 111111
line 22222
line 33333
line 44444

Download the complete project here.

Read_Write_TXT.rar

Comments

Leave a Comment