File Handling in JAVA reading writing text JAVA by Ravinder Nath Rajotiya - April 16, 2021April 16, 20210 Share on Facebook Share Send email Mail Print Print Table of Contents Toggle File HandlingWriting to a File:2. Reading from a fileThis is because we are trying to read data from a line where there is no data available.Solution : File Handling In this lecture we are going to understand how to write to a file and how to read from a file. Writing to a File: writing to a file is a three step process: 1. Step-1: Create a file – use PrintWrite class outputFile is an object to printWriter class and we initialize it by passing the file name as argument to new printWriter(), see below PrintWriter outputFile=new printWriter(fileName “outFile.txt”); 2. Step-2 Write to file – We can now use the objectname.println() method to write to the file as: outputFile.println(“This is first line”); 3. Close the file- After writing to the file, you need to close the file as outputFile.close(); class PrintWriter belongs to java.io.PrintWriter; package. In Intellij it is automatically imported as we write it. we also need to add throws IOException after the main() method. Throws_IOException is part of java.io.IOException package and gets added automatically. The exception is used to get exception of the type file does not exist or no data in file to read. package com.example; import javax.print.DocFlavor; import javax.swing.*; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class fileHandling { public static void main(String[] args) throws IOException { // creat and open a file, if file desnot exist it create, if exist it overwrites PrintWriter outputFile = new PrintWriter(“outputFile.txt”); outputFile.println(“This is first line”); outputFile.println(“This is second line”); outputFile.println(“This is third line”); outputFile.close(); } } This way the output will not be printed on the console, but will be printed to the named file. You can also observe that a file with the name “xxxx.txt” gets created and can be seen in navigation pane of Intellij. Double click to open and see what is there in that file. 2. Reading from a file To read from a file we use an object of class File as shown below: File file= new File(“fileName.txt”); We also need to use throws_IOException so that the main method can throw a file exception for example file not found exception we can read different type of data from a file. For this there are number of nextxxx() methods defined which can be used as argument in the System.out.println() method. as an example: System.out.println(inputFile.nextLine()) we can use various input methods with inputFile such as InputFile.nextInt() To read integer values InputFile.nextDouble() To read double InputFile.nextBoolean() To read Boolean inputFile.nextByte() To read byte inputFile.nextFloat() To read float inputFile.nextLine() To read line of text Example: reading from an existing file: package com.example; import javax.print.DocFlavor; import javax.swing.*; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class fileHandling { public static void main(String[] args) throws IOException { File file = new File(“outputFile.txt”); //this establishes a connection between file object and fileName if(file.exists() { // we need scanner class to input from KB , from a file Scanner inputFile = new Scanner(file); //we pass the file objet to scanner class to establish connection with named file System.out.println(inputFile.nextLine());// as son as file opens it points t beginging tp the first line. The nextLine() method extracts the data from A LINE AND POINTS TO NEXT LINE // and tkes the line pointer System.out.println(inputFile.nextLine()); System.out.println(inputFile.nextLine()); System.out.println(inputFile.nextLine()); inputFile.close(); } else { JOptionPane.showMessageDialog(null ,“File does not exist”); } } } Notice, there were three lines of text in the file and we have used four nextLine() methods for reading from the file. As the line number four does not exisit, JAVA throw as exception as: Exception in thread “main” java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at com.example.fileHandling.main(fileHandling.java:31) This is because we are trying to read data from a line where there is no data available. Solution : use while loop with argument as filename.hasNext() here is the complete program that do the correct reading from a file package com.example; import javax.print.DocFlavor; import javax.swing.*; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class fileHandling { public static void main(String[] args) throws IOException { File file = new File(“outputFile.txt”); if(file.exists()) { // checks that the line pointed has the data to be read, if yes then loop proceeds or exitsScanner inputFile = new Scanner(file); while(inputFile.hasNext()) { // reads the data from the line and moves the pointer to next line System.out.println(inputFile.nextLine()); } } else { JOptionPane.showMessageDialog(null ,“File does not exist”); } } } Share on Facebook Share Send email Mail Print Print