Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6ca856a

Browse files
committed
Add read lines snippet
1 parent841ca94 commit6ca856a

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

‎README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Update the sample application with the snippet and add a test for it. After prov
1313
*[Generic two array concatenation](#generic-two-array-concatenation)
1414
*[Generic N array concatenation](#generic-N-array-concatenation)
1515

16+
###File
17+
*[Read lines from file to string list](#read-lines-from-file-to-string-list)
18+
1619
###Math
1720
*[Factorial](#factorial)
1821
*[Fibonacci](#fibonacci)
@@ -54,6 +57,18 @@ Update the sample application with the snippet and add a test for it. After prov
5457

5558
[⬆ back to top](#table-of-contents)
5659

60+
##File
61+
62+
###Read lines from file to string list
63+
64+
```java
65+
publicstaticList<String> readLines(String filename) throwsIOException {
66+
returnFiles.readAllLines(newFile(filename).toPath());
67+
}
68+
```
69+
70+
[⬆ back to top](#table-of-contents)
71+
5772
##Math
5873

5974
###Fibonacci

‎src/main/java/Library.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
importjava.io.File;
2+
importjava.io.IOException;
3+
importjava.nio.file.Files;
14
importjava.util.Arrays;
5+
importjava.util.List;
26

37
/*
48
* Java Snippets code
@@ -74,4 +78,14 @@ public static int factorial(int number) {
7478
publicstaticStringreverseString(Strings) {
7579
returnnewStringBuilder(s).reverse().toString();
7680
}
81+
82+
/**
83+
* Read file as list of strings
84+
* @param filename the filename to read from
85+
* @return list of strings
86+
* @throws IOException
87+
*/
88+
publicstaticList<String>readLines(Stringfilename)throwsIOException {
89+
returnFiles.readAllLines(newFile(filename).toPath());
90+
}
7791
}

‎src/main/resources/somelines.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo
2+
bar
3+
baz

‎src/test/java/LibraryTest.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
importorg.junit.Test;
2+
3+
importjava.io.IOException;
4+
importjava.util.List;
5+
26
importstaticorg.junit.Assert.*;
37

48
/*
@@ -74,9 +78,32 @@ public void testFactorial() {
7478
assertEquals(362880,Library.factorial(9));
7579
assertEquals(3628800,Library.factorial(10));
7680
}
81+
82+
/**
83+
* Tests for {@link Library#reverseString(String)}
84+
*/
7785
@Test
7886
publicvoidtestReverseString() {
7987
assertEquals("oof",Library.reverseString("foo"));
8088
assertEquals("ÖÄÅ321FED cba",Library.reverseString("abc DEF123ÅÄÖ"));
8189
}
90+
91+
/**
92+
* Tests for {@link Library#readLines(String)}
93+
* @throws IOException
94+
*/
95+
@Test
96+
publicvoidtestReadLines()throwsIOException {
97+
List<String>somelines =Library.readLines("src/main/resources/somelines.txt");
98+
assertEquals(3,somelines.size());
99+
assertEquals("foo",somelines.get(0));
100+
assertEquals("bar",somelines.get(1));
101+
assertEquals("baz",somelines.get(2));
102+
try {
103+
Library.readLines("some/nonexistent/filename.txt");
104+
fail();
105+
}catch (IOExceptione) {
106+
// catched the expected exception
107+
}
108+
}
82109
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp