001 /*
002 * The contents of this file are subject to the Mozilla Public
003 * License Version 1.1 (the "License"); you may not use this file
004 * except in compliance with the License. You may obtain a copy of
005 * the License at http://www.mozilla.org/MPL/
006 *
007 * Software distributed under the License is distributed on an "AS
008 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
009 * implied. See the License for the specific language governing
010 * rights and limitations under the License.
011 *
012 * The Original Code is Knowtator.
013 *
014 * The Initial Developer of the Original Code is University of Colorado.
015 * Copyright (C) 2005-2008. All Rights Reserved.
016 *
017 * Knowtator was developed by the Center for Computational Pharmacology
018 * (http://compbio.uchcs.edu) at the University of Colorado Health
019 * Sciences Center School of Medicine with support from the National
020 * Library of Medicine.
021 *
022 * Current information about Knowtator can be obtained at
023 * http://knowtator.sourceforge.net/
024 *
025 * Contributor(s):
026 * Philip V. Ogren <philip@ogren.info> (Original Author)
027 */
028 package edu.uchsc.ccp.util.io;
029
030 import java.io.BufferedReader;
031 import java.io.FileInputStream;
032 import java.io.IOException;
033 import java.io.InputStream;
034 import java.io.InputStreamReader;
035 import java.nio.charset.Charset;
036 import java.util.ArrayList;
037 import java.util.List;
038
039 public class FileReadingUtil {
040
041 /**
042 * Reads in a text file and returns it as an array of Strings. Risky from a
043 * scale point of view.
044 */
045 public static String[] readFileLines(String infile) throws IOException {
046 return readFileLines(new FileInputStream(infile));
047 }
048
049 public static String[] readFileLines(String infile, String comment) throws IOException {
050 return readFileLines(new FileInputStream(infile), comment);
051 }
052
053 public static String[] readFileLines(InputStream inputStream) throws IOException {
054 return readFileLines(inputStream, null);
055 }
056
057 public static String[] readFileLines(InputStream inputStream, String comment) throws IOException {
058
059 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
060
061 String line = new String();
062 List<String> lines = new ArrayList<String>();
063
064 while ((line = reader.readLine()) != null) {
065 if (comment == null || !line.startsWith(comment))
066 lines.add(line);
067 }
068
069 return (String[]) (lines.toArray(new String[lines.size()]));
070
071 }
072
073 public static String toString(String infile) throws IOException {
074 return toString(new FileInputStream(infile));
075 }
076
077 public static String toString(InputStream inputStream) throws IOException {
078 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
079 StringBuffer contents = new StringBuffer();
080
081 String line = new String();
082 while ((line = reader.readLine()) != null) {
083 contents.append(line + "\n");
084 }
085 return contents.toString();
086 }
087
088 public static void main(String[] args) {
089 try {
090 String[] lines = readFileLines(args[0]);
091 System.out.println("lines.length=" + lines.length);
092 for (int i = 0; i < lines.length; i++) {
093 System.out.println(lines[i]);
094 }
095 } catch (Exception exception) {
096 exception.printStackTrace();
097 }
098 }
099 }