使用文本文件复制到新文本文件

时间:2015-10-14 03:04:06

标签: java io

我正在开发一个项目来读取,编写,格式化和更改桌面上预定文件的名称。除了namechange方法之外,一切正常。当我键入更改名称并将其文件提供给新文件时,它会使用旧文本文件的位置填充新文本文件。

更新!我发现了问题,但我仍然没有答案。看来,无论输入什么信息 扫描仪inputfile =新扫描仪(“此处”);   here 将打印出来,而不是文件的内容。因此,当下一步到达时,它会读取它应该扫描的文件而不是扫描器的 here 部分并将其打印到文件变量,然后将打印到输出,然后打印到文件。

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");

    }
}

}

3 个答案:

答案 0 :(得分:1)

在完成新文件的编写之前,您正在关闭PrintWriter对象。

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());

    }
}

答案 1 :(得分:0)

如果您想更改文件名,则File io包类提供方法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();
}    

答案 2 :(得分:0)

我刚刚找到答案。看了一些其他代码并在扫描仪上阅读后,我似乎忘记了设置文件。咄。我需要的只是一个简单的。

Sheets([Newest_Account]).Select

这似乎解决了我的问题。 感谢你的帮助,没有你,我无法做到!