>

Monday, April 14, 2014

FileInputStream Demo in Java


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
 *
 * @author MALAR-KSP
 */
public class FileInputStreamDemo {
        public static void readFileContents(String location,String fileName)
    {
        if(location!=null&&fileName!=null)
        {
            FileInputStream fis= null;
            try {
                String file = location+File.separator+fileName;
                fis = new FileInputStream(file);
                BufferedInputStream bis= new BufferedInputStream(fis);
                int readContent = 0;
                while((readContent=bis.read())!=-1)
                {
                    System.out.print((char)readContent);
                }
                System.out.println("\nRead Completed....");
            } catch (FileNotFoundException ex) {
                System.out.println(""+ex.toString());
            } catch (IOException ex) {
                System.out.println(""+ex.toString());
            } finally {
                try {
                    fis.close();
                } catch (IOException ex) {
                    System.out.println(""+ex.toString());
                }
            }
        }
    }
    public static void main(String[] args) {
       
        String location="K:";
        String fileName = "TestFile.doc";
         
        readFileContents(location, fileName);
    }
}

FileOutputStream Example in Java


Whenever data needs to be written into file dont just write using fileoutputstream. Use BufferedOutputStream
to increase the efficiency and reduce the performance complexity.
#Whenever data is written into file using any outputstream we need to flush the stream and close the stream properly. Otherwise it will lead to memory leak and data writing gets failed.


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author Saravanan K
 */
public class FileOutputStreamDemo{
  

    public static void createAndWriteIntoFile(String fileLocation,String fileName,String content)
    {
        if(fileLocation!=null&&fileName!=null)
        {
            FileOutputStream fos = null;
            try {
                String file = fileLocation+File.separator+fileName;
                fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                bos.write(content.getBytes());
                bos.flush();
                bos.close();
            } catch (FileNotFoundException ex) {
                System.out.println(""+ex.toString());
            } catch (IOException ex) {
                System.out.println(""+ex.toString());            }
            finally {
                try {
                    fos.close();
                } catch (IOException ex) {
                    System.out.println(""+ex.toString());
                }
            }
        } else {
            System.out.println("Sorry.Filename cannot be empty...");
        }
        System.out.println("File wrote success");
    }
    public static void main(String[] args) {
      
        String location="K:";
        String fileName = "TestFile.doc";
        String content = "Hi this is FileOutputStreamDemo...";
         
        createAndWriteIntoFile(location, fileName, content);
    }
}

Wednesday, March 19, 2014

Remove Numbers in String - Java Program

public String removeNumbersInString(String str) {
        String temp = "";
        String regexp = "^[0-9]";
        Pattern pattern = Pattern.compile(regexp);
        Matcher matcher;
        if(!str.isEmpty()){
        for (int i = 0; i < str.length(); i++) {
            matcher = pattern.matcher(str.charAt(i) + "");
            if (!matcher.matches()) {
                temp = temp + str.charAt(i) + "";
            }
        }}
        System.out.println("" + temp);
        return temp;
    }

Remove String in Numbers-Java Program

public String removeStringInNumbers(String str) {
        String temp = "";
        String regexp = "^[a-zA-Z]";
        Pattern pattern = Pattern.compile(regexp);
        Matcher matcher;
        if(!str.isEmpty()){
        for (int i = 0; i < str.length(); i++) {
            matcher = pattern.matcher(str.charAt(i) + "");
            if (!matcher.matches()) {
                temp = temp + str.charAt(i) + "";
            }
        }}
        System.out.println("" + temp);
        return temp;
    }

Saturday, March 1, 2014

Write an example that counts the number of times a particular character, such as e, appears in a file. The character can be specified at the command line.



import java.io.BufferedInputStream;
import java.io.FileInputStream;

class GetOccurrences{
    public static void main(String args[])
    {
        try{
            FileInputStream fis=new FileInputStream("C:\\Users\\SAN-MCA-ME-MTECH-MS\\Documents\\SAN\\Temp.txt");
            BufferedInputStream bis=new BufferedInputStream(fis);
                   
            int i;
            int occurs=0;
           
            while((i=bis.read())!=-1)
            {
                char a=(char)i;
               
                if(a==args[0].charAt(0))
                {
                 occurs++;
                }
            }
            System.out.println("Letter e  is "+occurs+" of times repeated in the file");
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }
}

Saturday, February 23, 2013

Java Connect to Oracle XE Database

 

In order to connect to oracle  database you need to add ojdbc14.jar file in your eclipse project as a external jar file.... Download from web.


import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class OracleConnect {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException{
        // TODO Auto-generated method stub
        try
        {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","saravanan","saravanan");
        System.out.println("Connected Successfully To Oracle");
        Statement stmt;
        ResultSet rs;
        stmt=con.createStatement();
        rs=stmt.executeQuery("select * from saran");
       
       while(rs.next())
       {
            System.out.println(rs.getString("empname")+"--"+rs.getInt("salary"));
       }
       
       
        }
        catch(Exception ex)
        {
        ex.printStackTrace();
        }

    }

}