summaryrefslogtreecommitdiff
path: root/gi/posterior-regularisation/prjava/src/util/InputOutput.java
blob: da7f71bf3b9c7b8056d64eb4fb7dce6528d2f808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class InputOutput {

	/**
	 * Opens a file either compress with gzip or not compressed.
	 */
	public static BufferedReader openReader(String fileName) throws UnsupportedEncodingException, FileNotFoundException, IOException{
		System.out.println("Reading: " + fileName);
		BufferedReader reader;
		fileName = fileName.trim();
		if(fileName.endsWith("gz")){
			reader = new BufferedReader(
			new InputStreamReader(new GZIPInputStream(new FileInputStream(fileName)),"UTF8"));
		}else{
			reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(fileName), "UTF8"));
		}
		
		return reader;
	}
	
	
	public static PrintStream openWriter(String fileName) 
	throws UnsupportedEncodingException, FileNotFoundException, IOException{
		System.out.println("Writting to file: " + fileName);
		PrintStream writter;
		fileName = fileName.trim();
		if(fileName.endsWith("gz")){
			writter = new PrintStream(new GZIPOutputStream(new FileOutputStream(fileName)),
					true, "UTF-8");

		}else{
			writter = new PrintStream(new FileOutputStream(fileName),
					true, "UTF-8");

		}
		
		return writter;
	}
	
	public static Properties readPropertiesFile(String fileName) {
		Properties properties = new Properties();
		try {
			properties.load(new FileInputStream(fileName));
		} catch (IOException e) {
			e.printStackTrace();
			throw new AssertionError("Wrong properties file " + fileName);
		}
		System.out.println(properties.toString());
		
		return properties;
	}
}