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+ }
0 commit comments