Tuesday, 29 August 2017

java Program to Read and Write a file in Java using Byte Streams

Read and Write a file in Java using Byte Streams (Byte Streams)

Program

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteStreamDemo {

 public static void main(String args[]) throws IOException {
  FileInputStream fileInputStream = null;
  FileOutputStream fileOutputStream = null;

  try {
   int data;
   fileInputStream = new FileInputStream("photo.jpg");
   fileOutputStream = new FileOutputStream("output.jpg");
   while ((data = fileInputStream.read()) != -1) {
    System.out.print(" " + data);
    fileOutputStream.write(data);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } finally {
   if (fileInputStream != null)
    fileInputStream.close();
   if (fileOutputStream != null)
    fileOutputStream.close();
  }
 }

}

Output

Byte Streams  core java Byte Streams  core java

0 comments:

Post a Comment