Java File to String fileToString(File f)

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

Description

Read a file and return its contents as a string.

License

Open Source License

Parameter

Parameter Description
f The file to read.

Return

The contents of the file as a string.

Declaration

public static String[] fileToString(File f) 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class Main {
    /**/*  w  ww . jav  a 2 s. c  o  m*/
     * Read a file and return its contents as a string.
     * 
     * @param f
     *          The file to read.
     * @return The contents of the file as a string.
     */
    public static String[] fileToString(File f) {
        try {
            StringBuffer buff = new StringBuffer();
            BufferedReader in = new BufferedReader(new FileReader(f));
            String line = null;
            while ((line = in.readLine()) != null) {
                buff.append(line);
                buff.append("\n");
            }
            in.close();
            return new String[] { buff.toString() };
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

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)
  7. fileToString(File f, String encoding)
  8. fileToString(File file)
  9. fileToString(File file)