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