dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Beginner
Feb 12, 1999

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Creating Files Using FileOutputStream

You accomplish basic file I/O in Java using data streams. The most basic file I/O operations in Java use the functionality offered by these classes:
 
FileInputStream
FileOutputStream
If you're new to Java, you'd probably assume that creating a file on the file system would be an operation on Java's File class. Constructing a File class does not create a file on the system. However, the mkdir() method on the File class may be used to create a directory as follows:
 
File dir = new File("mydir");
dir.mkdir();
This code will create the directory "mydir" in the current working directory. Similarly, you can delete a file or a directory by invoking the delete() operation on an instance of the File class.

However, how do you create a file? The simplest way to create a file in Java is by opening an instance of a FileOutputStream. You can accomplish this by invoking one of the following FileOutputStream constructors:

 
public FileOutputStream (String name)
public FileOutputStream (File file) 
For example, you would create a file called "myfile" in the current working directory by calling one of the following:
 
FileOutputStream fos = new  FileOutputStream("myfile");
FileOutputStream fos = new FileOutputStream(new File("myfile"));
Ajit Sagar
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date