Java File to String fileToStr(String strInput)

Here you can find the source of fileToStr(String strInput)

Description

file To Str

License

Open Source License

Declaration

public static String fileToStr(String strInput) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    public static String fileToStr(String strInput) throws IOException {

        // It is implicit that the input is a file so...

        File f = new File(strInput);

        // Ensure we have the capacity to prevent constant reallocation

        StringBuilder sb = new StringBuilder(new Long(f.length()).intValue());

        FileInputStream fIn = new FileInputStream(strInput);
        BufferedReader br = new BufferedReader(new InputStreamReader(fIn));
        String line = null;//from   ww  w  .  ja  v a2s  .  co m
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }

        br.close();

        return sb.toString();
    }
}

Related

  1. fileToStream(File source, OutputStream target)
  2. fileToStream(final File file)
  3. fileToStream(String filename)
  4. fileToString(File f)