package de.fuberlin.projecta.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
public class IOUtils {
public static String readMultilineStringFromStdin() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String data = "";
String line;
try {
while ((line = in.readLine()) != null && line.length() != 0) {
data += line + '\n';
}
} catch (IOException e) {
return "";
}
return data;
}
/**
* Stolen from
* http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file
*/
public static String readFile(String path) throws IOException {
FileInputStream stream = new FileInputStream(new File(path));
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0,
fc.size());
/* Instead of using default, pass in a decoder. */
return Charset.defaultCharset().decode(bb).toString();
} finally {
stream.close();
}
}
}