mikhail_dvorkin

StreamExample.java

Feb 11th, 2015
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class StreamExample {
  5.     public static void main(String[] args) throws IOException {
  6.         FileInputStream in = new FileInputStream("file.dat");
  7.         ArrayList<Integer> arrayList = new ArrayList<Integer>();
  8.         for (;;) {
  9.             int x = in.read();
  10.             if (x == -1) {
  11.                 break;
  12.             }
  13.             arrayList.add(x);
  14.         }
  15.         System.out.println(arrayList);
  16.         byte[] bytes = new byte[arrayList.size()];
  17.         for (int i = 0; i < bytes.length; i++) {
  18.             bytes[i] = (byte) (int) arrayList.get(i);
  19.         }
  20.         System.out.println(Arrays.toString(bytes));
  21.         in.close();
  22.         FileOutputStream out = new FileOutputStream("file2.dat");
  23.         out.write(bytes);
  24.         out.close();
  25.     }
  26. }
Add Comment
Please, Sign In to add comment