Tuesday, June 11, 2013

How to read, write, delete, update file content using Java

How to read, write, delete, update file content using Java


package com.fm.helloworld.customer.model.fileutil;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;

public class FileUtil {
    public FileUtil() {
        super();
      int a=34;
      Integer a1 = new Integer("34");
      System.out.println(a1);
    }

    /**
     * This method reads the contents of the given file
     * and returns the String
     * @param fileName
     * @return
     */
    public String readFileContent(String fileName) {
        StringBuilder sb = new StringBuilder();
        // Get the reference of given File
        File file = new File(fileName);
        int ch;
        // FileInputStream variable
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(file);
            // Iterate through every character of given File
            while ((ch = fin.read()) != -1)
                sb.append((char)ch);
            fin.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        return sb.toString();
    }

    /**
     * This method creates a new File
     * @param fileName
     * @param fileContent
     * @return
     */
    public boolean createFile(String fileName, String fileContent) {
        boolean isSuccess = true;
        try {
            File f1 = new File(fileName);
            BufferedWriter bw = new BufferedWriter(new FileWriter(f1));
            bw.write(fileContent);
            bw.close();
        } catch (Exception e) {
            System.out.println(e);
            isSuccess = false;
        }
        return isSuccess;
    }

    /**
     * This method reads the contents of the given file using Buffer Reader
     * and returns the String
     * @param fileName
     * @return
     */
    public String readFileContentUsingBufferReader(String fileName) {
        StringBuilder sb = new StringBuilder();
        // Get the reference of given File
        File file = new File(fileName);
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            while (true) {
                String str = br.readLine();
                if (str == null) {
                    break;
                } else {
                    sb.append(str);
                }
            }
            br.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        return sb.toString();
    }
    /**
     * This method updated the given file with given content.
     * If everything goes fine, return true otherwise return false
     * @param fileName
     * @param fileContent
     * @return
     */
    public boolean updateFile(String fileName, String fileContent) {
        boolean isSuccess = true;
        StringBuilder sb = new StringBuilder();
        try {
            File f1 = new File(fileName);
            if (f1.canWrite()) {
                String existFileContent =
                    readFileContentUsingBufferReader(fileName);
                sb.append(existFileContent);
                BufferedWriter bw = new BufferedWriter(new FileWriter(f1));
                sb.append(fileContent);
                bw.write(sb.toString());
                bw.close();
            }

        } catch (Exception e) {
            System.out.println(e);
            isSuccess = false;
        }
        return isSuccess;
    }

    /**
     * This file copies data from srcFile to tgtFile
     * @param srcFile
     * @param tgtFile
     * @return
     */
    public boolean copyFile(String srcFile, String tgtFile) {
        boolean isSuccess = true;
        try {
            String existFileContent =
                readFileContentUsingBufferReader(srcFile);
            createFile(tgtFile, existFileContent);

        } catch (Exception e) {
            System.out.println(e);
            isSuccess = false;
        }
        return isSuccess;
    }

    /**
     *
     * @param fileName
     * @return
     */
    public boolean updateFileStatus(String fileName,boolean isReadOnly) {
        boolean isSuccess = true;
        StringBuilder sb = new StringBuilder();
        try {
            File file = new File(fileName);

            if(isReadOnly){
                file.setReadOnly();
            }else{
                file.setWritable(true);
            }
           
            boolean readOnly = file.canWrite();
            System.out.println("File Status : " + readOnly);

        } catch (Exception e) {
            System.out.println(e);
            isSuccess = false;
        }
        return isSuccess;
    }
    /**
     *
     * @param dirName
     * @return
     */
    public boolean createDirectory(String dirName){
        boolean isSuccess = true;
       
        try {
            File file = new File(dirName);
            file.mkdirs();

        } catch (Exception e) {
            System.out.println(e);
            isSuccess = false;
        }
        return isSuccess;
    }
    public static void main(String[] args) {
        FileUtil util = new FileUtil();
        //String fileContent=util.readFileContent("C:/RKP/emp.txt");
        //System.out.println(fileContent);
        String fileName = "C:/RKP/fmtest.txt";
        String tgtFileName = "C:/RKP/fmtest_copy.txt";
        String fileContent = null;
        boolean isSuccess = false;
        fileContent = " || Fortune Minds Inc - April 23";
        String dirName="C:/tmp/tmp1";
       
        //isSuccess= util.createDirectory(dirName);
         //isSuccess = util.createFile(fileName, fileContent);
        //fileContent = util.readFileContentUsingBufferReader(fileName);
        //isSuccess = util.updateFileStatus(fileName,true);
        //isSuccess = util.updateFile(fileName, fileContent);
        //isSuccess = util.updateFileStatus(fileName, false);
        //isSuccess = util.updateFile(fileName, fileContent);
        isSuccess=util.copyFile(fileName, tgtFileName);
        System.out.println(isSuccess);
        System.out.println(fileContent);
    }
}

No comments:

Post a Comment