Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | 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.FileOutputStream;
12
import java.io.IOException;
13
import java.io.InputStreamReader;
14
import java.io.ObjectInputStream;
15
import java.io.ObjectOutputStream;
16
 
17
/**
18
 * Utility functions 
19
 * 
20
 * @author rajveer
21
 *
22
 */
23
public class FileUtils {
24
 
786 rajveer 25
	public static String read(String filename) throws IOException  {
507 rajveer 26
 
27
		File f = new File(filename);
28
		if(!f.exists()) {
29
			System.out.println(filename + " - does not exist");
30
			throw new FileNotFoundException();
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
			}
42
		} catch (IOException e) {
43
			e.printStackTrace();
44
			throw e;
45
		}
46
		finally {
47
			if(fis != null) {
48
				fis.close();
49
			}
50
		}
51
		System.out.println("Reading complete");
52
		return textString.toString();
53
	}
54
 
55
 
56
	/**
57
	 * 
58
	 * @param filename
59
	 * @throws Exception
60
	 */
61
	public static boolean delete(String filename) throws Exception {
62
		File f = new File(filename);
63
		if(f.exists()) {
64
			return f.delete();
65
		}
66
		return true;
67
	}
68
}