Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to store data with Java

In this article I will teach you how you may store data in java. We discuss 3 ways which applies according to various situations.

Hello guys, today we will learn some ways to store data with java, we will talk about these following ways :

  1. Storing through vector
  2. Storing with file
  3. Storing with database

First we will discuss how we may store data using Vectors

Listing 1: Storing data with Vectors


import java.util.Vector;

public class Storage 
{
	Vector a=new Vector();
	Person p=new Person();
	public static void main(String args[])
	{
		Storage s=new Storage();
		for(int i=0;i<5;i++)
			s.start("ID"+i);
		s.show();
	}
	public void start(String id)
	{
		p.setId(id);
		p.setName("Anurag");
		a.add(p);
	}
	public void show()
	{
		for(int i=0;i<a.size();i++)
		{
			Person per=(Person)a.get(i);
			System.out.println("ID:"+per.getId()+" and Name:"+per.getName());
		}
	}
}

class Person
{
	String name;
	String id;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
}

Here:

  1. java.util.Vector is used to implement the vector class
  2. Now we define a class and within it we define vector and also an object of another class called Person
  3. Now the Person object is made to store basic info like name and id. And we use vector to store this information in easy manner
  4. We define a loop and call the start method 5 times passing a new id to start method each time as an argument
  5. In start method we are setting information on person object and storing this information in vector a using the add method of vector
  6. Since the loop run 5 time so the vector will record the 5 records and save them which you may use for future purpose
  7. We can use the show method to show all the data inside the vector
  8. In show method we run a loop from 0 to vector size so that each element in vector gets processed. In next step we display the information
  9. After that we defined a class called Person which will define the variable name and id within it. We have defined setters and getters for these.

Now we move forward to 2nd method which is saving to file

Listing 2: Storing data with files

import java.io.BufferedWriter;
import java.io.FileWriter;

public class Storage2 
{
	Person2 p=new Person2();
	public static void main(String args[])
	{
		Storage2 s=new Storage2();
		for(int i=0;i<5;i++)
			s.start("ID"+i);
		
		System.out.print("Done");
	}
	public void start(String id)
	{
		p.setId(id);
		p.setName("Anurag");
		try
		{
		FileWriter fw=new FileWriter("out.txt",true);
		BufferedWriter bw=new BufferedWriter(fw);
		bw.write(p.msg());
		bw.newLine();
		bw.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("error"+e.getMessage());
		}
	}
}
class Person2
{
	String name;
	String id;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String msg()
	{
		return this.id+"  "+this.name;
	}
}

Here:

  1. We include FileWriter and BufferedWriter to include functionality to write to file
  2. First we make a class named Storage2
  3. We make a object of class person2 which contain info about person like id and name
  4. We define the main method and define the object of Storage2 class.
  5. Now we call the start method of this class which will write data to file.This will be done 5 times
  6. Now we define the start object and then we make objects for File Writer and BufferedWriter.Now they are used to write the name and id using the msg function of Person2
  7. After writing to file we close the stream.Remember in FileWriter we gave 2 argument which are file path and the other argument that is true tell that we want to append in the file
  8. We capture all exception which may occur
  9. We define the Person2 class which define name and id.We also define setter and getter for the same.We also define a msg function which will return the id and name for current object

The third way of storage is Database. We have already discussed this in our previous posts about How to connect with MySQL database using Java. You may use this as reference.

Remember that you use file when you have some linear data where you don’t involve search operations since here database would be much more effective.

This is all for this article. See you next time with some more exciting article



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login