Java File to String fileToString(String file)

Here you can find the source of fileToString(String file)

Description

Read a file into a String

License

Open Source License

Declaration

public static String fileToString(String file) throws IOException 

Method Source Code


//package com.java2s;
/*//from ww w  . j  av a 2  s  . c  o m
Copyright (C) 2010 Nick Crafford <nickcrafford@gmail.com>
    
This file is part of dbmojo
    
dbmojo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
dbmojo 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 General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with dbmojo.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.*;

public class Main {
    /** Read a file into a String */
    public static String fileToString(String file) throws IOException {
        StringBuilder lines = new StringBuilder();
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        String line = null;

        try {
            fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader);
            while ((line = bufferedReader.readLine()) != null) {
                lines.append(line);
            }
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
            if (fileReader != null) {
                fileReader.close();
            }
        }
        return lines.toString();
    }
}

Related

  1. fileToString(final File file, final String charsetName)
  2. fileToString(final String filePath)
  3. fileToString(InputStream file)
  4. fileToString(InputStream inputStream)
  5. fileToString(String file)
  6. fileToString(String file)
  7. fileToString(String file, String encoding)
  8. FileToString(String file_name)
  9. fileToString(String fileName)