Skip to content

Commit 126bee2

Browse files
add a simple Java File IO example
1 parent c664f91 commit 126bee2

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package IO_example;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.Scanner;
6+
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
public class JavaFileIOExample {
10+
11+
public static void main(String... args) throws IOException {
12+
System.out.println("Program started.");
13+
readFileOnDisk();
14+
System.out.println("Program finished.");
15+
}
16+
17+
private static void readFileOnDisk() throws IOException {
18+
String file = "src/test/resources/sample_input.txt";
19+
Scanner scanner = new Scanner(new File(file));
20+
scanner.useDelimiter(" ");
21+
22+
assertTrue(scanner.hasNext());
23+
while (scanner.hasNext()) {
24+
System.out.println(scanner.next());
25+
}
26+
27+
scanner.close();
28+
}
29+
}

‎src/test/resources/sample_input.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cool test it is

0 commit comments

Comments
 (0)