Read Files In Java

A Discussion is being conducted on How to read files in Java with examples to make learning easier.



Java Files Read – Txt

You learned how to create and write to files in the past chapter.

The text file we created in the prior chapter is read using the Scanner class in the example below.

But before reading the file make sure you write anything to the file manually or by the use of write() method, which was discussed earlier in the java write and create file.

This example illustrates the retrieving of data from FirstFile.txt which we created in the prior session.

Java Read Files Example:1 

import java.io.File; // Import the File class from Java.io package import java.io.FileNotFoundException; // This class is imported in order to tackle issues import java.util.Scanner; // Here we imported the Scanner class from java.util Packagepublic class Main{ public static void main(String[] args) {try { File ample = new File("FirstFile.txt"); Scanner mrx_read = new Scanner(ample); while (mrx_read.hasNextLine()) { String mrx_data = mrx_read.nextLine(); System.out.println(mrx_data); } mrx_read.close(); } catch (FileNotFoundException e){ System.out.println("An Error occurred while reading the file"); e.printStackTrace(); } } }

Java Files Read example 1Correspondingly, if we want to read the content of the file SecondFile.txt which we created earlier, follow the given example:

 

Example:2 

import java.io.File; // Import the File class from Java.io package import java.io.FileNotFoundException; // This class is imported in order to tackle issues import java.util.Scanner; // Here we imported the Scanner class from java.util Packagepublic class Main{ public static void main(String[] args) {try { File mrx = new File("SecondFile.txt"); Scanner ample_read = new Scanner(mrx); while (ample_read.hasNextLine()) { String mrx_data = ample_read.nextLine(); System.out.println(mrx_data); } ample_read.close(); } catch (FileNotFoundException e){ System.out.println("An Error occurred while reading the file"); e.printStackTrace(); } } }

Java Files Read – PDF

We can also read the content of the .pdf file which we created in the last post:

Example: 

