mikhail_dvorkin icon

StreamExample.java

mikhail_dvorkin | PRO | 02/11/15 06:01:22 AM UTC | 0 ⭐ | 655 👁️ | Never ⏰ | []
Java |

680 B

|

None

|

0 👍

/

0 👎

import java.io.*;
import java.util.*;
 
public class StreamExample {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("file.dat");
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        for (;;) {
            int x = in.read();
            if (x == -1) {
                break;
            }
            arrayList.add(x);
        }
        System.out.println(arrayList);
        byte[] bytes = new byte[arrayList.size()];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) (int) arrayList.get(i);
        }
        System.out.println(Arrays.toString(bytes));
        in.close();
        FileOutputStream out = new FileOutputStream("file2.dat");
        out.write(bytes);
        out.close();
    }
}

Comments