]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/util/FileUtil.java
Consistent EOL format.
[DictionaryPC.git] / src / com / hughes / util / FileUtil.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.util;
16
17 import java.io.BufferedReader;
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.io.PrintStream;
24 import java.io.RandomAccessFile;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 @SuppressWarnings("WeakerAccess")
29 public final class FileUtil {
30     public static String readLine(final RandomAccessFile file, final long startPos) throws IOException {
31         file.seek(startPos);
32         return file.readLine();
33     }
34
35     public static List<String> readLines(final File file) throws IOException {
36         final List<String> result = new ArrayList<>();
37         try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
38             String line;
39             while ((line = in.readLine()) != null) {
40                 result.add(line);
41             }
42         }
43         return result;
44     }
45
46     public static String readToString(final File file) throws IOException {
47         StringBuilder result = new StringBuilder();
48         try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
49             String line;
50             while ((line = in.readLine()) != null) {
51                 result.append(line).append("\n");
52             }
53         }
54         return result.toString();
55     }
56
57     public static void writeStringToUTF8File(final String string, final File file) {
58         throw new IllegalStateException();
59     }
60
61     public static void printString(final File file, final String s) throws IOException {
62         final PrintStream out = new PrintStream(new FileOutputStream(file));
63         out.print(s);
64         out.close();
65     }
66
67 }