Ishmam_Rahman

Java_HW_File Handling write read

May 6th, 2021
514
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.BufferedInputStream;
  4. import java.io.FileInputStream;
  5.  
  6. class Main {
  7.   public static void main(String[] args) {
  8.     File file = new File("Myself.java");
  9.     try {
  10.  
  11.       boolean value = file.createNewFile();
  12.       if (value) {
  13.         System.out.println("New Java File is created.");
  14.       }
  15.       else {
  16.         System.out.println("The file already exists.");
  17.       }
  18.     }
  19.     catch(Exception e) {
  20.       e.getStackTrace();
  21.     }
  22.    
  23.     try {
  24.        FileWriter output = new FileWriter("Myself.java");
  25.        output.write("Myself Khadija Rahman Riya. ID 19103108");
  26.        output.close();
  27.      }
  28.      catch (Exception e) {
  29.        e.getStackTrace();
  30.      }
  31.      
  32.      try {
  33.       FileInputStream file = new FileInputStream("Myself.txt");
  34.       BufferedInputStream input = new BufferedInputStream(file);
  35.       int i = input .read();
  36.       while (i != -1) {
  37.         System.out.print((char) i);
  38.         i = input.read();
  39.       }
  40.       input.close();
  41.     }
  42.     catch (Exception e) {
  43.       e.getStackTrace();
  44.     }
  45.   }
  46. }
RAW Paste Data