jasonpogi1669

Integer object to stream (using Java) Technical Assessment 3

Mar 2nd, 2021 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class Task1{
  4.   public static void main(String[] args){
  5.    
  6.     // Write to file
  7.     try {
  8.       OutputStream output_stream_object = new FileOutputStream("Task1_test.txt");
  9.       Integer data = 809;
  10.       byte[] data_bytes = data.toString().getBytes();
  11.       output_stream_object.write(data_bytes);
  12.       System.out.println("Successfully wrote to the file!");
  13.       output_stream_object.close();
  14.     } catch (Exception e) {
  15.       e.printStackTrace();
  16.     }
  17.    
  18.     // Read from file
  19.     try {
  20.       InputStream input_stream_object = new FileInputStream("Task1_test.txt");
  21.       byte[] array_data = new byte[input_stream_object.available()];
  22.       input_stream_object.read(array_data);
  23.       String full_data = new String(array_data);
  24.       System.out.println("Successfully read from the file!");
  25.       System.out.println("Data from the file: ");
  26.       System.out.println(full_data);
  27.       input_stream_object.close();
  28.     } catch (Exception e) {
  29.       e.printStackTrace();
  30.     }
  31.    
  32.   }
  33. }
Add Comment
Please, Sign In to add comment