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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s