我目前正在用Java进行拼贴项目,我遇到了一些问题,并希望你们中的一个能够发现我看不到的东西。下面是我用来将Array []写入文件的代码。数组本身是[100]。我遇到的问题是我无法理解堆栈告诉你什么或如何解决问题并阻止它。它正在调用的文件已经由另一个类创建,我已经检查确认它
public void writeGridToFile() throws IOException {
FileWriter fw = new FileWriter("D:/GridArrayFile.txt");
try {
for(int i = 0; i < 100; i++){
BufferedWriter bw = new BufferedWriter(fw);
bw.write(GridArray[i]);
bw.close();
}
}
catch (IOException e) {
e.printStackTrace();
System.out.println("Error");
}
finally {
//TODO
System.out.println("Finished");
}
}
这是正在打印的异常堆栈
java.io.IOException: Stream closed
at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.BufferedWriter.close(Unknown Source)
at playgame.writeGridToFile(playgame.java:195)
at playgame.CreateFile(playgame.java:183)
at playgame$1.actionPerformed(playgame.java:73)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
希望你能找到问题,同时作为旁注,如果这是一个简单的快速让我离开我只使用java大约1年半,它对我来说仍然是新的。
答案 0 :(得分:1)
public void writeGridToFile() throws IOException {
FileWriter fw = new FileWriter("D:/GridArrayFile.txt");
BufferedWriter bw = new BufferedWriter(fw);
try {
for(int i = 0; i < 100; i++){
bw.write(GridArray[i]);
}
}
catch (IOException e) {
e.printStackTrace();
System.out.println("Error");
}
finally {
bw.close();
System.out.println("Finished");
}
}
答案 1 :(得分:0)
循环中的陈述:
bw.close();
也会关闭FileWriter fw
。也许你正在寻找这样的东西?
public static void main(String[] args)
throws IOException {
int[] gridArray = new int[] { 'H', 'u', 'r', 'r', 'a', 'y', '!' };
try (FileWriter fw = new FileWriter("gridArrayFile.txt")) {
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < gridArray.length; i++) {
bw.write(gridArray[i]);
}
bw.flush();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error");
} finally {
System.out.println("Finished");
}
}
我做了并运行,之后文件gridArrayFile.txt
被创建并包含字符串Hurray!
。
答案 2 :(得分:0)
感谢您的帮助,以下所有人都是解决此问题的固定解决方案的代码。
public void writeGridToFile() throws IOException {
try(BufferedWriter bw = new BufferedWriter(new FileWriter("D:/GridArrayFile.txt"))) {
for(int i = 0; i < 100; i++){
bw.write(Integer.toString(GridArray[i]));
}
bw.flush();
}
catch (IOException e) {
e.printStackTrace();
System.out.println("Error");
}
finally {
System.out.println("Finished");
}
}