java - 使用文本文件复制到新的文本文件

标签 java io

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

更新!我已经找到了问题,但我仍然没有答案。似乎无论输入什么信息 扫描仪输入文件 = new Scanner("这里"); 这里将被打印出来,而不是文件的内容。因此,当下一步到来时,它读取应该扫描的文件,而是采用扫描仪的此处部分并将其打印到文件变量,然后打印到输出,然后打印到文件。

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

    }
}

}

最佳答案

在完成写入新文件之前,您将关闭 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());

    }
}

关于java - 使用文本文件复制到新的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33115912/

相关文章:

java - 如何从 Java EE 服务器(JBoss)上同一耳朵的不同 war 中访问文件?

multithreading - 为什么这个 Haskell 程序在写入文件时会挂起?

Java小程序: Caller-Allowable-Codebase does not work

java - 如何将列表发送到请求它的 jsp?

java - 为什么不能在Java中继承构造函数?

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

c++ - Xcode 找不到“iostream”文件

c - 打印文件中前 10 个重复出现的单词

在c中复制可执行文件

java.lang.ClassNotFoundException : org. jboss.logging.Logger 错误