Index

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");
ANSI codes foreground:
30 black
    31 red
    32 green
    33 yellow
    34 blue
    35 magenta
    36 cyan
    37 white
ANSI codes background:
40 black background
    41 red background
    42 green background
    43 yellow background
    44 blue background
    45 magenta background
    46 cyan background
    47 white background
modifiers
1 make bright (usually just bold)
    21 stop bright (normalizes boldness)
    4 underline
    24 stop underline
    0 clear all formatting