Advertisement
Guest User

task2

a guest
Dec 11th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. import acm.program.ConsoleProgram;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.Scanner;
  7.  
  8. public class Task2 extends ConsoleProgram {
  9.    
  10.     public void run(){
  11.         String str = fileReader();
  12.         String newFileName = readLine();
  13.         try {
  14.             fileWriter(newFileName, str);
  15.         } catch (IOException e) {
  16.             e.printStackTrace();
  17.         }
  18.     }
  19.  
  20.     public String fileReader() {
  21.         FileReader fr = null;
  22.         try {
  23.             fr = new FileReader("C:\\Users\\pc\\Desktop\\pract11.txt");
  24.         } catch (FileNotFoundException e) {
  25.  
  26.            
  27.             e.printStackTrace();
  28.         }
  29.         Scanner scan = new Scanner(fr);
  30.         String str = "";
  31.         boolean f = true;
  32.         while (scan.hasNextLine()) {
  33.             if (f) {
  34.                 f = false;
  35.                 str = scan.nextLine();
  36.             } else
  37.                 str = str + "\n" + scan.nextLine();
  38.         }
  39.  
  40.         return str;
  41.     }
  42.  
  43.     public void fileWriter(String newFileName, String str) throws IOException {
  44.  
  45.         FileWriter nFile = new FileWriter("C:\\Users\\pc\\Desktop\\"+newFileName+".bak");
  46.         nFile.write(str);
  47.         nFile.close();
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement