1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.util;
17 import java.io.BufferedReader;
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;
28 @SuppressWarnings("WeakerAccess")
29 public final class FileUtil {
30 public static String readLine(final RandomAccessFile file, final long startPos) throws IOException {
32 return file.readLine();
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)))) {
39 while ((line = in.readLine()) != null) {
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)))) {
50 while ((line = in.readLine()) != null) {
51 result.append(line).append("\n");
54 return result.toString();
57 public static void writeStringToUTF8File(final String string, final File file) {
58 throw new IllegalStateException();
61 public static void printString(final File file, final String s) throws IOException {
62 final PrintStream out = new PrintStream(new FileOutputStream(file));