Java examples for File Path IO:Text File
file read to String
//package com.java2s; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; public class Main { public static void main(String[] argv) throws Exception { String filePath = "java2s.com"; System.out.println(file_read(filePath)); }/*from w w w. j a v a 2 s .c om*/ public static String file_read(String filePath) { try { FileInputStream fin = new FileInputStream(new File(filePath)); String ret = convertStreamToString(fin); fin.close(); return ret; } catch (Exception e) { return ""; } } public static String convertStreamToString(InputStream is) throws Exception { BufferedReader reader = new BufferedReader( new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } reader.close(); return sb.toString(); } }