Subversion Repositories SmartDukaan

Rev

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

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