Knowee
Questions
Features
Study Tools

Write a short note on FilelnputStream and FileOutputStream class.

Question

Write a short note on FilelnputStream and FileOutputStream class.

🧐 Not the exact question you are looking for?Go ask a question

Solution

FileInputStream and FileOutputStream are two classes provided by Java for reading data from and writing data to a file, respectively. They are part of Java's I/O streams framework which is used to handle input and output operations.

  1. FileInputStream: This is an input stream used for reading data from a file. It is used to read byte-oriented data (streams of raw bytes such as image data, etc.). If the file does not exist, a FileNotFoundException is thrown. Here is a simple usage example:
FileInputStream fis = new FileInputStream("input.txt");
int i = fis.read();
while(i != -1) {
    System.out.print((char)i);
    i = fis.read();
}
fis.close();

In this example, we're creating a FileInputStream on the file "input.txt", reading one byte at a time from the file, and then printing out the data as characters. The read() method returns -1 when there is no more data to read.

  1. FileOutputStream: This is an output stream used for writing data to a file. Like FileInputStream, it is used for writing byte-oriented data. If the file does not exist, one will be created. Here is a simple usage example:
FileOutputStream fos = new FileOutputStream("output.txt");
String data = "Hello, world!";
fos.write(data.getBytes());
fos.close();

In this example, we're creating a FileOutputStream on the file "output.txt", converting a string to bytes using the getBytes() method, and then writing those bytes to the file.

Both FileInputStream and FileOutputStream should always be closed after use to free up system resources. This can be done using the close() method, or by using a try-with-resources statement which automatically closes the streams.

This problem has been solved

Similar Questions

Develop a Java program to copy the contents of one file to another fileusing FileInputStream and FileOutputStream classes.

Which stream class is used to write formatted text to a file in Java?Question 13Answera.PrintWriterb.FileWriterc.FileOutputStreamd.BufferedWriter

Which class in Java is used to read data line by line from a file?OptionsFileInputStreamBufferedReaderFileWriterOutputStream

t the correct answerWhich stream does Java application uses to read data from a source, it may be a file, an array, peripheral device or socket?OptionsInputStreamInput/OutputStreamOutputStreamNone of these

orrect answerWhat is the output of this program? (Assume 'inputoutput.java' file exists in the current directory)import java.io.*;class filesinputoutput { public static void main(String args[]) throws FileNotFoundException, IOException { InputStream obj = new FileInputStream("inputoutput.java"); System.out.print(obj.available()); obj.close(); }}

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.