import java.io.*; public class ByteStreamDemo { static void copyFile(String from, String to) throws IOException { InputStream in = null; OutputStream out = null; try { in = new FileInputStream (from); out = new FileOutputStream(to); int c; while((c = in.read())!=-1) { out.write(c); } } finally { if(in != null) in.close(); if(out != null) out.close(); } } static boolean testByteDataIO() throws IOException { InputStream in = null; OutputStream out = null; int i = 1234567890; int j = 0; double d = 12345.6789; double e = 0.0; boolean result = false; try { out = new FileOutputStream("data1.txt"); out.write(i>>24); out.write(i>>16); out.write(i>> 8); out.write(i); long ld = Double.doubleToLongBits(d); out.write((int)(ld>>56)); out.write((int)(ld>>48)); out.write((int)(ld>>40)); out.write((int)(ld>>32)); out.write((int)(ld>>24)); out.write((int)(ld>>16)); out.write((int)(ld>> 8)); out.write((int) ld&0xff); out.flush(); in = new FileInputStream("data1.txt"); j = in.read()<<24 | in.read()<<16 | in.read()<< 8 | in.read(); ld = (long)in.read()<<56 | (long)in.read()<<48 | (long)in.read()<<40 | (long)in.read()<<32 | (long)in.read()<<24 | (long)in.read()<<16 | (long)in.read()<< 8 | (long)in.read(); e = Double.longBitsToDouble(ld); } finally { if(in != null) in.close(); if(out != null) out.close(); } if((i == j) && (d == e)) result = true; return result; } static boolean testDataIO() throws IOException { DataOutputStream out = null; DataInputStream in = null; int i = 1234567890; int j = 0; double d = 12345.6789; double e = 0.0; boolean result = false; try { out = new DataOutputStream( new FileOutputStream("data2.txt") ); out.writeInt(i); out.writeDouble(d); out.flush(); in = new DataInputStream( new FileInputStream("data2.txt") ); j = in.readInt(); e = in.readDouble(); } finally { if(in != null) in.close(); if(out != null) out.close(); } if((i == j) && (d == e)) result = true; return result; } public static void main(String[] args) throws IOException { copyFile(".\\test.txt", "test_copy.txt"); System.out.println(testByteDataIO()); System.out.println(testDataIO()); } }