Java File Read via ByteBuffer readFromFile(Path path)

Here you can find the source of readFromFile(Path path)

Description

read From File

License

Open Source License

Declaration

public static String readFromFile(Path path) 

Method Source Code

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

import java.io.*;

import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.*;

public class Main {
    public static String readFromFile(Path path) {
        try (FileInputStream stream = new FileInputStream(path.toFile())) {
            FileChannel fc = stream.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            return Charset.defaultCharset().decode(bb).toString();
        } catch (IOException e) {
            e.printStackTrace();/*from  w w  w. ja va 2  s .c  o m*/
        }
        return "";
    }
}

Related

  1. readFileToString(String path)
  2. readFlashString(DataInputStream s)
  3. readFloat(BufferedReader br)
  4. ReadFloat(InputStream is)
  5. readFromBuffer(byte[] buffer, int start, int end)
  6. readFromFile(String fileName)
  7. readFromSocket(SocketChannel channel)
  8. readFull(Reader in)
  9. readFully(final Reader input, final char[] buffer, final int offset, final int length)