0% found this document useful (0 votes)
130 views10 pages

1.26 File Input-Output in JAVA

This document discusses Java I/O and streams. It explains that Java uses streams to perform fast I/O operations and that the java.io package contains classes for input and output. It also defines three standard streams (System.out, System.in, System.err) and describes the FileOutputStream and FileInputStream classes for writing to and reading from files in Java.

Uploaded by

Ajinkya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
130 views10 pages

1.26 File Input-Output in JAVA

This document discusses Java I/O and streams. It explains that Java uses streams to perform fast I/O operations and that the java.io package contains classes for input and output. It also defines three standard streams (System.out, System.in, System.err) and describes the FileOutputStream and FileInputStream classes for writing to and reading from files in Java.

Uploaded by

Ajinkya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

JAVA I/O

• Java I/O (Input and Output) is used to process the input and produce
the output.
• Java uses the concept of a stream to make I/O operation fast. The
java.io package contains all the classes required for input and output
operations.
• We can perform file handling in Java by Java I/O API.

08-12-2022 "Programming in JAVA" 1


Stream

A stream is a sequence of data. In Java, a stream is composed of bytes. It's


called a stream because it is like a stream of water that continues to flow.

In Java, 3 streams are created for us automatically. All these streams are
attached with the console.
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream

08-12-2022 "Programming in JAVA" 2


OutputStream vs InputStream
The explanation of OutputStream and InputStream classes are given below:
OutputStream
Java application uses an output stream to write data to a destination; it may be a file, an array, peripheral device or socket.
InputStream
Java application uses an input stream to read data from a source; it may be a file, an array, peripheral device or socket.
Let's understand the working of Java OutputStream and InputStream by the figure given below.

08-12-2022 "Programming in JAVA" 3


Java FileOutputStream Class
• Java FileOutputStream is an output stream used for writing data to
a file.
• If you have to write primitive values into a file, use FileOutputStream
class. You can write byte-oriented as well as character-oriented data
through FileOutputStream class. But, for character-oriented data, it is
preferred to use FileWriter than FileOutputStream.

08-12-2022 "Programming in JAVA" 4


import java.io.FileOutputStream;
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
fout.write(65);
fout.close();
System.out.println("success...");
}catch(Exception e){System.out.println(e);}
} Output:
} Success...

The content of a text file testout.txt is set with


the data A.

testout.txt
A
08-12-2022 "Programming in JAVA" 5
import java.io.FileOutputStream;
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
String s="Welcome to javaTpoint.";
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("success...");
}catch(Exception e){System.out.println(e);}
}
} Output:
Success...
The content of a text file testout.txt is set with the
data Welcome to javaTpoint.
testout.txt
Welcome to javaTpoint.

08-12-2022 "Programming in JAVA" 6


Java FileInputStream Class

• Java FileInputStream class obtains input bytes from a file. It is used for
reading byte-oriented data (streams of raw bytes) such as image data,
audio, video etc.
• You can also read character-stream data. But, for reading streams of
characters, it is recommended to use FileReader class.

08-12-2022 "Programming in JAVA" 7


import java.io.FileInputStream;
public class DataStreamExample {
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("D:\\testout.txt");
int i=fin.read();
System.out.print((char)i);

fin.close();
}catch(Exception e){System.out.println(e);}
}
}

08-12-2022 "Programming in JAVA" 8


import java.io.FileInputStream;
public class DataStreamExample {
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("D:\\testout.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.print((char)i);
}
fin.close();
}catch(Exception e){System.out.println(e);}
}
}

08-12-2022 "Programming in JAVA" 9


Demonstration

08-12-2022 "Programming in JAVA" 10

You might also like