EgzonIseini

[OS] Lab 01 - Problem 3

Mar 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. import java.io.*;
  2. import java.nio.charset.StandardCharsets;
  3.  
  4. public class HW01_3 {
  5.     public static void main(String[] args) {
  6.         try {
  7.             //Read text from command line and make a new text file with that input.
  8.             File in = new File("source.txt"); //File will be named 'source.txt'
  9.             String text;
  10.             //If file manually created with text, skips the next block. If it was never created then :
  11.             if(!in.exists()) { // - we create it. In the file we write whatever we read from command line.
  12.                 BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  13.                 text = bf.readLine();
  14.  
  15.                 PrintWriter pw = new PrintWriter("source.txt");
  16.                 pw.println(text);
  17.                 pw.close();
  18.             } // Now we have a file created it it already didn't exist.
  19.  
  20.  
  21.             //Actual code required by lab.
  22.             //We don't declare the input file again as its delcared above as 'in' - we just use it from now on.
  23.             File out = new File("destination.txt"); //Destination file where we print. :)
  24.             BufferedReader br = new BufferedReader(new FileReader(in));
  25.             BufferedWriter bw = new BufferedWriter(new FileWriter(out));
  26.  
  27.             String reversed = new StringBuilder(br.readLine()).reverse().toString();
  28.             bw.write(reversed);
  29.  
  30.             br.close();
  31.             bw.close();
  32.         }
  33.         catch (IOException e) {
  34.             e.printStackTrace();
  35.         }
  36.         System.exit(0);
  37.     }
  38. }
Add Comment
Please, Sign In to add comment