【问题标题】:Java outputting console output to text file inside of eclipseJava将控制台输出输出到eclipse内部的文本文件
【发布时间】:2015-10-09 01:06:21
【问题描述】:

我试图将控制台输出到一个文本文件,当询问用户想要输出到哪个文件时,用户可以创建/命名该文本文件,但由于某种原因,它只在控制台中显示结果。有没有办法将输出发送到 Eclipse 内部的新文本文件?这是我写的代码。

代码:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class Project03 {

    public static void main(String[] args) throws FileNotFoundException {
        CaesarCipher CaesarCipher = new CaesarCipher("", 0);
        Scanner choice = new Scanner(System.in);
        Scanner intoff = new Scanner(System.in);
        Scanner output = new Scanner(System.in);
        System.out.println("Type E to encrypt a file, or D to decrypt a file");
        String pick = choice.nextLine();
        if (pick.toLowerCase().equals("e")) {
            System.out.println("Enter the file path of the text you'd like to encrypt: ");
            File file = new File(choice.nextLine());
            Scanner textfile = new Scanner(file);
            String line = textfile.nextLine();
            System.out.println("Enter the offset you would like to use (must be 1-25)");
            int offset = intoff.nextInt();
            System.out.println("Name the file you would like to output to");
            String TextOutput = output.nextLine();
            System.out.println(CaesarCipher.encode(line, offset));
            PrintStream out = new PrintStream(new FileOutputStream(TextOutput));
            System.setOut(out);
        } else if (pick.toLowerCase().equals("d")) {
            System.out.println("Enter the file path of the text you'd like to decrypt: ");
            File file = new File(choice.nextLine());
            Scanner textfile = new Scanner(file);
            String line = textfile.nextLine();
            System.out.println("Enter the offset you would like to use (must be 1-25)");
            int offset = choice.nextInt();
            System.out.println("Name the file you would like to output to");
            String TextOutput = output.nextLine();
            System.out.println(CaesarCipher.decode(line, offset));
            PrintStream out = new PrintStream(new FileOutputStream(TextOutput));
            System.setOut(out);
        } else {
            System.out.println("Something went Wrong");
        }
    }
}

【问题讨论】:

  • 您只需要先创建文件并将其包含在 eclipse 中。
  • 你知道你应该在写之前设置System.out,否则它不会重定向?此外,您可能应该使用简单的WriterOutputStream,看看Basic I/O。您也不需要三个Scanners 都从相同的输入中读取,他们都会同时看到相同的东西......
  • “有没有办法将输出发送到 Eclipse 内部的新文本文件?” - 我认为这不是一个好主意,如果用户输入“C:\MyTextFile.txt”?然后应该将文件写入用户指示它的任何位置。如果用户没有提供路径,那么它将被写入执行程序的同一位置。如果他们提供的路径不存在,那么你就有麻烦了;)

标签: java eclipse file output text-files


【解决方案1】:

这是你的工作代码

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Project03 {

    public static void main(String[] args) throws FileNotFoundException {

        Scanner input = new Scanner(System.in);
        // Don't need to bulk of Scanner object
        System.out.println("Type E to encrypt a file, or D to decrypt a file");
        String pick = input.nextLine();
        if (pick.toLowerCase().equals("e")) {
            System.out
                    .println("Enter the file path of the text you'd like to encrypt: ");
            File file = new File(input.nextLine());
            Scanner inputFromFile = new Scanner(file);
            String line = inputFromFile.nextLine();
            System.out
                    .println("Enter the offset you would like to use (must be 1-25)");
            int offset = input.nextInt();
            input.nextLine(); // Consume Extra NewLine
            System.out.println("Name the file you would like to output to");
            String textOutput = input.nextLine();

            PrintStream out = new PrintStream(new FileOutputStream(textOutput));
            System.setOut(out);

            System.out.println(CaesarCipher.encode(line, offset)); // This line should be placed after System.setOut(out), to redirect output to the file

            inputFromFile.close();

        } else if (pick.toLowerCase().equals("d")) {
            System.out
                    .println("Enter the file path of the text you'd like to decrypt: ");
            File file = new File(input.nextLine());
            Scanner inputFromFile = new Scanner(file);
            String line = inputFromFile.nextLine();
            System.out
                    .println("Enter the offset you would like to use (must be 1-25)");
            int offset = input.nextInt();
            input.nextLine(); // Consume Extra NewLine
            System.out.println("Name the file you would like to output to");
            String textOutput = input.nextLine();

            PrintStream out = new PrintStream(new FileOutputStream(textOutput));
            System.setOut(out);

            System.out.println(CaesarCipher.decode(line, offset));
            inputFromFile.close();

        } else {
            System.out.println("Something went Wrong");
        }
        input.close();

    }
}

