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:

 FileInputStreamFileOutputStream

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"));
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS