package class9_1_FileChannelAndByteBuffer; import java.io.*; import java.util.Random; /* What is an advantage of having os be of type OutputStream instead of FileOutputStream? Why is OutputStream declared inside parenthesis, but not inside the try block? What is the minimum possible value that a byte can have? (It CAN have negative values) (Extra: How about int?) (Extra) What is the minimum number of bytes that will be found in the file? (Extra) What is the maximum number of bytes that will be found in the file? */ public class Writer { public static void main(String[] args) { Random generator = new Random(); OutputStream os = null; try { os = new BufferedOutputStream(new FileOutputStream("binary.bin")); int numBytes = generator.nextInt(256); os.write(numBytes); for(int i = 0; i < numBytes; i++) { os.write(generator.nextInt(256)); } } catch (FileNotFoundException e) { System.out.println("Could not find the file (probably the directory)"); } catch (IOException e) { System.out.println("Something went wrong while trying to write the file."); } finally { if(os !=null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); // TODO: Something more meaningful } } } } }