Subversion Repositories SmartDukaan

Rev

Rev 1034 | Go to most recent revision | 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
 
14
/**
15
 * Utility functions 
16
 * 
17
 * @author rajveer
18
 *
19
 */
20
public class FileUtils {
21
 
786 rajveer 22
	public static String read(String filename) throws IOException  {
507 rajveer 23
 
24
		File f = new File(filename);
25
		if(!f.exists()) {
26
			System.out.println(filename + " - does not exist");
27
			throw new FileNotFoundException();
28
		}
29
 
30
		StringBuilder textString = new StringBuilder();
31
		FileInputStream fis = null;
32
		try {
33
			fis = new FileInputStream(f);
34
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
35
			String line;
36
			while((line = br.readLine()) != null){
37
				textString.append(line+"\n");
38
			}
39
		} catch (IOException e) {
40
			e.printStackTrace();
41
			throw e;
42
		}
43
		finally {
44
			if(fis != null) {
45
				fis.close();
46
			}
47
		}
48
		System.out.println("Reading complete");
49
		return textString.toString();
50
	}
51
 
52
 
53
	/**
54
	 * 
55
	 * @param filename
56
	 * @throws Exception
57
	 */
58
	public static boolean delete(String filename) throws Exception {
59
		File f = new File(filename);
60
		if(f.exists()) {
61
			return f.delete();
62
		}
63
		return true;
64
	}
65
}