Monday, January 12, 2015

Read input from java console in java file URI reference in Java Pro



How to read input from java console in java

package com.java2novice.files;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
public class ReadFromConsole
{
public static void main(String a[])
{
        BufferedReader br = null;
        Reader r = new InputStreamReader(System.in);
        br = new BufferedReader(r);
        String str = null;
        try
      {
            do
        {
                System.out.println("Enter Input, exit to quit.");
                str = br.readLine();
                System.out.println(str);
            }
            while (!str.equalsIgnoreCase("exit"));
            }
            catch (IOException e)
           {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(br != null) br.close();
            }
            catch(Exception ex){}
        }
    }
}

How to get file list from a folder filtered by extensions

package com.java2novice.files;
import java.io.File;
import java.io.FilenameFilter;
public class MyFilteredFileList
{
public static void main(String a[])
{
        File file = new File("C:/MyFolder/");
        File[] files = file.listFiles(new FilenameFilter()
{
 public boolean accept(File dir, String name)
{
                if(name.toLowerCase().endsWith(".csv"))
               {
                    return true;
                }
                  else
              {
                    return false;
                }
            }
        });
        for(File f:files)
        {
            System.out.println(f.getName());
        }
    }
}

How to get file URI reference

package com.java2novice.files;
import java.io.File;
public class MyFileUrl
{
public static void main(String a[])
{
        File f = new File("C:/TestForm.txt");
        System.out.println(f.toURI());
    }
}

How to store and read objects from a file

package com.java2novice.files;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
public class MyObjectFileStore
{
public void storeObject(Employee emp)
{
        OutputStream ops = null;
        ObjectOutputStream objOps = null;
        try
        {
            ops = new FileOutputStream("MyEmpFile.txt");
            objOps = new ObjectOutputStream(ops);
            objOps.writeObject(emp);
            objOps.flush();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
         }
         catch (IOException e)
         {
            e.printStackTrace();
         }
         finally
         {
            try
            {
                if(objOps != null) objOps.close();
            }
            catch (Exception ex){}
        }
      }
    public void displayObjects()
    {
        InputStream fileIs = null;
        ObjectInputStream objIs = null;
        try
        {
            fileIs = new FileInputStream("MyEmpFile.txt");
            objIs = new ObjectInputStream(fileIs);
            Employee emp = (Employee) objIs.readObject();
            System.out.println(emp);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
           {
                if(objIs != null) objIs.close();
            }
            catch (Exception ex){}
              }
         }
    public static void main(String a[])
   {
        MyObjectFileStore mof = new MyObjectFileStore();
        Employee e1 = new Employee("Tony",1,"1000");
        mof.storeObject(e1);
        mof.displayObjects();
    }
}
class Employee implements Serializable
{
    private String name;
    private int id;
    private String salary;
 public Employee(String name, int id, String salary)
{
        this.name = name;
        this.id = id;
        this.salary = salary;
    }
public String toString()
{
        return name +"=="+id+"=="+salary;
 }
public String getName()
{
        return name;
 }
public void setName(String name)
{
        this.name = name;
 }
 public int getId()
{
        return id;
 }
 public void setId(int id)
{
        this.id = id;
}
public String getSalary()
{
        return salary;
 }
 public void setSalary(String salary)
 {
        this.salary = salary;
    }
}

No comments:

Post a Comment