Dan Nordin
Dan Nordin

Reputation: 3

Using a text file to copy to a new text file

I am working on a project to read, write, format, and change the name of a predetermined file on the desktop. Everything is working fine except for the namechange method. When i type change name and give it the file it makes the new file, however, it fills the new text file with the location of the old text file.

Update! I have found the problem but i still don't have an answer. It seems that whatever information is put into the Scanner inputfile = new Scanner("here"); the here will be printed out not the contents of the file. So when the next step comes where it reads the file that its supposed to be scanning it instead takes the here part of the scanner and prints that to the file variable instead which then gets printed to the output and subsequently the file.

package file.io;

import java.io.*;
import java.util.Scanner;
import java.lang.StringBuilder;

public class FileIo {


public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    String answer;
    int x = 0;
    String loop;

    while (x < 1) {
        int y = 0;
        System.out.println("what would you like to do read, write, format, "
                + "or change the name?");
        answer = keyboard.nextLine();
        if (answer.equalsIgnoreCase("Read")) {
            read();
        } else if (answer.equalsIgnoreCase("write")) {
            write();
        } else if (answer.equalsIgnoreCase("format")) {
            format();
        } else if (answer.equalsIgnoreCase("change the name")) {
            namechange();
        } else {
            System.out.println("you entered an invalid function.");
        }
        while (y < 1) {
            System.out.println("would you like to do another opperation?");
            loop = keyboard.nextLine();

            if (loop.equalsIgnoreCase("no")) {
                x++;
                y++;
                System.out.println("goodbye!");
            } else if (loop.equalsIgnoreCase("yes")) {
                System.out.println("\n");
                y++;
            } else {
                System.out.println("the possible answers are yes or no, try again.");
            }
        }
    }

}

public static void read() {

    try {
        String file = "C:\\Users\\danor\\Desktop\\example.txt";
        File fileHandle = new File(file);
        try (Scanner inputfile = new Scanner(fileHandle)) {
            String line;
            if (inputfile.hasNextLine()) {
                while (inputfile.hasNextLine()) {
                    line = inputfile.nextLine();
                    System.out.println(line);
                }
                System.out.println("\n");
            } else {
                System.out.println("the file contains nothing.");
            }
        }
    } catch (Exception e) {
        System.out.println("something went wrong");
    }

}

public static void write() {
    Scanner keyboard = new Scanner(System.in);
    String print;
    try {
        String file = "C:\\Users\\danor\\Desktop\\example.txt";
        FileWriter fwriter = new FileWriter(file, true);
        PrintWriter out = new PrintWriter(fwriter);
        System.out.println("what would you like to print?");
        print = keyboard.nextLine();
        out.println(print);
        out.close();
    } catch (Exception e) {
        System.out.println("something went wrong");

    }
}

public static void format() {
    try {
        String file = "C:\\Users\\danor\\Desktop\\example.txt";
        PrintWriter writer = new PrintWriter(file);
        writer.print("");
        writer.close();
        System.out.println("file formatted.");
    } catch (Exception e) {
        System.out.println("something went wrong");
    }
}

public static void namechange() {
    try {
        String fileorig = "C:\\Users\\danor\\Desktop\\example.txt";
        String asf;
        String line;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("what do you want the new file to be named.");
        StringBuilder file = new StringBuilder();
        file.append("C:\\Users\\danor\\Desktop\\");
        file.append(keyboard.nextLine());
        file.append(".txt");
        asf = file.toString();
        try {
            FileWriter fwriter = new FileWriter(asf, true);
            PrintWriter out = new PrintWriter(fwriter);
            Scanner inputfile = new Scanner(fileorig);
            if (inputfile.hasNextLine()) {
                while (inputfile.hasNextLine()) {
                    line = (inputfile.nextLine());
                    out.println(line);
                    out.close();
                }
                System.out.println("\n");
            } else {
                System.out.println("the file contains nothing.");
            }
        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            System.out.println("something went wrong with the writing");
        }

    } catch (Exception e) {
        System.out.println("there was something wrong witht the reading");

    }
}

}

Upvotes: 0

Views: 61

Answers (3)

Dan Nordin
Dan Nordin

Reputation: 3

I just found the answer. After looking at some other codes and reading up on the scanner it appears that i forgot to set up the file. Duh. All i needed was a simple.

File origional = new File(fileorig);

and that seemed to solve my problem. thanks for all your help i couldn't have done it without you!

Upvotes: 0

Sindhoo Oad
Sindhoo Oad

Reputation: 1194

if you want to change the name of your file then File Class of io package provide the method renameTo.

public static void namechange() {

        String fileorig = "C:\\Users\\danor\\Desktop\\example.txt";
        File file = new File(fileorig);
        Scanner scanner = new Scanner(System.in);
        StringBuilder sb = new StringBuilder("C:\\Users\\danor\\Desktop\\");
        sb.append(scanner.nextLine());
        sb.append(".txt");
        file.renameTo(new File(sb.toString()));
        scanner.close();
}    

Upvotes: 0

Achintha Gunasekara
Achintha Gunasekara

Reputation: 1165

You are closing your PrintWriter object before finishing up writing the new file.

public static void namechange() {
    try {
        String fileorig = "C:\\Users\\danor\\Desktop\\example.txt";
        String asf;
        String line;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("what do you want the new file to be named.");
        StringBuilder file = new StringBuilder();
        file.append("C:\\Users\\danor\\Desktop\\");
        file.append(keyboard.nextLine());
        file.append(".txt");
        asf = file.toString();
        try {
            FileWriter fwriter = new FileWriter(asf, true);
            PrintWriter out = new PrintWriter(fwriter);
            Scanner inputfile = new Scanner(fileorig);
            if (inputfile.hasNextLine()) {
                while (inputfile.hasNextLine()) {
                    line = (inputfile.nextLine());
                    out.println(line);
                }
                System.out.println("line");
            } else {
                System.out.println("Reached the end of the file");
            }

            //close the PrintWriter when you finish writing to the file
            out.close();

        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            System.out.println("something went wrong with the writing : " + e.getMessage());
        }

    } catch (Exception e) {
        System.out.println("there was something wrong witht the reading : " + e.getMessage());

    }
}

Upvotes: 1

Related Questions