Java 将控制台输出输出到 eclipse 内的文本文件

Java outputting console output to text file inside of eclipse

我试图将控制台输出到一个文本文件,当用户询问用户想要输出到哪个文件时,该文件可以 created/named,但由于某种原因,它只在安慰。有没有办法将输出发送到 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");
        }
    }
}

这是你的工作代码

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. 以静态方式调用静态方法

for some reason it only shows the result in the console

正如@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" 中重载它。 我没有故意触及其余代码。