Java BufferedReader Read readFile(File f)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(File f) throws IOException 

Method Source Code


//package com.java2s;
/*//from ww  w.  j a  v  a2 s.  c  om
    
Copyright 2012 Daniel Gonzalez Pe?a, Osvaldo Gra?a
    
    
This file is part of the bicycle Project. 
    
bicycle Project is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
bicycle Project 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 Public License for more details.
    
You should have received a copy of the GNU Lesser Public License
along with bicycle Project.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.BufferedReader;
import java.io.File;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static String readFile(File f) throws IOException {
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new FileReader(f));
        boolean first = true;
        for (String line = reader.readLine(); line != null; line = reader.readLine(), first = false) {
            if (!first)
                builder.append("\n");
            builder.append(line);
        }
        return builder.toString();
    }

    public static void append(File f, String s) throws IOException {
        FileWriter writer = new FileWriter(f);
        writer.write(s);
        writer.close();
    }
}

Related

  1. loadXML(String fileName)
  2. loadXMLDocumentFromClasspath(String resourcePath)
  3. loadY(InputStream filePath)
  4. readFile(File f)
  5. readFile(File f)
  6. readFile(File f)
  7. readFile(File f)
  8. readFile(File f)
  9. readFile(File f)