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

Duke, the Java Mascot, in the waving pose. Duk...

Image via Wikipedia

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

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.

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

Duke, the Java Mascot, in the waving pose. Duk...

Image via Wikipedia

//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