FileInputStream and FileOutputStream

Source: Internet
Author: User
Tags java fileinputstream

Java FileOutputStream Class

Java FileOutputStream is an output stream for writing data to a file.

If you had to write primitive values then use Fileoutputstream.instead, for character-oriented data, prefer FILEWRITER.BU T can write byte-oriented as well as character-oriented data.

Example of Java FileOutputStream class
  1. Import java.io.*;
  2. Class test{
  3. public static void Main (String args[]) {
  4. try{
  5. FileOutputStream fout=New FileOutputStream ("Abc.txt");
  6. String s="Sachin Tendulkar is my favourite player";
  7. byte b[]=s.getbytes (); //converting string into byte array
  8. Fout.write (b);
  9. Fout.close ();
  10. System.out.println ("Success ...");
  11. }catch (Exception e) {System.out.println (e);}
  12. }
  13. }
Output:success ...

Java FileInputStream Class

Java FileInputStream class obtains input bytes from a file. It is used for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

It should is used to read byte-oriented data for example to read image, audio, video etc.

Example of FileInputStream class
  1. Import java.io.*;
  2. Class simpleread{
  3. public static void Main (String args[]) {
  4. try{
  5. FileInputStream fin=New FileInputStream ("Abc.txt");
  6. int i=0;
  7. While ((I=fin.read ())!=-1) {
  8. System.out.println ((char) i);
  9. }
  10. Fin.close ();
  11. }catch (Exception e) {System.out.println (e);}
  12. }
  13. }
Output:sachin is my favourite player.

Example of Reading The data of current Java file and writing it into another file

We can read the data of any file using the FileInputStream class whether it is Java file, image file, video file etc. In this example, we is reading the data of C.java file and writing it into another file M.java.

  1. Import java.io.*;
  2. Class c{
  3. Public static void Main (String args[])throws exception{
  4. FileInputStream fin=New FileInputStream ("C.java");
  5. FileOutputStream fout=New FileOutputStream ("M.java");
  6. int i=0;
  7. while ((I=fin.read ())!=-1) {
  8. Fout.write ((byte) i);
  9. }
  10. Fin.close ();
  11. }
  12. }

FileInputStream and FileOutputStream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.