Java File to String fileToString(File f)

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

Description

Reads in a File and dumps it into a String.

License

Open Source License

Parameter

Parameter Description
f a parameter

Exception

Parameter Description
FileNotFoundException an exception

Return

a String containing all text from f.

Declaration

public static String fileToString(File f) throws FileNotFoundException 

Method Source Code


//package com.java2s;
//it under the terms of the GNU Lesser General Public License as 

import java.io.*;
import java.util.*;

public class Main {
    /**// ww  w  .ja  v a 2 s . c  om
     * Reads in a File and dumps it into a String.
     * 
     * @param f
     * @return a String containing all text from f.
     * @throws FileNotFoundException
     */
    public static String fileToString(File f) throws FileNotFoundException {
        Scanner s = new Scanner(f);
        StringBuilder sb = new StringBuilder();
        while (s.hasNextLine()) {
            sb.append(s.nextLine() + '\n');
        }
        s.close();
        return sb.toString();
    }
}

Related

  1. fileToString(File f)
  2. fileToString(File f)
  3. fileToString(File f)
  4. fileToString(File f)
  5. fileToString(File f)
  6. fileToString(File f, String encoding)
  7. fileToString(File file)
  8. fileToString(File file)
  9. fileToString(File file)