Java FileInputStream Read readFile(File f)

Here you can find the source of readFile(File f)

Description

Reads in the file's contents, skipping all line terminators (newlines or carriage returns).

License

Open Source License

Parameter

Parameter Description
f the file

Exception

Parameter Description
RuntimeException if something goes wrong

Return

the file contents (without line terminators)

Declaration

static String readFile(File f) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * ALMA - Atacama Large Millimeter Array
 * Copyright (c) ESO - European Southern Observatory, 2011
 * (in the framework of the ALMA collaboration).
 * All rights reserved./*from   ww  w  . ja  va 2 s .  c  om*/
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *******************************************************************************/

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

public class Main {
    /**
     * Reads in the file's contents, skipping all
     * line terminators (newlines or carriage returns).
     * 
     * @param f the file
     * @return the file contents (without line terminators)
     * @throws RuntimeException if something goes wrong
     */
    static String readFile(File f) {
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(f));
            StringBuilder buf = new StringBuilder((int) f.length());
            int c;
            while (true) {
                c = bis.read();
                if (c == -1)
                    break;
                if (c == '\r' || c == '\n')
                    continue;
                buf.append((char) c);
            }

            return buf.toString();

        } catch (IOException e) {
            throw new RuntimeException(
                    "workdir is " + System.getProperty("user.dir") + ", couldn't read file contents: " + e);

        } finally {
            try {
                bis.close();
            } catch (Exception e1) {
            }
        }
    }
}

Related

  1. loadStackableItems(String fileName)
  2. loadTxtFile(String fileName)
  3. readFile( String filename)
  4. readFile(File f)
  5. readFile(File f)
  6. readFile(File f)
  7. readFile(File f)
  8. readFile(File f)
  9. readFile(File f)