Java File Handling Example : Java File Handling Sample Code

Java File Handling Example : Java File Handling Sample Code

Java File Handling Examplejava file handling example contains complete code to understand how java programming can be used to write into file. Java file handling is part of Java Application.

If you are not sure about Java Application  Vs Java Applet

 

import java.io.*;
import java.util.*;

public class File1 
{
	public static void main(String[] ar) throws IOException
	{
		File f1 = new File("d:\\odd1.txt");
		File f2 = new File("d:\\even1.txt");

		if ( !(f1.exists()))
		{
			f1.createNewFile();
		}
		if ( !(f2.exists()))
		{
			f2.createNewFile();
		}

		FileWriter w1 = new FileWriter(f1);		//odd
		FileWriter w2 = new FileWriter(f2);		//even

		for(int i=1;i<50;i++)
		{
			if( (i%2)==1)
			{
				w1.write(""+i);
				w1.flush();
			}
			else
			{
				w2.write(""+i);
				w2.flush();
			}
		}
		w1.close();
		w2.close();
	}
}

 

If you are going for interview in few days, Must check out Java Interview Question

 

Leave a Reply