Subversion Repositories SmartDukaan

Rev

Rev 2146 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
507 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.utils;
5
 
6
 
7
import java.io.BufferedReader;
8
import java.io.File;
9
import java.io.FileInputStream;
10
import java.io.FileNotFoundException;
11
import java.io.IOException;
12
import java.io.InputStreamReader;
13
 
2453 chandransh 14
import org.apache.log4j.Logger;
15
 
507 rajveer 16
/**
17
 * Utility functions 
18
 * 
19
 * @author rajveer
20
 *
21
 */
22
public class FileUtils {
23
 
2453 chandransh 24
	private static Logger log = Logger.getLogger(Class.class);
25
 
26
	public static String read(String filename) throws IOException {
507 rajveer 27
 
28
		File f = new File(filename);
29
		if(!f.exists()) {
2453 chandransh 30
			throw new FileNotFoundException(filename + " - does not exist");
507 rajveer 31
		}
32
 
33
		StringBuilder textString = new StringBuilder();
34
		FileInputStream fis = null;
35
		try {
36
			fis = new FileInputStream(f);
37
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
38
			String line;
39
			while((line = br.readLine()) != null){
40
				textString.append(line+"\n");
41
			}
2453 chandransh 42
		} finally {
43
			if(fis != null)
507 rajveer 44
				fis.close();
45
		}
2453 chandransh 46
		log.debug("Reading " + filename + " completed");
507 rajveer 47
		return textString.toString();
48
	}
49
 
50
 
51
	/**
52
	 * 
53
	 * @param filename
54
	 * @throws Exception
55
	 */
56
	public static boolean delete(String filename) throws Exception {
57
		File f = new File(filename);
58
		if(f.exists()) {
59
			return f.delete();
60
		}
61
		return true;
62
	}
63
}