-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReadFileExample.java
More file actions
29 lines (27 loc) · 844 Bytes
/
ReadFileExample.java
File metadata and controls
29 lines (27 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.codinginterview;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class ReadFileExample {
public static void main(String[] args) {
System.out.println("Read a File Example");
}
private static void readFile(String path) throws IOException {
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
String str;
while((str = br.readLine()) != null ){
System.out.println(str);
}
br.close();
}
private static void readFile2(String path) throws IOException {
File file = new File(path);
Scanner sc = new Scanner(file);
while(sc.hasNextLine()){
System.out.println(sc.nextLine());
}
}
}