Package min.lang

Source Code of min.lang.File

package min.lang;

import java.io.IOException;
import java.io.FileInputStream;
import java.nio.channels.FileChannel;
import java.nio.MappedByteBuffer;

public class File extends MinObject {

    // Taken from http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file
    static public String read(String path) throws MinException {
        try {
            FileInputStream stream = new FileInputStream(new java.io.File(path));
            try {
                FileChannel fc = stream.getChannel();
                MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
                return java.nio.charset.Charset.defaultCharset().decode(bb).toString();
            } finally {
                stream.close();
            }
        } catch (IOException e) {
            throw new MinException(e);
        }
    }

    static public boolean exists(String path) throws MinException {
        return new java.io.File(path).exists();
    }

}
TOP

Related Classes of min.lang.File

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.