Java File Read via ByteBuffer readFile(String path)

Here you can find the source of readFile(String path)

Description

read File

License

Open Source License

Declaration

static String readFile(String path) throws IOException 

Method Source Code

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

import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

public class Main {
    static String readFile(String path) throws IOException {
        FileInputStream stream = new FileInputStream(new File(path));
        try {//from w  w  w. j  a  va  2s . c om
            FileChannel fc = stream.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            return Charset.defaultCharset().decode(bb).toString();
        } finally {
            stream.close();
        }
    }
}

Related

  1. readFile(String name)
  2. readFile(String name)
  3. readFile(String path)
  4. readFile(String path)
  5. readFile(String path)
  6. readFileAsByteArray(File file)
  7. readFileAsByteArray(String path)
  8. readFileAsStringArray(File file)
  9. readFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)