Java URL Load readContentFromFile(URL u, String encoding)

Here you can find the source of readContentFromFile(URL u, String encoding)

Description

read Content From File

License

Open Source License

Declaration

public static String readContentFromFile(URL u, String encoding) 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.io.BufferedReader;

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

import java.io.InputStream;
import java.io.InputStreamReader;

import java.net.URL;

public class Main {
    public static final int INTERNAL_BUFFER_SIZE = 16 * 1024;

    public static String readContentFromFile(URL u, String encoding) {
        String content = "";

        try {/*from w w w  . j  a va2s .co  m*/
            int readLength = 0;
            char[] readBuffer = new char[INTERNAL_BUFFER_SIZE];

            BufferedReader uReader = null;
            StringBuffer buf = new StringBuffer();
            char c;
            long toRead = Long.MAX_VALUE;

            if (encoding != null && !encoding.equalsIgnoreCase("")) {
                uReader = new BufferedReader(new InputStreamReader(u.openStream(), encoding), INTERNAL_BUFFER_SIZE);
            } else {
                uReader = new BufferedReader(new InputStreamReader(u.openStream()), INTERNAL_BUFFER_SIZE);
            }

            // read gtom source into buffer
            while (toRead > 0 && (readLength = uReader.read(readBuffer, 0, INTERNAL_BUFFER_SIZE)) != -1) {
                if (toRead < readLength) {
                    //well, if toRead(long) is less than readLenght(int)
                    //then there can be no overflow, so the cast is safe
                    readLength = (int) toRead;
                }

                buf.append(readBuffer, 0, readLength);
                toRead -= readLength;
            }

            // 4.close reader
            uReader.close();

            content = new String(buf);
        } catch (java.net.MalformedURLException urlEx) {
            urlEx.printStackTrace();
        } catch (java.io.IOException ioEx) {
            ioEx.printStackTrace();
        }

        return content;
    }

    public static String readContentFromFile(String inputFilename, String encoding) {
        String content = "", st = "";

        try {
            File df = new File(inputFilename);
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(df), encoding));

            content = reader.readLine();
            while ((st = reader.readLine()) != null) {
                content += "\n" + st;
            }

            // 4.close reader
            reader.close();
        } catch (java.net.MalformedURLException urlEx) {
            urlEx.printStackTrace();
        } catch (java.io.IOException ioEx) {
            ioEx.printStackTrace();
        }

        return content;
    }

    public static String readContentFromFile(InputStream inputStream) {
        String content = "";

        try {
            int readLength = 0;
            char[] readBuffer = new char[INTERNAL_BUFFER_SIZE];

            BufferedReader uReader = null;
            StringBuffer buf = new StringBuffer();
            char c;
            long toRead = Long.MAX_VALUE;

            uReader = new BufferedReader(new InputStreamReader(inputStream), INTERNAL_BUFFER_SIZE);

            // read gtom source into buffer
            while (toRead > 0 && (readLength = uReader.read(readBuffer, 0, INTERNAL_BUFFER_SIZE)) != -1) {
                if (toRead < readLength) {
                    //well, if toRead(long) is less than readLenght(int)
                    //then there can be no overflow, so the cast is safe
                    readLength = (int) toRead;
                }

                buf.append(readBuffer, 0, readLength);
                toRead -= readLength;
            }

            // 4.close reader
            uReader.close();

            content = new String(buf);
        } catch (java.net.MalformedURLException urlEx) {
            urlEx.printStackTrace();
        } catch (java.io.IOException ioEx) {
            ioEx.printStackTrace();
        }

        return content;
    }
}

Related

  1. readAsStringUTF8(URL url)
  2. readBytes(URL url)
  3. readBytesFromURL(String url)
  4. readBytesFromUrl(URL url)
  5. readContent(URL url)
  6. readContents(URL url)
  7. readContentsToArray(URL url)
  8. readContentsToList(URL url)
  9. readContentsToString(URL url)