我需要帮助使用Printwriter将一些数组对象的内容(以下是我正在使用的两个数组示例)写入文本文件。有任何想法吗?我是初学者,所以越简单越好,谢谢!
Astronauts[0][0] = new Passengers(-1, "", 1, 0, 0, "", "", 0, "", "", "", "", "");
Astronauts[0][1] = new Passengers(0, "Pilot", 2424, 14, 0, "Bruce", "Banner", 0, "678-884-6325", "Mom", "678-884-6323","","");
Astronauts[0][2] = new Passengers(0, "Pilot", 1248, 3, 0, "Sally", "Forth", 0, "678-921-1135", "Hannah", "678-921-1130","","");
Astronauts[1][0] = new Passengers(-1, "", 2, 0, 0, "", "", 0, "", "", "", "", "");
Astronauts[1][1] = new Passengers(0, "Pilot", 1022, 55, 0, "Buz", "Aldrin", 0, "404-014-4553", "June", "404-014-4555","","");
Astronauts[1][2] = new Passengers(0, "Pilot", 2813, 8, 0, "Alice", "Dyer", 0, "678-884-6325", "Mom", "678-884-6323","","");
答案 0 :(得分:2)
我不确定我是否正确捕捉到您的问题,因为将数组内容写入文件非常简单:
String[] arr = {"a", "b", "c"};
try {
PrintWriter pw = new PrintWriter(new FileWriter("output.txt"));
pw.println(Arrays.toString(arr));
pw.flush();
System.out.println("Finished");
} catch (IOException e) {
e.printStackTrace();
}
<强> [编辑] 强>
我意识到我可能没有解决你的整个问题。如果您想知道如何编写数组中包含的对象的所需特征,您可以覆盖自定义类的toString()方法:
class A {
public static void main(String[] args) {
B[] bs = {new B("a", "b"),
new B("c", "d"),
new B("e", "f"),
new B("g", "h")};
try {
PrintWriter pw = new PrintWriter(new FileWriter("output.txt"));
for (B b : bs) {
pw.println(b);
}
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Finished");
}
}
class B {
private String prop1;
private String prop2;
public B (String prop1, String prop2) {
this.prop1 = prop1;
this.prop2 = prop2;
}
@Override
public String toString() {
return this.prop1 + " " + this.prop2;
}
}
答案 1 :(得分:0)
PrintWriter
允许String
,因此您可以覆盖toString()
类中的Astronauts
方法,然后迭代1-D或2-D维的数组:
顺便说一下,变量名应该以小写字符开头。
1-d
for (int i = 0; i < astronauts.length; i++) {
pw.print(Arrays.toString(astronauts[i]);
}
2-d
for (int i = 0; i < astronauts.length; i++) {
for (int j = 0; j < astronauts[i].length; i++) {
pw.print(astronauts[i][j]);
}
}
别忘了flush()
和close()
PrintWriter
答案 2 :(得分:0)
以下是您可能想要的方式。
String twoDArray[][] = {{"one","two"},{"one","two"},{"one","two"}};
String filePath = "C:/Users/arjun.lajpal/Desktop/dummyFile.txt";
PrintWriter writer = null;
try {
writer = new PrintWriter(filePath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer.printf("%20s%20s","Astraunauts","Passengers");
writer.println();
for(int i=0;i<twoDArray.length;i++){
for(int j=0;j<twoDArray[i].length;j++)
writer.printf("%20s",twoDArray[i][j]);
writer.println();
}
writer.flush();
writer.close();
这不仅可以生成您的文件,还可以以漂亮的表格格式写入文件,而无需覆盖toString()方法。
答案 3 :(得分:0)
由于您的数组内容属于同一个类Passengers
,如果您对默认的Array.toString格式不满意,我会在{{{{{}}中创建一个toString()
方法1}}返回所需的字符串表示。
然后:
Passengers
注意:正如其他人所提到的,我将try {
PrintWriter printWriter = new PrintWriter(new FileWriter("output.txt"));
for(Passengers[] passengers: Astronauts) {
for(Passengers passenger: passengers) {
printWriter.println(passenger);
}
}
printWriter.close(); // no need to flush, since close() does it anyway.
} catch (IOException e) {
e.printStackTrace();
}
变量重命名为小写Astronauts
。我还将astronauts
课程重命名为Passengers
。
编辑:使用上面的代码,输出文件应出现在运行程序的工作目录中。或者,您可以提供完整的文件路径,例如Passenger
,但在这种情况下,您需要确保目录路径已存在。
另一种方法是修改代码以自动为您创建路径:
C:/Users/Me/directory/output.txt