Java Find and Read File Given by User

There are many ways to read a text file in java. Let's look at java read text file different methods one by one.

Java read text file

java read file, java read text file

There are many ways to read a text file in java. A text file is made of characters, so nosotros tin can employ Reader classes. There are some utility classes likewise to read a text file in coffee.

  1. Coffee read text file using Files class
  2. Read text file in java using FileReader
  3. Java read text file using BufferedReader
  4. Using Scanner class to read text file in java

Now allow'southward look at examples showing how to read a text file in coffee using these classes.

Java read text file using java.nio.file.Files

We can utilize Files class to read all the contents of a file into a byte assortment. Files grade also has a method to read all lines to a list of string. Files course is introduced in Java vii and it'south expert if y'all want to load all the file contents. Y'all should use this method only when you are working on pocket-size files and yous demand all the file contents in retentiveness.

                          String fileName = "/Users/pankaj/source.txt"; Path path = Paths.get(fileName); byte[] bytes = Files.readAllBytes(path); List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);                      

Read text file in coffee using java.io.FileReader

You can use FileReader to get the BufferedReader and and so read files line by line. FileReader doesn't support encoding and works with the system default encoding, then information technology's not a very efficient way of reading a text file in java.

                          Cord fileName = "/Users/pankaj/source.txt"; File file = new File(fileName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line; while((line = br.readLine()) != aught){     //process the line     System.out.println(line); }                      

Coffee read text file using coffee.io.BufferedReader

BufferedReader is good if you desire to read file line by line and procedure on them. It's practiced for processing the large file and it supports encoding too.

BufferedReader is synchronized, so read operations on a BufferedReader can safely be washed from multiple threads. BufferedReader default buffer size is 8KB.

                          String fileName = "/Users/pankaj/source.txt"; File file = new File(fileName); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, cs); BufferedReader br = new BufferedReader(isr);  Cord line; while((line = br.readLine()) != null){      //process the line      System.out.println(line); } br.shut();                      

Using scanner to read text file in java

If you lot want to read file line past line or based on some java regular expression, Scanner is the course to employ.

Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods. The scanner grade is not synchronized and hence not thread safe.

                          Path path = Paths.go(fileName); Scanner scanner = new Scanner(path); Arrangement.out.println("Read text file using Scanner"); //read line by line while(scanner.hasNextLine()){     //procedure each line     Cord line = scanner.nextLine();     System.out.println(line); } scanner.shut();                      

Coffee Read File Example

Here is the instance class showing how to read a text file in java. The example methods are using Scanner, Files, BufferedReader with Encoding support and FileReader.

                          parcel com.journaldev.files;  import java.io.BufferedReader; import coffee.io.File; import java.io.FileInputStream; import coffee.io.FileReader; import java.io.IOException; import coffee.io.InputStreamReader; import coffee.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import coffee.nio.file.Path; import coffee.nio.file.Paths; import coffee.util.List; import coffee.util.Scanner;  public class JavaReadFile {      public static void chief(String[] args) throws IOException {         Cord fileName = "/Users/pankaj/source.txt";                  //using Java vii Files form to process small files, get complete file data         readUsingFiles(fileName);                  //using Scanner class for large files, to read line by line         readUsingScanner(fileName);                  //read using BufferedReader, to read line by line         readUsingBufferedReader(fileName);         readUsingBufferedReaderJava7(fileName, StandardCharsets.UTF_8);         readUsingBufferedReader(fileName, StandardCharsets.UTF_8);                  //read using FileReader, no encoding back up, non efficient         readUsingFileReader(fileName);     }      individual static void readUsingFileReader(Cord fileName) throws IOException {         File file = new File(fileName);         FileReader fr = new FileReader(file);         BufferedReader br = new BufferedReader(fr);         Cord line;         Arrangement.out.println("Reading text file using FileReader");         while((line = br.readLine()) != cipher){             //process the line             Arrangement.out.println(line);         }         br.close();         fr.close();              }      private static void readUsingBufferedReader(String fileName, Charset cs) throws IOException {         File file = new File(fileName);         FileInputStream fis = new FileInputStream(file);         InputStreamReader isr = new InputStreamReader(fis, cs);         BufferedReader br = new BufferedReader(isr);         String line;         System.out.println("Read text file using InputStreamReader");         while((line = br.readLine()) != zip){             //process the line             System.out.println(line);         }         br.shut();              }      private static void readUsingBufferedReaderJava7(String fileName, Charset cs) throws IOException {         Path path = Paths.get(fileName);         BufferedReader br = Files.newBufferedReader(path, cs);         String line;         Arrangement.out.println("Read text file using BufferedReader Java vii improvement");         while((line = br.readLine()) != nil){             //process the line             System.out.println(line);         }         br.close();     }      private static void readUsingBufferedReader(String fileName) throws IOException {         File file = new File(fileName);         FileReader fr = new FileReader(file);         BufferedReader br = new BufferedReader(fr);         String line;         System.out.println("Read text file using BufferedReader");         while((line = br.readLine()) != null){             //process the line             Organisation.out.println(line);         }         //close resource         br.shut();         fr.close();     }      private static void readUsingScanner(String fileName) throws IOException {         Path path = Paths.go(fileName);         Scanner scanner = new Scanner(path);         System.out.println("Read text file using Scanner");         //read line by line         while(scanner.hasNextLine()){             //process each line             String line = scanner.nextLine();             System.out.println(line);         }         scanner.close();     }      individual static void readUsingFiles(String fileName) throws IOException {         Path path = Paths.get(fileName);         //read file to byte array         byte[] bytes = Files.readAllBytes(path);         System.out.println("Read text file using Files form");         //read file to String listing         @SuppressWarnings("unused") 		List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);         System.out.println(new String(bytes));     }  }                      

The choice of using a Scanner or BufferedReader or Files to read file depends on your projection requirements. For example, if you lot are just logging the file, you can employ Files and BufferedReader. If you are looking to parse the file based on a delimiter, you should employ Scanner class.

Before I end this tutorial, I want to mention about RandomAccessFile. Nosotros tin can utilize this to read text file in coffee.

                          RandomAccessFile file = new RandomAccessFile("/Users/pankaj/Downloads/myfile.txt", "r"); String str;  while ((str = file.readLine()) != null) { 	Organisation.out.println(str); } file.close();                      

That'due south all for java read text file example programs.

Java Find and Read File Given by User

Source: https://www.journaldev.com/867/java-read-text-file

0 Response to "Java Find and Read File Given by User"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel