Skip to main content

ObjectOutputStream — Writing Object To a File

This example illustrates using ObjectOutputStream to write an object to a file. This is a part of serialization tutorial.
class WriteObj
    public static void main(String args[]) throws Exception
        FileOutputStream fout=new FileOutputStream("obj.dat");
        ObjectOutputStream out=new ObjectOutputStream(fout);
        String st="I am written in the file.";

fout=new FileOutputStream("obj.dat"): This statement creates a FileOutputStream object pointing to obj.dat
out=new ObjectOutputStream(fout): This statement creates an ObjectOutputStream object pointing to the given FileOutputStream object.
writeObject(st): This method in the ObjectOutputStream class writes a String object to the file obj.dat


Popular posts from this blog

Setting Background Image in JFrame - Swing

Here is sample tutorial, a simple trick that enables you to set background image for JFrame.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame
JButton b1;
JLabel l1;
public BackgroundImageJFrame()
setTitle("Background Color for JFrame");
One way
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
// Another way
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am …

Add two numbers in JSP easily

Here is an example on adding up two numbers in JSP. This is very simple as you see below. This requires very small piece of code. All i have done here is created a HTML file containing the input fields which contain the numbers to add up.Now the values in the input field are of type String, so i have done type casting and converted them into int using Integer.parseInt(). See how easy it looks.

Catch The Rat: Simple Game in Java

A simple, shortest and easiest game that could be written in Java using Swing components and event handling.