Java Text File Read by Charset readFile(Bundle bundle, String path, Charset cs)

Here you can find the source of readFile(Bundle bundle, String path, Charset cs)

Description

read File

License

Open Source License

Parameter

Parameter Description
bundle the bundle that contains the text resource
path the path of the resource
cs the character set to use

Exception

Parameter Description
IOException if the resource cannot be read

Return

the content as string (including line delimiters)

Declaration

public static String readFile(Bundle bundle, String path, Charset cs) throws IOException 

Method Source Code


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

import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import org.osgi.framework.Bundle;

public class Main {
    /**//from  w  w  w. ja  v a2s. c  o m
     * Uses UTF-8 character encoding
     * @param bundle the bundle that contains the text resource
     * @param path the path of the resource
     * @return the content as string (including line delimiters)
     * @throws IOException if the resource cannot be read
     */
    public static String readFile(Bundle bundle, String path) throws IOException {
        return readFile(bundle, path, StandardCharsets.UTF_8);
    }

    /**
     * @param bundle the bundle that contains the text resource
     * @param path the path of the resource
     * @param cs the character set to use
     * @return the content as string (including line delimiters)
     * @throws IOException if the resource cannot be read
     */
    public static String readFile(Bundle bundle, String path, Charset cs) throws IOException {
        URL jsonUrl = bundle.getEntry(path);
        try (Scanner scanner = new Scanner(jsonUrl.openStream(), cs.name())) {
            scanner.useDelimiter("\\A");
            if (scanner.hasNext()) {
                return scanner.next();
            } else {
                throw new IOException("no content");
            }
        }
    }
}

Related

  1. readAsString(InputStream is, CharsetDecoder decoder)
  2. readCharsWithEncoding(File file, Charset charset)
  3. reader(Class baseClass, String resourceName, Charset charset)
  4. reader(InputStream in, String charsetName)
  5. readerBuffered(final File file, final Charset charset)
  6. readFile(File f, Charset chst)
  7. readFile(File file, Charset charset)
  8. readFile(File file, Charset charset)
  9. readFile(File file, Charset cs)