Java program demonstrating the usage of FileOutputStream Class in java.io package

FileOutputStream creates an OutputStream that you can use to write bytes to a file.

//Java Program demonstrating the FileOutputStream Class in java.io package

FileOutputStreamDemo.java

// Demonstrate FileOutputStream.
import java.io.*;

class FileOutputStreamDemo {
	public static void main(String args[]) throws Exception {
	
		String Source = "Hello!! What are all of you doing???";
		
		byte buf[] = Source.getBytes();//Creating Byte Array
		
		OutputStream f1 = new FileOutputStream("FileWrite.txt");
		f1.write(buf);
		f1.close();
		
		OutputStream f2 = new FileOutputStream("FileWrite2.txt");
		for (int i=0; i < buf.length; i += 3) {
			f2.write(buf[i]);
		}
		f2.close();	
	}
}
		

No output is shown.  It creates a two  Files.

One called FileWrite.txt and write the text Hello!! What are all of you doing???.

FileWrite2.txt contains “Hl!h eloy i?”

Look for the Files in the Directory in which your program is there.

Advertisements

Java program demonstrating the usage of FileInputStream in java.io package-Different Way

The FileInputStream class creates an InputStream that you can use to read bytes from a file.

For this you need to create a text file called Hello.txt with some text. We will read from this file and display it in console.

//Java Program demonstrating the FileInputStream Class in java.io package

FileInputStreamDemo1.java

Check out the while loop used for looping the File

import java.io.*;

class FileInputStreamDemo1
{
	public static void main(String[] args)
	{
		try{
			File f = new File("Hello.txt");
			int size;
			FileInputStream fis = new FileInputStream(f);
			System.out.println("Total Available Bytes: " + (size = fis.available()));

			int i = 0;
			while(i!=-1)
			{
				i = fis.read();
				if(i!=-1)
				System.out.print((char)i);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

Snapshot of the o/p- Same as this program

https://programmingpalace.wordpress.com/2011/08/27/java-program-demonstrating-the-usage-of-fileinputstream-in-java-io-package/         

Java program demonstrating the usage of FileInputStream in java.io package

The FileInputStream class creates an InputStream that you can use to read bytes from a file.
Here we will be reading from a file called Hello.txt

It contains the following text

===

Hello World from Java.

Java is a high level programming language used throughout the
world.

Java is platform independent with a rich API.

Hope you will enjoy being a part of Java.

===

Create Text File Hello.txt  and keep it in the same directory as your program

//Java Program demonstrating the FileInputStream Class in java.io package

FileInputStreamDemo.java


import java.io.*;

class FileInputStreamDemo
{
	public static void main(String[] args)
	{
		try{
			File f = new File("Hello.txt");
			int size;
			FileInputStream fis = new FileInputStream(f);

			System.out.println("Total Available Bytes: " + (size = fis.available()));
			int n = size = fis.available();

			int i = 0;

			for(i=0; i<n;i++)
			{
				System.out.print((char)fis.read());
			}
		}//try
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

Snapshot of the o/p

When we run the program, the contents of the file are displayed on the console.

Java program demonstrating the usage of File Class in java.io package

//Java Program demonstrating the File Class in java.io package

FileDemo.java


import java.io.*;

class FileDemo
{
	public static void main(String[] args)
	{
		try{
			File f = new File("Hello.txt");
			
			System.out.println("File Class Methods in Java\n");
			
			System.out.println("File Exists : " +f.exists());
			System.out.println("File Name : " +f.getName());	
			System.out.println("File Parent Name : " +f.getParent());
			System.out.println("Is it File : " +f.isFile());
			System.out.println("Is it Directory : " +f.isDirectory());
			System.out.println("Is File Writable : "+f.canWrite());
			System.out.println("Is File Readable : "+f.canRead());	
			System.out.println("Does it have Absolute Path : " +f.isAbsolute());
			System.out.println("Path of File : "+f.getPath());
			System.out.println("Absolute Path of File : "+f.getAbsolutePath());
			System.out.println("File last modified : " +f.lastModified());
			System.out.println("File size : " +f.length()+ " bytes" );
			System.out.println("Is File Hidden: " +f.isHidden() );
			
			//System.out.println("Make File Readonly: " +f.setReadOnly() );
			//File f1 = new File("Hello2.txt");
			//System.out.println("Rename the File: " +f.renameTo(f1) );
			//System.out.println("Delete the File: " +f.delete() );
			
			System.out.println();
			
			 //For creating a file use createNewFile method
			/* File f2 = new File("File1.txt");
			System.out.println(f2.createNewFile());//if file already exists then file won't be created and  it will return false
			System.out.println(f2.exists());
		
			//Getting Directory Details
			File f1 = new File("Dir1");
			System.out.print("Directory Exists: ");
			System.out.println(f1.exists());
			System.out.print("Making Directory: ");
			System.out.println(f1.mkdir());
			*/
			//Proper way to create a file
			File f3 = new File("File3.txt");
			
			if(!f3.exists()){
				
				boolean b1 = f3.createNewFile();
				if(b1==true){
					System.out.println("File created successfully");
				}				
			}
			else
			{
				System.out.println("File already exists");				
			}
						
			//Proper way to create a directory
			File f4 = new File("Dir4");
			
			if(!f4.exists()){
				
				boolean b1 = f4.mkdir();
				if(b1==true){
					System.out.println("Directory created successfully");
				}				
			}
			else
			{
				System.out.println("Directory already exists");				
			}			

		}//try
		
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

Snapshot of the o/p

First o/p when the File and Directory are successfully created

2nd Time when the File and Directory already exists and are not created