How to read file in Java – BufferedInputStream
Here you can see how to show and how to read a file in java withBufferedInputStream and DataInputStream classes.
The readLine() from the type DataInputStream is deprecated. Sun officially announced this method can not convert property from bytes to characters. It’s advised to use BufferedReader.
package com.mycompany.internationalyouthacuity;
/**
*
* @author rajesh shukla
*/
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class BufferedInputStreamExample {
public static void main(String[] args) {
File file = new File("C:\\testing.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0) {
System.out.println(dis.readLine());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
bis.close();
dis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
1. Files.newBufferedReader (Java 8)
In Java 8, there is a new method Files.newBufferedReader(Paths.get("file")) to return a BufferedReader
filename.txt
A
B
C
D
E
package com.mycompany.internationalyouthacuity;
/**
*
* @author rajesh shukla
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class FileExample1 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
try (BufferedReader br = Files.newBufferedReader(Paths.get("filename.txt"))) {
// read line by line
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
System.out.println(sb);
}
}
2. BufferedReader
2.1 A classic BufferedReader with JDK 1.7 try-with-resources to auto close the resources.
package com.mycompany.internationalyouthacuity;
/**
*
* @author rajesh shukla
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileExample2 {
public static void main(String[] args) {
try (FileReader reader = new FileReader("filename.txt");
BufferedReader br = new BufferedReader(reader)) {
// read line by line
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
}
}
2.2 In the old days, we have to close everything manually.
package com.mycompany.internationalyouthacuity;
/**
*
* @author rajesh shukla
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileExample3 {
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader("filename.txt");
br = new BufferedReader(fr);
// read line by line
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
System.err.format("IOException: %s%n", ex);
}
}
}
}
0 comments:
Post a Comment