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.
FileOutputStream class declaration
Let's see the declaration for Java.io.FileOutputStream class:
- public class FileOutputStream extends OutputStream
Java FileOutputStream Example 1: write byte
- 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:
The content of a text file testout.txt is set with the data A.
testout.txt
Java FileOutputStream example 2: write string
- 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();
- fout.write(b);
- fout.close();
- System.out.println("success...");
- }catch(Exception e){System.out.println(e);}
- }
- }
Output:
Sign up here with your email
ConversionConversion EmoticonEmoticon