Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.nio.charset.StandardCharsets;
- public class HW01_3 {
- public static void main(String[] args) {
- try {
- //Read text from command line and make a new text file with that input.
- File in = new File("source.txt"); //File will be named 'source.txt'
- String text;
- //If file manually created with text, skips the next block. If it was never created then :
- if(!in.exists()) { // - we create it. In the file we write whatever we read from command line.
- BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
- text = bf.readLine();
- PrintWriter pw = new PrintWriter("source.txt");
- pw.println(text);
- pw.close();
- } // Now we have a file created it it already didn't exist.
- //Actual code required by lab.
- //We don't declare the input file again as its delcared above as 'in' - we just use it from now on.
- File out = new File("destination.txt"); //Destination file where we print. :)
- BufferedReader br = new BufferedReader(new FileReader(in));
- BufferedWriter bw = new BufferedWriter(new FileWriter(out));
- String reversed = new StringBuilder(br.readLine()).reverse().toString();
- bw.write(reversed);
- br.close();
- bw.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- System.exit(0);
- }
- }
Add Comment
Please, Sign In to add comment