Java Path File Write nio saveTo(String path, InputStream in)

Here you can find the source of saveTo(String path, InputStream in)

Description

Saves content read from input stream into a file.

License

LGPL

Parameter

Parameter Description
path path to file.
in input stream to read content from.

Exception

Parameter Description
IOException IO error
IllegalArgumentException if stream is null or path is null

Declaration

public static void saveTo(String path, InputStream in) throws IOException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.BufferedOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class Main {
    /**//from   www  . j a  va2 s.  c  om
     * Saves content read from input stream into a file.
     *
     * @param path path to file.
     * @param in  input stream to read content from.
     * @throws IOException IO error
     * @throws IllegalArgumentException if stream is null or path is null
     */
    public static void saveTo(String path, InputStream in) throws IOException {
        if (in == null)
            throw new IllegalArgumentException("input stream cannot be null");
        if (path == null)
            throw new IllegalArgumentException("path cannot be null");

        try (OutputStream out = new BufferedOutputStream(
                Files.newOutputStream(Paths.get(path), StandardOpenOption.CREATE, StandardOpenOption.APPEND))) {
            byte[] bytes = new byte[1024];
            for (int x = in.read(bytes); x != -1; x = in.read(bytes))
                out.write(bytes, 0, x);
        }
    }

    /**
     * Reads contents of the input stream fully and returns it as String. Sets UTF-8 encoding internally.
     *
     * @param in InputStream to read from.
     * @return contents of the input stream fully as String.
     * @throws IOException in case of IO error
     * @throws IllegalArgumentException if stream is null
     */
    public static String read(InputStream in) throws IOException {
        return read(in, StandardCharsets.UTF_8);
    }

    /**
     * Reads contents of the input stream fully and returns it as String.
     *
     * @param in InputStream to read from.
     * @param charset name of supported charset to use
     * @return contents of the input stream fully as String.
     * @throws IOException in case of IO error
     * @throws IllegalArgumentException if stream is null
     */
    public static String read(InputStream in, Charset charset) throws IOException {
        if (in == null)
            throw new IllegalArgumentException("input stream cannot be null");

        InputStreamReader reader = new InputStreamReader(in, charset);
        char[] buffer = new char[1024];
        StringBuilder sb = new StringBuilder();

        for (int x = reader.read(buffer); x != -1; x = reader.read(buffer)) {
            sb.append(buffer, 0, x);
        }
        return sb.toString();
    }
}

Related

  1. robustCheckWriteable(Path logFile)
  2. save(Object o, String path)
  3. saveFile(String path, String text)
  4. saveHeaders(Path path, ArrayList headers)
  5. saveProperties(Path filePath, Properties prop)
  6. saveToFile(String path, byte[] text)
  7. setPosixPermissions(String path, boolean ownerRead, boolean ownerWrite, boolean ownerExecute, boolean groupRead, boolean groupWrite, boolean groupExecute, boolean othersRead, boolean othersWrite, boolean othersExecute)
  8. srcImageToSavePath(String src, Path saveFolder)
  9. write(List list, String outputPath)