Approach
1: Using Scanner
String
content = new Scanner(new File(filePath)).useDelimiter("\\Z").next();
Approach
2: Using Files.readAllBytes.
String
content = new String(Files.readAllBytes(Paths.get(filePath)),
StandardCharsets.UTF_8);
public static String readFileContentViaStream(String filePath) throws IOException {
validateInput(filePath);
try (FileInputStream fis = new FileInputStream(new File(filePath))) {
StringBuilder builder = new StringBuilder();
int content;
while ((content = fis.read()) != -1) {
builder.append((char) content);
}
return builder.toString();
}
}
Approach
4: Using FileChannel.
public static String readFileContentViaChannel(String filePath) throws IOException {
validateInput(filePath);
try (FileInputStream fin = new FileInputStream(filePath)) {
FileChannel fileChannel = fin.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int noOfBytesRead;
StringBuilder builder = new StringBuilder();
while ((noOfBytesRead = fileChannel.read(buffer)) != -1) {
String str = new String(buffer.array());
builder.append(str);
buffer.flip();
}
return builder.toString();
}
}
Find the
below working application.
package com.sample.app;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
public class App {
private static void validateInput(String filePath) {
if (filePath == null || filePath.isEmpty()) {
throw new IllegalArgumentException("filePath is null or empty");
}
File file = new File(filePath);
if (!file.exists()) {
throw new RuntimeException("File is not exist");
}
if (file.isDirectory()) {
throw new RuntimeException("File is a directory");
}
}
public static String readFileContentViaScanner(String filePath) throws FileNotFoundException {
validateInput(filePath);
return new Scanner(new File(filePath)).useDelimiter("\\Z").next();
}
public static String readFileContentViaReadingAllBytes(String filePath) throws IOException {
validateInput(filePath);
return new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
}
public static String readFileContentViaStream(String filePath) throws IOException {
validateInput(filePath);
try (FileInputStream fis = new FileInputStream(new File(filePath))) {
StringBuilder builder = new StringBuilder();
int content;
while ((content = fis.read()) != -1) {
builder.append((char) content);
}
return builder.toString();
}
}
public static String readFileContentViaChannel(String filePath) throws IOException {
validateInput(filePath);
try (FileInputStream fin = new FileInputStream(filePath)) {
FileChannel fileChannel = fin.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int noOfBytesRead;
StringBuilder builder = new StringBuilder();
while ((noOfBytesRead = fileChannel.read(buffer)) != -1) {
String str = new String(buffer.array());
builder.append(str);
buffer.flip();
}
return builder.toString();
}
}
public static void main(String args[]) throws IOException {
String filePath = "data.txt";
String fileContent1 = readFileContentViaScanner(filePath);
String fileContent2 = readFileContentViaReadingAllBytes(filePath);
String fileContent3 = readFileContentViaStream(filePath);
String fileContent4 = readFileContentViaChannel(filePath);
System.out.println(fileContent1);
System.out.println(fileContent2);
System.out.println(fileContent3);
System.out.println(fileContent4);
}
}
You may
like
No comments:
Post a Comment