Skip to content

Commit 5af1f54

Browse files
committed
Add NIO Files readLines utility and tests
Introduces a new `io.microsphere.nio.file.Files` utility class with overloaded `readLines` methods for `File` and `Path`, supporting both default and explicit `Charset`. The implementation uses `java.nio.file.Files.newInputStream` and delegates line parsing to `IOUtils.readLines`. Adds `FilesTest` to verify consistent behavior between `Path` and `File` inputs and expected content reading from `test.txt`.
1 parent 7358c62 commit 5af1f54

2 files changed

Lines changed: 240 additions & 0 deletions

File tree

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.nio.file;
19+
20+
import io.microsphere.annotation.Nonnull;
21+
import io.microsphere.io.IOUtils;
22+
import io.microsphere.util.Utils;
23+
24+
import java.io.File;
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.nio.charset.Charset;
28+
import java.nio.file.Path;
29+
30+
import static io.microsphere.nio.charset.CharsetUtils.DEFAULT_CHARSET;
31+
import static java.nio.file.Files.newInputStream;
32+
33+
/**
34+
* The utilties class of {@link File} based on NIO
35+
*
36+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
37+
* @see Path
38+
* @since 1.0.0
39+
*/
40+
public abstract class Files implements Utils {
41+
42+
/**
43+
* Reads all lines from the given file and returns them as an array of strings,
44+
* using the default {@link Charset} for decoding.
45+
*
46+
* <p>This method reads the entire content of the file, converts it to a string using
47+
* the default charset, and then splits the string into lines based on the system line separator.
48+
* The file input stream is automatically closed after this operation.</p>
49+
*
50+
* <h3>Example Usage</h3>
51+
* <pre>{@code
52+
* File file = new File("/example.txt");
53+
* try {
54+
* String[] lines = Files.readLines(file);
55+
* for (String line : lines) {
56+
* System.out.println(line);
57+
* }
58+
* } catch(IOException e) {
59+
* // Handle the exception
60+
* }
61+
* }</pre>
62+
*
63+
* @param file the file to read from
64+
* @return an array of strings representing the lines read from the file
65+
* @throws IOException if an I/O error occurs during reading from the file
66+
* @see #readLines(File, Charset)
67+
*/
68+
@Nonnull
69+
public static String[] readLines(File file) throws IOException {
70+
return readLines(file, DEFAULT_CHARSET);
71+
}
72+
73+
/**
74+
* Reads all lines from the given file and returns them as an array of strings,
75+
* using the specified {@link Charset} for decoding.
76+
*
77+
* <p>This method reads the entire content of the file, converts it to a string using
78+
* the provided charset, and then splits the string into lines based on the system line separator.
79+
* The file input stream is automatically closed after this operation.</p>
80+
*
81+
* <h3>Example Usage</h3>
82+
* <pre>{@code
83+
* File file = new File("/example.txt");
84+
* Charset charset = java.nio.charset.StandardCharsets.UTF_8;
85+
* try {
86+
* String[] lines = Files.readLines(file, charset);
87+
* for (String line : lines) {
88+
* System.out.println(line);
89+
* }
90+
* } catch(IOException e) {
91+
* // Handle the exception
92+
* }
93+
* }</pre>
94+
*
95+
* @param file the file to read from
96+
* @param charset the {@link Charset} to use for decoding the file content
97+
* @return an array of strings representing the lines read from the file
98+
* @throws IOException if an I/O error occurs during reading from the file
99+
* @see IOUtils#readLines(InputStream, Charset)
100+
*/
101+
@Nonnull
102+
public static String[] readLines(File file, Charset charset) throws IOException {
103+
return readLines(file.toPath(), charset);
104+
}
105+
106+
/**
107+
* Reads all lines from the given file path and returns them as an array of strings,
108+
* using the default {@link Charset} for decoding.
109+
*
110+
* <p>This method reads the entire content of the file, converts it to a string using
111+
* the default charset, and then splits the string into lines based on the system line separator.
112+
* The file input stream is automatically closed after this operation.</p>
113+
*
114+
* <h3>Example Usage</h3>
115+
* <pre>{@code
116+
* Path filePath = Paths.get("/example.txt");
117+
* try {
118+
* String[] lines = Files.readLines(filePath);
119+
* for (String line : lines) {
120+
* System.out.println(line);
121+
* }
122+
* } catch(IOException e) {
123+
* // Handle the exception
124+
* }
125+
* }</pre>
126+
*
127+
* @param filePath the file path
128+
* @return an array of strings representing the lines read from the file
129+
* @throws IOException if an I/O error occurs during reading from the file
130+
* @see #readLines(Path, Charset)
131+
*/
132+
@Nonnull
133+
public static String[] readLines(Path filePath) throws IOException {
134+
return readLines(filePath, DEFAULT_CHARSET);
135+
}
136+
137+
/**
138+
* Reads all lines from the given file path and returns them as an array of strings,
139+
* using the specified {@link Charset} for decoding.
140+
*
141+
* <p>This method reads the entire content of the file, converts it to a string using
142+
* the provided charset, and then splits the string into lines based on the system line separator.
143+
* The file input stream is automatically closed after this operation.</p>
144+
*
145+
* <h3>Example Usage</h3>
146+
* <pre>{@code
147+
* Path filePath = Paths.get("/example.txt");
148+
* Charset charset = java.nio.charset.StandardCharsets.UTF_8;
149+
* try {
150+
* String[] lines = Files.readLines(filePath, charset);
151+
* for (String line : lines) {
152+
* System.out.println(line);
153+
* }
154+
* } catch(IOException e) {
155+
* // Handle the exception
156+
* }
157+
* }</pre>
158+
*
159+
* @param filePath the file path
160+
* @param charset the {@link Charset} to use for decoding the file content
161+
* @return an array of strings representing the lines read from the file
162+
* @throws IOException if an I/O error occurs during reading from the file
163+
* @see IOUtils#readLines(InputStream, Charset)
164+
*/
165+
@Nonnull
166+
public static String[] readLines(Path filePath, Charset charset) throws IOException {
167+
try (InputStream inputStream = newInputStream(filePath)) {
168+
return IOUtils.readLines(inputStream, charset);
169+
}
170+
}
171+
172+
private Files() {
173+
}
174+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.nio.file;
19+
20+
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.io.File;
25+
import java.io.IOException;
26+
import java.net.URL;
27+
import java.nio.file.Path;
28+
import java.nio.file.Paths;
29+
30+
import static io.microsphere.nio.file.Files.readLines;
31+
import static io.microsphere.util.ClassLoaderUtils.getResource;
32+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
33+
import static org.junit.jupiter.api.Assertions.assertEquals;
34+
35+
/**
36+
* {@link Files} Test
37+
*
38+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
39+
* @see Files
40+
* @since 1.0.0
41+
*/
42+
class FilesTest {
43+
44+
private static final String TEST_FILE_PATH = "test.txt";
45+
46+
private final URL TEST_FILE_RESOURCE = getResource(TEST_FILE_PATH);
47+
48+
private Path testFilePath;
49+
50+
private File testFile;
51+
52+
@BeforeEach
53+
void setUp() throws Throwable {
54+
this.testFilePath = Paths.get(TEST_FILE_RESOURCE.toURI());
55+
this.testFile = this.testFilePath.toFile();
56+
}
57+
58+
@Test
59+
void testReadLines() throws IOException {
60+
String[] lines = readLines(this.testFilePath);
61+
String[] lines2 = readLines(this.testFile);
62+
assertArrayEquals(lines, lines2);
63+
assertEquals(1, lines.length);
64+
assertEquals("test", lines[0]);
65+
}
66+
}

0 commit comments

Comments
 (0)