Index

Table of contents

Java IO

Streams

String to OutputStream
public static void writeStringToOutputStream(String data, OutputStream os) {
        try (PrintWriter writer = new PrintWriter(os)) {
            writer.write(data);
        }
    }
InputStream to OutputStream
public static void copyStream(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.read(buffer)) != -1)
    {
        output.write(buffer, 0, bytesRead);
    }
}
InputStream to String
public static String toString(InputStream is) {
        try (Scanner scanner = new Scanner(is)) {
            scanner.useDelimiter("\\A");
            return scanner.hasNext() ? scanner.next() : "";
        }
    }
Reader to String
public static String toString(Reader reader) throws IOException {
        BufferedReader in = new BufferedReader(reader);
        StringBuilder result = new StringBuilder();

        String line = null;
        while ((line = in.readLine()) != null) {
            result.append(line);
        }
        return result.toString();
    }
String to InputStream
public static InputStream toInputStream(String data) {
	new ByteArrayInputStream(data.getBytes("UTF-8"))
}

Files

Copy File or directory
public static void copyFile(File from, File to) throws IOException {
	File[] children = from.listFiles();
	if (children == null || children.length == 0) {
		if (from.isDirectory()) {
			to.mkdirs();
		} else {
			to.getParentFile().mkdirs();
			Files.copy(from.toPath(), to.toPath(), StandardCopyOption.REPLACE_EXISTING);
		}
	} else {
		for (File child : children) {
			copyFile(child, new File(to, child.getName()));
		}
	}
}
Move File
public static void moveFile(File from, File to) throws IOException {
	Files.move(from.toPath(), to.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
Read Lines
public static List<String> readLines(File file) throws IOException {
        List<String> lines = new ArrayList<String>();
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line = br.readLine();
            while (line != null) {
                lines.add(line);
                line = br.readLine();
            }
        }
        return lines;
    }
Stream Lines
public static Stream<String> stream(File file) throws IOException {
        if (!file.exists()) {
            throw new IOException("File does not exist: " + file.getCanonicalPath());
        }
        return Files.lines(file.toPath());
    }
File to String
public static String readFileToString(File file) throws IOException {
        List<String> lines = readLines(file);
        StringBuilder builder = new StringBuilder();
        for (String line : lines) {
            builder.append(line).append("\r\n");
        }
        return builder.toString();
    }
Write Lines
public static void writeLines(File file, Collection<String> lines) throws IOException {
        file.getParentFile().mkdirs();
	try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
	new FileOutputStream(file)))) {

            for (String line : lines) {
                bw.write(line);
                bw.newLine();
            }
        }
}
List nested Files
public static List<File> listFiles(File file) {
        List<File> files = new ArrayList<File>();

        if (file.isDirectory()) {
            File[] children = file.listFiles();
            for (File child : children) {
                files.addAll(listFiles(child));
            }
        }
        if (file.isFile() && file.getName().endsWith(".java")) {
            files.add(file);
        }
        return files;
    }

System.out

print a simple line of text
System.out.println("text");
print a message in yellow (linux)
System.out.println("\033[33m" + message + "\033[0m");
print a line in bold red against black background (linux)
System.out.println("\033[1;31;40m" + message + "\033[0m");
advanced example
System.out.println("\033[0;1;33mbanana \033[1;37;44mocean\033[0;1;31m stop\033[0m");
more
https://misc.flogisoft.com/bash/tip_colors_and_formatting