我试图让我的java程序的输出写入文件。
用户输入一些不应包含在文件中的数据。当程序响应时,它应该向用户输出信息,并将 SOLELY 输出写入文件。
从示例中我开始在我的驱动程序类的顶部:
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static String lineFromOutput;
此代码位于我可能从程序接收输出的每个地方:
try {
lineFromInput = in.readLine();
FileWrite.write(lineFromInput);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
其所谓的课程是:
public class FileWrite {
public static void write(String message) {
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter("output.txt"), true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.write(message);
out.close();
}
}
它会创建输出文件,但就是这样。程序的输出都没有写入。 我已经查看了很多例子,这似乎是最简单的方法,尽管我对其他建议持开放态度。
谢谢!
答案 0 :(得分:2)
每次写入调用都会打开和关闭文本文件。每次打开它都会被覆盖,所以我希望只有最后一件事可以写在文件中。
我建议从构造函数中打开输出文件,然后从close方法关闭它。
答案 1 :(得分:0)
我认为下面的语句中应该InputStremReader
单t
:
static BufferedReader in= new BufferedReader(new OutputtStreamReader(System.in));
static String lineFromOutput;
作为
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static String lineFromOutput;
编辑:这很好用。 请确保通过输入控制台提供输入。另请注意,它只能读写(覆盖)单行。
public class FileWrite {
public static void write(String message) {
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter("output.txt"), true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.write(message);
out.close();
}
public static void main(String[] args){
String lineFromInput;
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
lineFromInput = in.readLine();
FileWrite.write(lineFromInput);
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
编辑2:更新了多行输入的程序。 每次写入都不是打开和关闭文件的最佳方式,但我只是想让你的程序稍作修改。请告诉我,如果你需要建议避免重复打开/关闭输出文件。
更改要点:
以append
模式打开文件。
public class FileWrite {
public static void write(String message) {
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter("output.txt", true), true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.write(message);
out.close();
}
public static void main(String[] args){
String lineFromInput = "";
try {
System.out.println("Provide the inputs in any number of lines");
System.out.println("Type \"exit\" in new line when done");
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
while(!"exit".equals(lineFromInput)){
lineFromInput = in.readLine();
FileWrite.write(lineFromInput+System.lineSeparator());
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
EDIT3:您使用Scanner
更新的程序来阅读输入内容:
private static HashMap<Integer, Object> shapes =
new HashMap<Integer, Object>();
static int i = 0;
public static void main(String[] args) {
PrintWriter output = null;
Scanner scanner = new Scanner(System.in);
try {
output = new PrintWriter(new FileWriter("output.txt"), true);
} catch (IOException e1) {
System.err.println("You don't have accress to this file");
System.exit(1);
}
String command = "";
while(!"quit".equalsIgnoreCase(command)){
System.out.println("Enter your Command: ");
command = scanner.next();
if (command.equalsIgnoreCase("create")) {
String type = scanner.next();
if (type.equalsIgnoreCase("line")) {
double length = scanner.nextDouble();
Line l = new Line(length);
scanner.nextLine();//flush the previous line
String line = scanner.nextLine();
output.format("%s", line);
shapes.put(i, l);
i++;
}else if (type.equalsIgnoreCase("circle")) {
double radius = scanner.nextDouble();
String color = scanner.next();
Circle c = new Circle(radius, Colors.valueOf(color));
scanner.nextLine();//flush the previous line
String line = scanner.nextLine();
output.format("%s", line);
shapes.put(i, c);
i++;
}else if (type.equals("rectangle")) {
double length = scanner.nextDouble();
double width = scanner.nextDouble();
String color = scanner.next();
Rectangle r = new Rectangle(length, width,
Colors.valueOf(color));
scanner.nextLine();//flush the previous line
String line = scanner.nextLine();
output.format("%s", line);
shapes.put(i, r);
i++;
}else if (type.equals("square")) {
double length = scanner.nextDouble();
String color = scanner.next();
Square s = new Square(length, Colors.valueOf(color));
scanner.nextLine();//flush the previous line
String line = scanner.nextLine();
output.format("%s", line);
shapes.put(i, s);
i++;
}
}else if (command.equals("printbyperimeter")) {
Shape[] shapeArray = shapes.values().toArray(new Shape[0]);
Arrays.sort(shapeArray);
System.out.println("Print in ascending order...");
for (int j = 0; j < shapeArray.length; j++) {
Shape temp = shapeArray[j];
if (temp.getClass().getName().equals("Line")) {
System.out.println("Shape: "
+ temp.getClass().getName() + ", Perimeter: "
+ temp.getPerimeter());
} else {
System.out.println("Shape: "
+ temp.getClass().getName() + ", Color: "
+ ((Colorable) temp).getColor()
+ ", Perimeter: " + temp.getPerimeter());
}
}
}else if (command.equals("printbyarea")) {
Shape[] shapeArray = shapes.values().toArray(new Shape[0]);
System.out.println("Print in random order...");
for (int j = 0; j < shapeArray.length; j++) {
Shape temp = shapeArray[j];
if (!temp.getClass().getName().equals("Line")) {
System.out.println("Shape: "
+ temp.getClass().getName() + ", Color: "
+ ((Colorable) temp).getColor() + ", Area: "
+ ((Areable) temp).getArea());
}
}
}else if (command.equals("quit")) {
scanner.close();
System.exit(0);
}
}
output.close();
}
答案 2 :(得分:0)
尝试使用代码。这个对我有用。您只需要更改文件路径以匹配您希望输出的位置。我在这里使用的是BufferedWriter,我认为是首选。
public static void main(String[] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String lineFromOutput;
try {
lineFromOutput = in.readLine();
FileWrite.write(lineFromOutput);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static class FileWrite {
private static void write(String message) throws IOException {
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(new File("C:\\Users\\Teresa\\Dropbox\\output.txt")));
//Replace the above line with your path.
out.write(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.close();
}
}