Friday, September 16, 2011

JAVA FileLineCount

How many lines are in a text File  

        public static int FileLineCount(String FileName)
        {
         /*
         * (c) John Minkjan
         * 101Java.blogspot.com
         * 2011 till 2031
         */
    int rCount = 0;   
            try{
            File f = new File(FileName);
            if (f.exists()){
                FileReader fr = new FileReader(f);
                LineNumberReader ln = new LineNumberReader(fr);
                int count = 0;
                while (ln.readLine() != null){
                    count++;
                }
                //System.out.println("Total line no: " + count);
                ln.close();
                                rCount = count;
            }
            else{
                System.out.println("File does not exists!");
            }
                       
                       
        }
        catch(IOException e){
            e.printStackTrace();
        }
             return rCount;  
    }

Till Next Time

JAVA FileExists

Returns True / False if file exists

public static boolean FileExists(String FileName)
{
    /*
     * (c) John Minkjan
     * 101Java.blogspot.com
     * 2011 till 2031
     */
    boolean blnFExists = false;       
    File f = new File(FileName);
    if (f.exists())
    {
        blnFExists = true;
    }
    else
    {
        blnFExists = false;  
    };
    return blnFExists;     
   
};

Till Next Time

JAVA FileRead

Read file into ArrayList

public static ArrayList<String> FileRead(String FileName)
{
     /*
     * (c) John Minkjan
     * 101Java.blogspot.com
     * 2011 till 2031
     * Usage:
     * Object[] elements = jGeneral.FileRead("c:\\java\\out.txt").toArray();
     * for(int i=0; i < elements.length ; i++)       
           System.out.println(i + " " +elements[i]);
     */
     ArrayList<String> lineArr = new ArrayList<String>();
    try {          
        FileInputStream fstream = new FileInputStream(FileName);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader( in ));
        String strLine;
        //Read File Line By Line and add to Arraylist
        while ((strLine = br.readLine()) != null) {
            lineArr.add(strLine);              
        }
        //Close the input stream
        in .close();
    } catch (Exception e) { //Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
return lineArr;
}

Till Next Time

JAVA FileAppend

Append a sting to a file:

public static void FileAppend(String FileName, String Text)
{
     /*
     * (c) John Minkjan
     * 101Java.blogspot.com
     * 2011 till 2031
     */
    try {
        // Create file if not exists else append
        FileWriter fstream = new FileWriter(FileName, true);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(Text);
        // add an extra CR;
        out.append("\r");
        //Close the output stream
        out.close();
    } catch (Exception e) { //Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
};

Till Next Time

JAVA FileWrite

Write text to file:

public static void FileWrite(String FileName, String Text)
{
    /*
     * (c) John Minkjan
     * 101Java.blogspot.com
     * 2011 till 2031
     */
    try {
        // Create a new file
        FileWriter fstream = new FileWriter(FileName);
        BufferedWriter out = new BufferedWriter(fstream);
        // Write the text
        out.write(Text);
        // add an extra CR;
        out.append("\r");
        //Close the output stream
        out.close();
    } catch (Exception e) { //Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
};

Till Next Time

JAVA ExitApp()

Exit an application:

public static void exitApp()
{
    /*
     * (c) John Minkjan
     * 101Java.blogspot.com
     * 2011 till 2031
     */
    System.exit(0);
};

Till Next Time

Welcome to Java101

An other blog in my 101 series

Till Next Time