Writing a Text File Using Java

0
33

This tutorial will teach you on how to Writing Text file using Java. A text is a type of file contains a .txt extension. This tutorial uses BufferedWriter, FileWriter, IOException, and Scanner Libraries.  When the program performs writing the file, the created file is located inside your Netbeans project.  Netbeans project usually located in your Documents. Please follow all the steps to complete this tutorial.

Writing Text file Using Java Steps

  1. Create a new Java Class and name it what you want.
How Print Hello World Text in Java, Sum Two Integers Values using Java
How Print Hello World Text in Java

2. Insert the following import above your Class.

  1. import java.io.BufferedWriter;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.Scanner;

3. Inside your main method, declare the following variables. Declare a scanner, string, and Buffer Writer variables.

  1. //scanner
  2. Scanner input = new Scanner(System.in);
  3. //the file to write
  4. String fileName;
  5. //buffer writer
  6. BufferedWriter bWriter=null;

4. Insert the following statement for user input

  1. System.out.print("Enter file name: ");
  2. fileName = input.nextLine();

5. After step 4, insert the following codes below for writing the file.

  1. try{
  2. //encoding
  3. FileWriter fWriter = new FileWriter(fileName + ".txt");
  4. //file writer
  5. bWriter = new BufferedWriter(fWriter);
  6. //character to write
  7. bWriter.write("Marjohn I. Bebeloni");
  8. bWriter.newLine();
  9. bWriter.write("Tindalo St., Sainz, City of Mati");
  10. System.out.println("The file name " + fileName + " is created!");
  11. }catch(IOException ex){
  12. System.out.println("Error writing the file");
  13. }finally{
  14. //close the writer
  15. try{
  16. if (bWriter != null){
  17. bWriter.close();
  18. }
  19. }catch(IOException e){
  20. }
  21. }

6. The output should look like the image below.

7. Complete Source Code of Writing Text file using Java

  1. import java.io.BufferedWriter;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5. public class FileWriting {
  6. public static void main(String arg[]){
  7. //scanner
  8. Scanner input = new Scanner(System.in);
  9. //the file to write
  10. String fileName;
  11. //buffer writer
  12. BufferedWriter bWriter=null;
  13.  
  14. System.out.print("Enter file name: ");
  15. fileName = input.nextLine();
  16.  
  17. try{
  18. //encoding
  19. FileWriter fWriter = new FileWriter(fileName + ".txt");
  20. //file writer
  21. bWriter = new BufferedWriter(fWriter);
  22. //character to write
  23. bWriter.write("Marjohn I. Bebeloni");
  24. bWriter.newLine();
  25. bWriter.write("Tindalo St., Sainz, City of Mati");
  26. System.out.println("The file name " + fileName + " is created!");
  27. }catch(IOException ex){
  28. System.out.println("Error writing the file");
  29. }finally{
  30. //close the writer
  31. try{
  32. if (bWriter != null){
  33. bWriter.close();
  34. }
  35. }catch(IOException e){
  36. }
  37. }
  38. }
  39. }
Facebook Comments
(Visited 32 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here