Java File Read via ByteBuffer readFileAsStringArray(File file)

Here you can find the source of readFileAsStringArray(File file)

Description

read File As String Array

License

Open Source License

Declaration

public static String[] readFileAsStringArray(File file) throws IOException 

Method Source Code


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

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

public class Main {
    private static Charset _charset = Charset.defaultCharset();

    public static String[] readFileAsStringArray(File file) throws IOException {
        return readFileAsString(file).split("\n");
    }/*from  w w  w. ja v a2s.c o  m*/

    public static String readFileAsString(File file) throws IOException {
        return new String(readFileAsByteArray(file), _charset);
    }

    public static byte[] readFileAsByteArray(File file) throws IOException {
        ByteBuffer bb;
        FileChannel fc = null;
        try {
            fc = new FileInputStream(file).getChannel();
            bb = ByteBuffer.allocate((int) fc.size());
            fc.read(bb);
        } finally {
            if (fc != null)
                fc.close();
        }
        return bb.array();
    }
}

Related

  1. readFile(String path)
  2. readFile(String path)
  3. readFile(String path)
  4. readFileAsByteArray(File file)
  5. readFileAsByteArray(String path)
  6. readFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)
  7. readFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)
  8. readFileDataIntoBufferBE(FileChannel fc, final int size)
  9. readFileFragment(FileChannel fc, long pos, int size)