Here you can find the source of readFileIntoString(String path)
Parameter | Description |
---|---|
path | the path |
Parameter | Description |
---|---|
IOException | Signals that an I/O exception has occurred. |
public static String readFileIntoString(String path) throws IOException
//package com.java2s; //License from project: LGPL import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; public class Main { /**// w ww . j a v a2 s.c om * Read the contents of a file into String. * * @param path the path * @return the string * @throws IOException Signals that an I/O exception has occurred. */ public static String readFileIntoString(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); try { FileChannel fileChannel = stream.getChannel(); MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); return Charset.defaultCharset().decode(mappedByteBuffer).toString(); } finally { if (stream != null) { stream.close(); } } } }