Here you can find the source of fileToString(String file)
Parameter | Description |
---|---|
file | path to file. |
public static String fileToString(String file)
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileReader; public class Main { /**//from w w w . java 2 s . c o m * Read the contents of a file and place them in a string object. * * @param file * path to file. * @return String contents of the file. */ public static String fileToString(String file) { String contents = ""; File f = null; try { f = new File(file); if (f.exists()) { FileReader fr = null; try { fr = new FileReader(f); char[] template = new char[(int) f.length()]; fr.read(template); contents = new String(template); } catch (Exception e) { e.printStackTrace(); } finally { if (fr != null) { fr.close(); } } } } catch (Exception e) { e.printStackTrace(); } return contents; } }