Wednesday, August 22, 2012

Reading file using FileReader

package com.jsl.core.io;
import java.io.BufferedReader;
import java.io.FileReader;
public class ReadingFile {
  public static void main(String[] args) {
   try{
    FileReader fr=new FileReader("info.txt");
    BufferedReader br=new BufferedReader(fr);
    String data=null;
    while((data=br.readLine())!=null){
     System.out.println(data);
    }
    br.close();
    fr.close();
   }catch(Exception e){
    e.printStackTrace();
   }
   
  }
}

Program to count the occurrence of word in the given file 


package com.jsl.core.io;

import java.io.BufferedReader;
import java.io.FileReader;

public class ReadingFile {
  public static void main(String[] args) {
   try{
    FileReader fr=new FileReader("info.txt");
    BufferedReader br=new BufferedReader(fr);
    String data=null;
    int count=0;
    while((data=br.readLine())!=null){
     String words[]=data.split(" ");
     for(String word:words){
      if(word.toLowerCase().contains("java"))
       count++;
     }
    }
    System.out.println("The count is :"+count);
    br.close();
    fr.close();
   }catch(Exception e){
    e.printStackTrace();
   }
   
  }
}

Creating and writing content into the file java 1.7 example 


package com.jsl.core.io;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class CreatingFileByWriter {

  public static void main(String[] args)  {
   String message="Welcome to core java world";
   try(FileWriter writer=new FileWriter("myfile.txt")){
     BufferedWriter bf=new BufferedWriter(writer); 
     bf.write(message);
     bf.close();
     System.out.println("DONE");
   } catch(IOException  e){
    System.out.println(e);
   }
  }
}

Serialization example


package com.jsl.core.io;

import java.io.Serializable;

public class Product implements Serializable {
  private int id;
  private String name;
  private double price;
  public Product(int id, String name, double price) {
   super();
   this.id = id;
   this.name = name;
   this.price = price;
  }
  @Override
  public String toString() {
   return id+" "+name+" "+price;
  }
}



package com.jsl.core.io;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;

public class Manager {
  public static void main(String[] args) {
   FileOutputStream fis=null;
   ObjectOutputStream ois=null;
   
   try{
    fis=new FileOutputStream("product.txt");
    ois=new ObjectOutputStream(fis);
    for(;;){
     Scanner sc=new Scanner(System.in);
     System.out.println("Enter the pid");
     int id=sc.nextInt();
     System.out.println("Enter the price");
     double price=sc.nextDouble();
     System.out.println("Enter the name ");
     String name=sc.next();
     
     Product p=new Product(id, name, price);
     ois.writeObject(p);
     
     System.out.println("Do u want to continue.... yes /no");
     String ch=sc.next();
     if(ch.equals("no"))
      break;
    }
    
   }catch(Exception e){
    e.printStackTrace();
   }finally{
    try{
     ois.close();
     fis.close();
    }catch(Exception e){
     System.out.println("While closing :"+e);
    }
    
   }
   
  }
}


Reading the object from the file:



package com.jsl.core.io;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class ReadingObjects {
  public static void main(String[] args) {
   FileInputStream fis=null;
   ObjectInputStream ois=null;
   try{
    fis=new FileInputStream("product.txt");
    ois=new ObjectInputStream(fis);
    Product p;
    while((p=(Product)ois.readObject())!=null){
     System.out.println(p);
    }
   }catch(Exception e){
    System.out.println("End of File");
   }
   
  }
}

Emplouyee.java

package com.jsl.core.io;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class Employee implements Externalizable {
 private int empno;
 private String email;
 private String password;
 public Employee() {
  // TODO Auto-generated constructor stub
 }
 @Override
 public void writeExternal(ObjectOutput out) throws IOException {
  out.writeInt(empno);
  out.writeObject(email);
 }

 @Override
 public void readExternal(ObjectInput in) throws IOException,
   ClassNotFoundException {
   this.empno=(in.readInt());
   this.email=((String)in.readObject());
  
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public int getEmpno() {
  return empno;
 }

 public void setEmpno(int empno) {
  this.empno = empno;
 }
  
}


package com.jsl.core.io;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class EmpManager {
  public static void main(String[] args) {
   FileOutputStream fis=null;
   ObjectOutputStream ois=null;
   
   try{
    fis=new FileOutputStream("employee.txt");
    ois=new ObjectOutputStream(fis);
    Employee e=new Employee();
    e.setEmpno(1001);
    e.setEmail("lakshman@jsltech.com");
    e.setPassword("xyxabc");
    
    Employee e1=new Employee();
    e1.setEmpno(1002);
    e1.setEmail("hari@jsltech.com");
    e1.setPassword("abcxyz");
    
    ois.writeObject(e1);
    ois.writeObject(e);
    
    
   }catch(Exception e){
    e.printStackTrace();
   }finally{
    try{
     ois.close();
     fis.close();
    }catch(Exception e){
     System.out.println("While closing :"+e);
    }
    
   }
   
  }
}


package com.jsl.core.io;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class ReadingObjects {
  public static void main(String[] args) {
   FileInputStream fis=null;
   ObjectInputStream ois=null;
   try{
    fis=new FileInputStream("employee.txt");
    ois=new ObjectInputStream(fis);
    Employee emp;
    while((emp=(Employee)ois.readObject())!=null){
     System.out.println(emp.getEmpno());
     System.out.println(emp.getEmail());
     System.out.println(emp.getPassword());
    }
   }catch(Exception e){
     System.out.println("End of File");
   }
   
  }
}

No comments:

Post a Comment

Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"...