Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import acm.program.ConsoleProgram;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class Task2 extends ConsoleProgram {
- public void run(){
- String str = fileReader();
- String newFileName = readLine();
- try {
- fileWriter(newFileName, str);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public String fileReader() {
- FileReader fr = null;
- try {
- fr = new FileReader("C:\\Users\\pc\\Desktop\\pract11.txt");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- Scanner scan = new Scanner(fr);
- String str = "";
- boolean f = true;
- while (scan.hasNextLine()) {
- if (f) {
- f = false;
- str = scan.nextLine();
- } else
- str = str + "\n" + scan.nextLine();
- }
- return str;
- }
- public void fileWriter(String newFileName, String str) throws IOException {
- FileWriter nFile = new FileWriter("C:\\Users\\pc\\Desktop\\"+newFileName+".bak");
- nFile.write(str);
- nFile.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement