Java Search String Read Text File and Find String
Sometimes while working with files, nosotros need to read the file to String in Coffee. Today we volition wait into various ways to read the file to String in Java.
Java read file to String
There are many ways to read a file to String in Java. We will explore the following ways in this tutorial.
- Java read file to String using BufferedReader
- Read file to String in java using FileInputStream
- Java read file to string using Files grade
- Read file to String using Scanner form
- Coffee read file to string using Apache Commons IO FileUtils form
Now permit's look into these classes and read a file to String.
Java read file to String using BufferedReader
We tin can use BufferedReader
readLine
method to read a file line past line. All nosotros have to exercise is suspend these to a StringBuilder object with newline character. Beneath is the code snippet to read the file to Cord using BufferedReader.
BufferedReader reader = new BufferedReader(new FileReader(fileName)); StringBuilder stringBuilder = new StringBuilder(); String line = zippo; String ls = Arrangement.getProperty("line.separator"); while ((line = reader.readLine()) != nada) { stringBuilder.append(line); stringBuilder.suspend(ls); } // delete the concluding new line separator stringBuilder.deleteCharAt(stringBuilder.length() - 1); reader.close(); String content = stringBuilder.toString();
There is another efficient mode to read file to String using BufferedReader and char array.
BufferedReader reader = new BufferedReader(new FileReader(fileName)); StringBuilder stringBuilder = new StringBuilder(); char[] buffer = new char[10]; while (reader.read(buffer) != -ane) { stringBuilder.append(new String(buffer)); buffer = new char[10]; } reader.close(); String content = stringBuilder.toString();
Read file to String in coffee using FileInputStream
We can use FileInputStream and byte array to read file to String. You lot should apply this method to read non-char based files such every bit prototype, video etc.
FileInputStream fis = new FileInputStream(fileName); byte[] buffer = new byte[x]; StringBuilder sb = new StringBuilder(); while (fis.read(buffer) != -1) { sb.append(new String(buffer)); buffer = new byte[10]; } fis.close(); String content = sb.toString();
Java read file to string using Files class
We tin utilize Files utility class to read all the file content to cord in a unmarried line of lawmaking.
String content = new String(Files.readAllBytes(Paths.get(fileName)));
Read file to String using Scanner grade
The scanner grade is a quick way to read a text file to cord in coffee.
Scanner scanner = new Scanner(Paths.get(fileName), StandardCharsets.UTF_8.name()); Cord content = scanner.useDelimiter("\\A").side by side(); scanner.close();
Coffee read file to string using Apache Commons IO FileUtils grade
If you lot are using Apache Commons IO in your project, and then this is a simple and quick way to read the file to cord in java.
Cord content = FileUtils.readFileToString(new File(fileName), StandardCharsets.UTF_8);
Java read file to Cord example
Here is the final program with proper exception handling and showing all the different ways to read a file to string.
package com.journaldev.files; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Scanner; import org.apache.commons.io.FileUtils; public class JavaReadFileToString { /** * This grade shows different means to read complete file contents to Cord * * @param args * @throws IOException */ public static void main(Cord[] args) { String fileName = "/Users/pankaj/Downloads/myfile.txt"; String contents = readUsingScanner(fileName); Arrangement.out.println("*****Read File to Cord Using Scanner*****\n" + contents); contents = readUsingApacheCommonsIO(fileName); Organisation.out.println("*****Read File to String Using Apache Commons IO FileUtils*****\n" + contents); contents = readUsingFiles(fileName); System.out.println("*****Read File to String Using Files Grade*****\n" + contents); contents = readUsingBufferedReader(fileName); System.out.println("*****Read File to Cord Using BufferedReader*****\n" + contents); contents = readUsingBufferedReaderCharArray(fileName); Organization.out.println("*****Read File to String Using BufferedReader and char assortment*****\due north" + contents); contents = readUsingFileInputStream(fileName); System.out.println("*****Read File to Cord Using FileInputStream*****\north" + contents); } private static String readUsingBufferedReaderCharArray(Cord fileName) { BufferedReader reader = zilch; StringBuilder stringBuilder = new StringBuilder(); char[] buffer = new char[10]; try { reader = new BufferedReader(new FileReader(fileName)); while (reader.read(buffer) != -1) { stringBuilder.suspend(new String(buffer)); buffer = new char[x]; } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) endeavour { reader.close(); } catch (IOException e) { e.printStackTrace(); } } return stringBuilder.toString(); } private static String readUsingFileInputStream(String fileName) { FileInputStream fis = zero; byte[] buffer = new byte[10]; StringBuilder sb = new StringBuilder(); try { fis = new FileInputStream(fileName); while (fis.read(buffer) != -1) { sb.append(new String(buffer)); buffer = new byte[10]; } fis.close(); } catch (IOException e) { eastward.printStackTrace(); } finally { if (fis != null) try { fis.close(); } grab (IOException e) { e.printStackTrace(); } } return sb.toString(); } individual static String readUsingBufferedReader(String fileName) { BufferedReader reader = cipher; StringBuilder stringBuilder = new StringBuilder(); try { reader = new BufferedReader(new FileReader(fileName)); String line = cypher; Cord ls = System.getProperty("line.separator"); while ((line = reader.readLine()) != null) { stringBuilder.suspend(line); stringBuilder.suspend(ls); } // delete the terminal ls stringBuilder.deleteCharAt(stringBuilder.length() - 1); } take hold of (IOException e) { e.printStackTrace(); } finally { if (reader != nada) endeavor { reader.close(); } catch (IOException e) { e.printStackTrace(); } } return stringBuilder.toString(); } private static String readUsingFiles(String fileName) { endeavour { return new String(Files.readAllBytes(Paths.become(fileName))); } catch (IOException e) { eastward.printStackTrace(); return naught; } } individual static String readUsingApacheCommonsIO(Cord fileName) { endeavor { return FileUtils.readFileToString(new File(fileName), StandardCharsets.UTF_8); } catch (IOException due east) { due east.printStackTrace(); return zip; } } private static String readUsingScanner(Cord fileName) { Scanner scanner = nada; try { scanner = new Scanner(Paths.go(fileName), StandardCharsets.UTF_8.name()); // we can use Delimiter regex as "\\A", "\\Z" or "\\z" String information = scanner.useDelimiter("\\A").next(); return information; } catch (IOException due east) { due east.printStackTrace(); render null; } finally { if (scanner != null) scanner.close(); } } }
Yous can apply whatsoever of the to a higher place ways to read file content to cord in java. All the same, it's not advisable if the file size is huge considering you might face up out of retention errors.
References:
- BufferedReader API Medico
- Files API Doc
Source: https://www.journaldev.com/875/java-read-file-to-string