一些建议

  1. 遵循命名规则
  2. 对于每种类型的流,每种类型使用一个 Scanner 对象。
  3. 以静态方式调用静态方法。

【讨论】:

  • 实际上,现在我仔细看看 OP 的代码,您的示例可能没有错,但我会强调您没有使用 System.setOut(out); 的事实...可能需要清洗我的眼睛用漂白剂:P - +1 给你,这是一个更简单的解决方案,然后尝试重定向 stdout
  • 当用户输入文件名时,文件到底去哪儿了?只有在其中包含文件路径时它才能工作吗?此外,当我厌倦了将所有扫描仪更改为单个扫描仪时,它会抛出一个未找到文件的异常,唉,您修改后的代码有效.. 嗯..
  • @DougBarta 如果用户只提供文件名,则输出文件将转到您的项目目录,刷新项目后您可以看到。
  • 但如果用户给出完整路径,那么输出文件将转到该目录。如果目录无效,那么您可能会得到 .FileNotFoundException。
  • 我认为这是因为在 .nextInt( ) 之后缺少 .nextLine( ) 导致它被抛弃
【解决方案2】:

由于某种原因,它只在控制台中显示结果

正如@MadProgrammer 所说,您在打开“out”文件之前写入“System.out”,因此结果不会出现在文件中。

System.out.println(CaesarCipher.encode(line, offset));
PrintStream out = new PrintStream(new FileOutputStream(TextOutput));
System.setOut(out);

你想要这样的东西吗:

char[] decoded = CaesarCipher.decode(line, offset);
System.out.println(decoded);
PrintStream out = new PrintStream(new File(TextOutput));
out.print(decoded);
out.close();

现在,如果你真的想重定向 System.out,那就是另一个故事了(但它的作用相同;你仍然需要调用两个“println”,一个用于文件,另一个用于控制台):

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class Project03 {

    public static class CaesarCipher {

        public CaesarCipher(String string, int i) {
        }

        public char[] encode(String line, int offset) {
            return line.toCharArray();
        }

        public char[] decode(String line, int offset) {
            return line.toCharArray();
        }

    }

    public static class OutStream extends PrintStream {
        PrintStream out;

        public OutStream(File file, PrintStream out) throws FileNotFoundException {
            super(file);
            this.out = out;
        }

        @Override
        public void println(char[] x) {
            super.println(x);
            out.println(x);
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        CaesarCipher CaesarCipher = new CaesarCipher("", 0);
        Scanner choice = new Scanner(System.in);
        Scanner intoff = new Scanner(System.in);
        Scanner output = new Scanner(System.in);
        System.out.println("Type E to encrypt a file, or D to decrypt a file");
        String pick = choice.nextLine();
        if (pick.toLowerCase().equals("e")) {
            System.out.println("Enter the file path of the text you'd like to encrypt: ");
            File file = new File(choice.nextLine());
            Scanner textfile = new Scanner(file);
            String line = textfile.nextLine();
            System.out.println("Enter the offset you would like to use (must be 1-25)");
            int offset = intoff.nextInt();
            System.out.println("Name the file you would like to output to");
            String TextOutput = output.nextLine();
            OutStream out = new OutStream(new File(TextOutput), System.out);
            System.setOut(out);
            System.out.println(CaesarCipher.encode(line, offset));
        } else if (pick.toLowerCase().equals("d")) {
            System.out.println("Enter the file path of the text you'd like to decrypt: ");
            File file = new File(choice.nextLine());
            Scanner textfile = new Scanner(file);
            String line = textfile.nextLine();
            System.out.println("Enter the offset you would like to use (must be 1-25)");
            int offset = choice.nextInt();
            System.out.println("Name the file you would like to output to");
            String TextOutput = output.nextLine();
            OutStream out = new OutStream(new File(TextOutput), System.out);
            System.setOut(out);
            System.out.println(CaesarCipher.encode(line, offset));
        } else {
            System.out.println("Something went Wrong");
        }
    }
}

如果您使用的不仅仅是“println”,您将不得不在“OutStream”中重载它。 我没有故意触碰其余的代码。

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 2011-07-21
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多