Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.social.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Utility functions 
 * 
 * @author rajveer
 *
 */
public class FileUtils {
        
        public static String read(String filename) throws IOException  {
                
                File f = new File(filename);
                if(!f.exists()) {
                        System.out.println(filename + " - does not exist");
                        throw new FileNotFoundException();
                }
                
                StringBuilder textString = new StringBuilder();
                FileInputStream fis = null;
                try {
                        fis = new FileInputStream(f);
                        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                        String line;
                        while((line = br.readLine()) != null){
                                textString.append(line+"\n");
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                        throw e;
                }
                finally {
                        if(fis != null) {
                                fis.close();
                        }
                }
                System.out.println("Reading complete");
                return textString.toString();
        }       
        
        /**
         * @param filename
         * @throws Exception
         */
        public static boolean delete(String filename) throws Exception {
                File f = new File(filename);
                if(f.exists()) {
                        return f.delete();
                }
                return true;
        }
}