Java Path File Read nio reader(String path)

Here you can find the source of reader(String path)

Description

reader

License

Open Source License

Declaration

public static String reader(String path) throws IOException 

Method Source Code


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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.Channel;

public class Main {
    public static String reader(String path) throws IOException {
        InputStream in = fileIn(path);
        String val = getString(in).toString();
        close(in);/*from w  w w . j  a  v a 2s.com*/
        return val;
    }

    public static InputStream fileIn(String path) throws FileNotFoundException {
        return fileIn(new File(path));
    }

    public static InputStream fileIn(File file) throws FileNotFoundException {
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new FileInputStream(file);
    }

    public static StringBuilder getString(Reader reader) throws IOException {
        StringBuilder sb = new StringBuilder();
        char[] cbuf = new char[65535];
        int len;
        while (-1 != (len = reader.read(cbuf))) {
            sb.append(cbuf, 0, len);
        }
        return sb;
    }

    public static StringBuilder getString(InputStream is) throws IOException {
        StringBuilder res = new StringBuilder();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader read = new BufferedReader(isr);
        String line;
        while (null != (line = read.readLine())) {
            res.append(line);
        }
        return res;
    }

    public static void close(InputStream in) {
        if (null != in) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Socket socket) {
        if (null != socket) {
            if (!socket.isClosed()) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void close(ServerSocket socket) {
        if (null != socket) {
            if (!socket.isClosed()) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void close(OutputStream out) {
        if (null != out) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Reader reader) {
        if (null != reader) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Writer writer) {
        if (null != writer) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Channel channel) {
        if (null != channel) {
            if (channel.isOpen()) {
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void append(String path, byte[] b) throws IOException {
        OutputStream out = fileOut(path, false);
        out.write(b);
        close(out);
    }

    public static void append(String path, String content) throws IOException {
        append(path, content, "UTF-8");
    }

    public static void append(String path, String content, String charset) throws IOException {
        OutputStream out = fileOut(path, true);
        out.write(content.getBytes(charset));
        close(out);
    }

    public static OutputStream fileOut(String path, boolean append) throws FileNotFoundException {
        return fileOut(new File(path), append);
    }

    public static OutputStream fileOut(File file, boolean append) throws FileNotFoundException {
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new FileOutputStream(file, append);
    }
}

Related

  1. readAllLines(String path)
  2. readAllLinesOrExit(Path path)
  3. readAndConsume(String filePath, Consumer consumer)
  4. readAsString(Path path)
  5. readAsString(String path)
  6. readers(String path)
  7. readFile(Path directory, String... parts)
  8. readFile(Path file)
  9. readFile(Path path)