Java File to String fileToString(File file)

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

Description

file To String

License

Open Source License

Declaration

public static String fileToString(File file) 

Method Source Code


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

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Main {
    public static String fileToString(File file) {
        FileInputStream fis = null;
        String str = "";

        try {//from  ww  w .j a  va2s.co m
            fis = new FileInputStream(file);
            int content;
            while ((content = fis.read()) != -1) {
                // convert to char and display it
                str += (char) content;
            }

            //System.out.println("After reading file");
            //System.out.println(str);
            return str;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return str;
    }
}

Related

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