我试图将控制台输出到文本文件,当用户要求输出要输出的文件时,该文件可由用户创建/命名,但由于某种原因,它仅在控制台中显示结果。有没有办法将输出发送到INSIDE eclipse的新文本文件?这是我写的代码。
代码:
/// <reference path="typings/angular2/angular2.d.ts" />
import {Component, View, bootstrap, DynamicComponentLoader, ElementRef } from 'angular2/angular2';
@Component({
selector:'my-app'
})
@View({
template: '<h1>Hello {{name }}</h1>'
})
class MyAppComponent{
name: string;
constructor(@Inject(DynamicComponentLoader) loader: DynamicComponentLoader, @Inject(ElementRef) elem: ElementRef) {
this.name='World';
}
}
bootstrap(MyAppComponent);
答案 0 :(得分: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 :(得分:0)
出于某种原因,它只在控制台中显示结果
正如@MadProgrammer所说,你写信给&#34; System.out&#34;在打开&#34; out&#34;之前因此,文件结果不会出现在文件中。
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,那就是另一个故事就是这样(但它也是如此;你仍然需要调用两个&#34; println&#34;一个用于文件,另一个用于文件,另一个对于控制台):
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");
}
}
}
如果您使用超过&#34; println&#34;你必须在&#34; OutStream&#34;中重载它。 我没有故意触及剩下的代码。