Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Core Java Interview Questions and Answers

Question: What is FileOutputStream in java?
Answer:

Java.io.FileOutputStream is an output stream for writing data to a File or to a FileDescriptor. Following are the important points about FileOutputStream:

  • This class is meant for writing streams of raw bytes such as image data.

  • For writing streams of characters, use FileWriter

public class FileOutputStream extends OutputStream
Constructors:
1FileOutputStream(File file) 
This creates a file output stream to write to the file represented by the specified File object.
2FileOutputStream(File file, boolean append) 
This creates a file output stream to write to the file represented by the specified File object.
3FileOutputStream(FileDescriptor fdObj) 
This creates an output file stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.
4FileOutputStream(String name) 
This creates an output file stream to write to the file with the specified name.
5FileOutputStream(String name, boolean append) 
This creates an output file stream to write to the file with the specified name.
Example:
file = new File("c:/newfile.txt");
fop = new FileOutputStream(file);
if (!file.exists()) {
	file.createNewFile();
} 
// get the content in bytes
byte[] contentInBytes = content.getBytes(); 
fop.write(contentInBytes);
fop.flush();
fop.close();
Is it helpful? Yes No

Most helpful rated by users:

©2024 WithoutBook