Showing posts with label deserialization. Show all posts
Showing posts with label deserialization. Show all posts

Monday, April 30, 2018

Read properties from an xml file !!

Using Properties class:
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.util.Enumeration;
  4. import java.util.Properties;

  5. public class ReadPropertiesXmlFile {
  6. public static void main(String[] args) throws Exception {
  7. File file = new File("D:\\prop.xml");
  8. FileInputStream fileInput = new FileInputStream(file);
  9. Properties properties = new Properties();
  10. properties.loadFromXML(fileInput);
  11. fileInput.close();

  12. Enumeration enuKeys = properties.keys();
  13. while (enuKeys.hasMoreElements()) {
  14. String key = (String) enuKeys.nextElement();
  15. String value = properties.getProperty(key);
  16. System.out.println(key + ": " + value);
  17. }
  18. }
  19. }
Output:
favoriteSeason: spring
favoriteDay: today
favoriteFruit: apple

prop.xml - file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Here are some favorites</comment>
<entry key="favoriteSeason">spring</entry>
<entry key="favoriteFruit">apple</entry>
<entry key="favoriteDay">today</entry>

</properties>

Read the data from properties file !!

Using Properties class:
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.util.Enumeration;
  5. import java.util.Properties;

  6. public class ReadPropertiesFiles {
  7. public static void main(String[] args) throws IOException {

  8. File file = new File("D:\\contacts.properties");
  9. FileInputStream fis = new FileInputStream(file);
  10. Properties prop = new Properties();
  11. prop.load(fis);
  12. fis.close();

  13. Enumeration enuKeys = prop.keys();
  14. while (enuKeys.hasMoreElements()) {
  15. String key = (String) enuKeys.nextElement();
  16. String value = prop.getProperty(key);
  17. System.out.println(key + " = " + value);
  18. }
  19. }
  20. }
Output:
delhi = sumit
japan = chet
bangalore = raju
chennai = ishaan

Read file using BufferedReader !!

Using BufferedReader:
  1. import java.io.File;
  2. import java.io.FileReader;
  3. import java.io.BufferedReader;

  4. public class ReadFileByBufferedReader {

  5. public static void main(String[] args) throws Exception {
  6. File file = new File("D://data.txt");
  7. FileReader fr = new FileReader(file);
  8. BufferedReader br = new BufferedReader(fr);
  9. StringBuffer sb = new StringBuffer();

  10. String line = br.readLine(); // read the line
  11. while (null != line) {
  12. sb.append(line); // append the line
  13. sb.append("\n");
  14. line = br.readLine(); // read the line
  15. }
  16. System.out.println(sb.toString());
  17. fr.close();
  18. }
  19. }
Output:
Hi guys !!
This is Ishaan from India.
and I'm reading the data from a text file line by line using BufferedReader.
Isn't it interesting !! to read the data from a file line by line 
irrespective of amount of data that a file is having.

Read data from a file using BufferedReader !!

Using BufferedReader:
  1. import java.io.File;
  2. import java.io.FileReader;
  3. import java.io.BufferedReader;

  4. public class ReadFileByBufferedReader {

  5. public static void main(String[] args) {
  6. try {
  7. File file = new File("D://data.txt");
  8. FileReader fr = new FileReader(file);
  9. BufferedReader br = new BufferedReader(fr);
  10. String line = br.readLine(); // read the line
  11. while (null != line) {
  12. System.out.println(line); // print the line
  13. line = br.readLine(); // read the next line
  14. }
  15. fr.close();
  16. } catch (Exception e) {
  17. System.out.println("excepton occured in " + e);
  18. }
  19. }
  20. }
Note: extension support .java/.txt/.xml/.yml/.html/.css etc file

Output:
Hi guys !!
This is Ishaan from India.
and I'm reading the data from a text file line by line using BufferedReader.
Isn't it interesting !! to read the data from a file line by line
irrespective of amount of data that a file is having.

II Approach:

  1. import java.io.File;
  2. import java.io.FileReader;
  3. import java.io.BufferedReader;

  4. public class ReadFileByBufferedReader {

  5. public static void main(String[] args) {
  6. File file = null;
  7. FileReader fr = null;
  8. BufferedReader br = null;
  9. try {
  10. file = new File("D://data.txt");
  11. fr = new FileReader(file);
  12. br = new BufferedReader(fr);
  13. String line = br.readLine(); // read the next line

  14. while (line != null) {
  15. System.out.println(line); // print the line
  16. line = br.readLine(); // read the next line
  17. }
  18. } catch (Exception e) {
  19. System.out.println("excepton occured in " + e);
  20. } finally {
  21. try {
  22. if (fr != null) {
  23. fr.close();
  24. }
  25. if (br != null) {
  26. br.close();
  27. }
  28. } catch (Exception e) {
  29. System.out.println("excepton occured in finally block " + e);
  30. }
  31. }
  32. }
  33. // support .java/.txt/.xml/.yml/.html/.css etc file
  34. }

Sunday, April 29, 2018

Read data from a file using FileReader class !!

Read file using FileReader:
  1. import java.io.File;
  2. import java.io.FileReader;

  3. public class ReadFileByFileReader {
  4. public static void main(String[] args) {
  5. try {
  6. File file = new File("D:\\data.txt");
  7. FileReader fr = new FileReader(file);

  8. int length = (int) file.length();
  9. char c[] = new char[length];

  10. fr.read(c);
  11. String data = new String(c);

  12. System.out.println(data);
  13. fr.close();
  14. } catch (Exception e) {
  15. System.out.println("excepton occured in " + e);
  16. }
  17. }
  18. }
Output:
Hi guys !!
This is Ishaan from India.
and I'm reading the data from a text file line by line using BufferedReader.
Isn't it interesting !! to read the data from a file line by line 
irrespective of amount of data that a file is having.


Read / Write data in a file for transient field using ObjectOutputStream

Using ObjectOutputStream class & transient keyword:
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.ObjectOutputStream;
  4. import java.io.Serializable;

  5. class Employee implements Serializable {

  6. private static final long serialVersionUID = 1L;
  7. int id;
  8. String name;
  9. // double salary;
  10. transient double salary;

  11. public Employee(int id, String name, double salary) {
  12. this.id = id;
  13. this.name = name;
  14. this.salary = salary;
  15. }

  16. @Override
  17. public String toString() {
  18. return "[id: " + id + ", name: " + name + ", salary: " + salary + "]";
  19. }
  20. }

  21. public class WriteByObjectOutputStreamExe {

  22. public static void main(String[] args) throws Exception {
  23. File file = new File("D:\\data1.txt");
  24. FileOutputStream fileStream = new FileOutputStream(file);
  25. ObjectOutputStream objStream = new ObjectOutputStream(fileStream);

  26. Employee e1 = new Employee(101, "suresh", 2000);

  27. objStream.writeObject(e1);
  28. objStream.flush();
  29. objStream.close();
  30. }
  31. }
Output:
ͭ sr  com.ishaan.filehandling.Employee        I idL namet Ljava/lang/String;xp   et suresh

Read transient field data from a file using ObjectInputStream:
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.ObjectInputStream;

  4. public class ReadFileByObjectInputStreamExe {

  5. public static void main(String[] args) throws Exception {
  6. File file = new File("D:\\data1.txt");
  7. FileInputStream fileStream = new FileInputStream(file);
  8. ObjectInputStream objStream = new ObjectInputStream(fileStream);

  9. Employee emp = (Employee) objStream.readObject();

  10. System.out.println(emp.toString());
  11. objStream.close();
  12. }
  13. }
Output: 
[id: 101, name: suresh, salary: 0.0]

Read file using ObjectInputStream !!

Using ObjectInputStream:
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.ObjectInputStream;

  4. public class ReadFileByObjectInputStreamExe {

  5. public static void main(String[] args) throws Exception {
  6. File file = new File("D:\\data1.txt");
  7. FileInputStream fileStream = new FileInputStream(file);
  8. ObjectInputStream objStream = new ObjectInputStream(fileStream);

  9. Employee emp = (Employee) objStream.readObject();

  10. System.out.println(emp.toString());
  11. objStream.close();
  12. }
  13. }
Employee.java:
  1. import java.io.Serializable;
  2. class Employee implements Serializable {

  3. private static final long serialVersionUID = 1L;
  4. int id;
  5. String name;
  6. double salary;
  7. // transient double salary;

  8. public Employee(int id, String name, double salary) {
  9. this.id = id;
  10. this.name = name;
  11. this.salary = salary;
  12. }

  13. @Override
  14. public String toString() {
  15. return "[id: " + id + ", name: " + name + ", salary: " + salary + "]";
  16. }
  17. }
Output: 
[id: 101, name: suresh, salary: 2000.0]

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...