Java — How to read a File | Code Factory

Java read file using BufferedReader

package com.example.java.programming.file;import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author code.factory
*
*/
public class BufferedReaderExample {
public static void main(String... args) {
Path filePath = Paths.get("test.txt");
Charset charset = StandardCharsets.UTF_8;
try (BufferedReader bufferedReader = Files.newBufferedReader(filePath, charset)) {
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}
Hello 
Code Factory!!!

Java read file line by line using Files.readAllLines()

package com.example.java.programming.file;import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
/**
* @author code.factory
*
*/
public class FilesReadAllLinesExample {
public static void main(String... args) {
Path filePath = Paths.get("test.txt");
Charset charset = StandardCharsets.UTF_8;
try {
List<String> lines = Files.readAllLines(filePath, charset);
for (String line : lines) {
System.out.println(line);
}
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}
Hello 
Code Factory!!!

Java read file line by line using Files.lines()

package com.example.java.programming.file;import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author code.factory
*
*/
public class FilesLinesExample {
public static void main(String... args) {
Path filePath = Paths.get("test.txt");
Charset charset = StandardCharsets.UTF_8;
try { Files.lines(filePath, charset).forEach(System.out::println); } catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}
Hello 
Code Factory!!!

Java read file line by line using LineNumberReader

package com.example.java.programming.file;import java.io.BufferedReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author code.factory
*
*/
public class LineNumberReaderExample {
public static void main(String... args) {
Path filePath = Paths.get("test.txt");
Charset charset = StandardCharsets.UTF_8;
try (BufferedReader bufferedReader = Files.newBufferedReader(filePath, charset);
LineNumberReader lineNumberReader = new LineNumberReader(bufferedReader)) {
String line;
while ((line = lineNumberReader.readLine()) != null) {
System.out.format("Line %d: %s%n", lineNumberReader.getLineNumber(), line);
}
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}
Line 1: Hello 
Line 2: Code Factory!!!

Java read binary file (image file) using BufferedInputStream

package com.example.java.programming.file;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* @author code.factory
*
*/
public class BufferedInputStreamImageCopyExample {
public static void main(String... args) {
try (InputStream inputStream = Files.newInputStream(Paths.get("bike.jpg"));
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
OutputStream outputStream = Files.newOutputStream(Paths.get("bike-copy.jpg"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)) {
byte[] buffer = new byte[4096];
int numBytes;
while ((numBytes = bufferedInputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, numBytes);
}
System.out.println("Done");
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}

Java read file into []byte using Files.readAllBytes()

package com.example.java.programming.file;import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* @author code.factory
*
*/
public class FilesReadAllBytesExample {
public static void main(String... args) {
try {
byte[] data = Files.readAllBytes(Paths.get("test.txt"));
System.out.println(new String(data));
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}
Hello 
Code Factory!!!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Beginner-Friendly Guide to PyTorch and How it Works from Scratch

How to Build Optimal Docker Images

Essential Configuration Management Interview Questions & Answers

Agile Methodologies and Team Development

Webinar: Forecasting the Stock Market with Watson Studio (14th August, 2020)

TryHackMe — Steel Mountain w/o MSF

BitBook Listing with Benefits

Rethinking Integration Solutions: From SOA to the Future

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Understanding CI/CD Pipeline, its Elements, and Characteristics of a Good CI/CD Pipeline

Access modifiers in java

Leading a world-class API experience

Ski boot debugging