import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; public class PdfToConsole { public static void main(String args[]) throws IOException { //Loading an existing document File file = new File("mrexample\\java\\SecondPdf.pdf"); PDDocument document = PDDocument.load(file); //Instantiate PDFTextStripper class PDFTextStripper pdfStripper = new PDFTextStripper(); //Retrieving text from PDF document String text = pdfStripper.getText(document); System.out.println(text); //Closing the document document.close(); } }

Java files read pdf

 


Java Files Read – CSV

To read the .csv file check out below examples:

Example: 

import java.io.File; // Import the File class from Java.io package import java.io.FileNotFoundException; // This class is imported in order to tackle issues import java.util.Scanner; // Here we imported the Scanner class from java.util Packagepublic class Main{ public static void main(String[] args) {try { File mrx = new File("NewFile.csv"); Scanner ample_read = new Scanner(mrx); while (ample_read.hasNextLine()) { String mrx_data = ample_read.nextLine(); System.out.println(mrx_data); } ample_read.close(); } catch (FileNotFoundException e){ System.out.println("An Error occurred while reading the file"); e.printStackTrace(); } } }

Java files read csv


Java Files Read – JSON

Similarly we can read JSON file in Java as portray in below examples:

Example: 

import java.io.File; // Import the File class from Java.io package import java.io.FileNotFoundException; // This class is imported in order to tackle issues import java.util.Scanner; // Here we imported the Scanner class from java.util Packagepublic class Main{ public static void main(String[] args) {try { File mrx = new File("NewFile.json"); Scanner ample_read = new Scanner(mrx); while (ample_read.hasNextLine()) { String mrx_data = ample_read.nextLine(); System.out.println(mrx_data); } ample_read.close(); } catch (FileNotFoundException e){ System.out.println("An Error occurred while reading the file"); e.printStackTrace(); } } }

 


Get File Information

You can use the File methods in Java Files Read to learn more about a file.

Example: 

import java.io.File; // Import the File class from Java.io package public class Main{ public static void main(String[] args) {File mrx=new File("FirstFile.txt");if(mrx.exists()){ System.out.println("\nFile name: "+mrx.getName()); // Returns name of the file (FirstFile.txt) System.out.println("\n"+mrx.getName()+" Absolute Path: "+mrx.getAbsolutePath()); // Returns the location of file in computer System.out.println("\n"+mrx.getName()+"'s length in bytes: "+mrx.length()); // Gives length of file in bytes System.out.println("\nIs my file "+mrx.canRead()+" Readable?"); System.out.println("Answer: "+mrx.canRead()); // Shows if file is readable or not System.out.println("\nCan I write to my file "+mrx.getName()+" ?"); System.out.println("Answer: "+mrx.canWrite()); // Replies true if file can be written else false System.out.println("\nCan I execute my file "+mrx.getName()+" ?"); System.out.println("Answer: "+mrx.canExecute()); // Returns true is file is executable or otherwise} else{ System.out.println("File not Found"); } } }

Similarly, We can also obtain all the details of the file “SecondFile.txt” by following the example given below:

Example: 

import java.io.File; // Import the File class from Java.io package public class Main{ public static void main(String[] args) {File mrx=new File("SecondFile.txt");if(mrx.exists()){ System.out.println("\nFile name: "+mrx.getName()); // Returns name of the file (FirstFile.txt) System.out.println("\n"+mrx.getName()+" Absolute Path: "+mrx.getAbsolutePath()); // Returns the location of file in computer System.out.println("\n"+mrx.getName()+"'s length in bytes: "+mrx.length()); // Gives length of file in bytes System.out.println("\nIs my file "+mrx.canRead()+" Readable?"); System.out.println("Answer: "+mrx.canRead()); // Shows if file is readable or not System.out.println("\nCan I write to my file "+mrx.getName()+" ?"); System.out.println("Answer: "+mrx.canWrite()); // Replies true if file can be written else false System.out.println("\nCan I execute my file "+mrx.getName()+" ?"); System.out.println("Answer: "+mrx.canExecute()); // Returns true is file is executable or otherwise} else{ System.out.println("File not Found"); } } }

We can also obtain the details of the .pdf file we created above:

Example: 

import java.io.File; // Import the File class from Java.io package public class Main{ public static void main(String[] args) {File mrx=new File("NewFile.pdf");if(mrx.exists()){ System.out.println("\nFile name: "+mrx.getName()); // Returns name of the file (FirstFile.txt) System.out.println("\n"+mrx.getName()+" Absolute Path: "+mrx.getAbsolutePath()); // Returns the location of file System.out.println("\n"+mrx.getName()+"'s length in bytes: "+mrx.length()); // Gives length of file in bytes System.out.println("\nIs my file "+mrx.canRead()+" Readable?"); System.out.println("Answer: "+mrx.canRead()); // Shows if file is readable or not System.out.println("\nCan I write to my file "+mrx.getName()+" ?"); System.out.println("Answer: "+mrx.canWrite()); // Replies true if file can be written else false System.out.println("\nCan I execute my file "+mrx.getName()+" ?"); System.out.println("Answer: "+mrx.canExecute()); // Returns true is file is executable or otherwise} else{ System.out.println("File not Found"); } } }

Correspondingly, we can get the information from the .csv file which we created in the session before this using Java’s createNewFile() method and wrote to it using write() method:

Example: 

import java.io.File; // Import the File class from Java.io package public class Main{ public static void main(String[] args) {File mrx=new File("NewFile.csv");if(mrx.exists()){ System.out.println("\nFile name: "+mrx.getName()); // Returns name of the file (FirstFile.txt) System.out.println("\n"+mrx.getName()+" Absolute Path: "+mrx.getAbsolutePath()); // Returns the location of file System.out.println("\n"+mrx.getName()+"'s length in bytes: "+mrx.length()); // Gives length of file in bytes System.out.println("\nIs my file "+mrx.canRead()+" Readable?"); System.out.println("Answer: "+mrx.canRead()); // Shows if file is readable or not System.out.println("\nCan I write to my file "+mrx.getName()+" ?"); System.out.println("Answer: "+mrx.canWrite()); // Replies true if file can be written else false System.out.println("\nCan I execute my file "+mrx.getName()+" ?"); System.out.println("Answer: "+mrx.canExecute()); // Returns true is file is executable or otherwise} else{ System.out.println("File not Found"); } } }

The details of .json File can be obtained in the similar way:

Example: 

import java.io.File; // Import the File class from Java.io package public class Main{ public static void main(String[] args) {File mrx=new File("NewFile.json");if(mrx.exists()){ System.out.println("\nFile name: "+mrx.getName()); // Returns name of the file (FirstFile.txt) System.out.println("\n"+mrx.getName()+" Absolute Path: "+mrx.getAbsolutePath()); // Returns the location of file System.out.println("\n"+mrx.getName()+"'s length in bytes: "+mrx.length()); // Gives length of file in bytes System.out.println("\nIs my file "+mrx.canRead()+" Readable?"); System.out.println("Answer: "+mrx.canRead()); // Shows if file is readable or not System.out.println("\nCan I write to my file "+mrx.getName()+" ?"); System.out.println("Answer: "+mrx.canWrite()); // Replies true if file can be written else false System.out.println("\nCan I execute my file "+mrx.getName()+" ?"); System.out.println("Answer: "+mrx.canExecute()); // Returns true is file is executable or otherwise} else{ System.out.println("File not Found"); } } }
Reminder: The Java API provides a wide variety of classes that can be used to read and write files in Java. This includes FileWriter, BufferedWriter, FileInputStream, FileOutputStream, Files, Scanner, FileReader, BufferedReader, etc.
Which one to use relies upon the Java version you’re using, whether you need to read characters or bytes, the length of the file/lines, and other factors.

You now have a better understanding of Java Files Read and how it works. To learn more about how to delete a files in Java, refer to the Java Delete Files chapter.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *