[英]My array list to .txt file program is giving me several errors
I have an inventory application that sets a list of strings based on user input. 我有一个库存应用程序,它根据用户输入设置字符串列表。 Than it is supposed to export the strings to a .txt file. 比它应该将字符串导出到.txt文件。 I have never used FileWriter before, and am very much stuck. 我之前从未使用过FileWriter,而且非常困难。
Any help is appreciated. 任何帮助表示赞赏。
Here is all the relevant code: 以下是所有相关代码:
The three lines near the end that have 'writer.' 接近尾声的三行有“作家”。 in them are giving me a unreported exception IOException; must be caught or declared to be thrown
在他们给我一个unreported exception IOException; must be caught or declared to be thrown
unreported exception IOException; must be caught or declared to be thrown
. unreported exception IOException; must be caught or declared to be thrown
。
import java.util.*;
import java.io.FileWriter;
public class mVentory extends javax.swing.JFrame {
/**
* Creates new form mVentory
*/
public mVentory() {
initComponents();
}
public class ProductInfo {
String name;
String des;
String ID;
String num;
public ProductInfo(String name, String des, String ID, String num) {
this.name = name;
this.des = des;
this.ID = ID;
this.num = num;
}
}
public static void Inventory() {
}
//creat Array
ArrayList<String> Inventory = new ArrayList<String>();
private void AddGoActionPerformed(java.awt.event.ActionEvent evt) {
// Add Item
String Name, Description, Identification, Number, Item;
Name = NameIn.getText();
Description = DesIn.getText();
Identification = IDIn.getText();
Number = NumIn.getText();
Item = "" + Name + "," + Description + "," + Identification + "," + Number + ".";
Inventory.add(new String(Item));
NameIn.setText("");
DesIn.setText("");
IDIn.setText("");
NumIn.setText("");
}
private void ExportGoActionPerformed(java.awt.event.ActionEvent evt) {
FileWriter writer;
writer = new FileWriter("Inventory.txt");
for (String str : Inventory) {
writer.write(str);
}
writer.close();
}
}
You need a try/catch statement around your writer. 你的作家需要一个try / catch语句。
Such as: 如:
private void ExportGoActionPerformed(java.awt.event.ActionEvent evt) {
FileWriter writer = null;
try {
writer = new FileWriter("Inventory.txt");
for (String str : Inventory) {
writer.write(str);
}
} catch (IOException e) {
//handle exception, rethrow or log
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException ignore){
}
}
}
FileWriter extends OutputStreamWriter which is where it gets its write
method from. FileWriter扩展了OutputStreamWriter,它是从中获取write
方法的地方。
http://docs.oracle.com/javase/7/docs/api/java/io/OutputStreamWriter.html#write(char[], int, int) http://docs.oracle.com/javase/7/docs/api/java/io/OutputStreamWriter.html#write(char [],int,int)
According to the docs write throws an IOException
. 根据文档写入抛出IOException
。
In Java you have to either wrap the code that throws an exception in a try catch block. 在Java中,您必须在try catch块中包装抛出异常的代码。 Or throw the exception. 或抛出异常。
private void ExportGoActionPerformed(java.awt.event.ActionEvent evt) {
FileWriter writer;
try {
writer = new FileWriter("Inventory.txt");
for (String str : Inventory) {
writer.write(str);
}
writer.close();
} catch (IOException e) {
//Do something here to handle the error. Maybe publish a notification to the user that their file didn't write.
} finally {
try {
writer.close();
} catch (IOException ignore){}
}
}
Note that the FileWriter
constructor you're using can also throw an IOException
( http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.lang.String) ) as well as the close
method. 请注意,您正在使用的FileWriter
构造函数也可以抛出IOException
( http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter ( java.lang.String) )以及close
方法。 So you need to include all of those statements within the try block. 所以你需要在try块中包含所有这些语句。
The finally block is there to close your handle to the file if at all possible. 如果可能的话,finally块可以关闭文件的句柄。 The try-with-resource gets rid of the need to do that though. 尽管如此,try-with-resource也不需要这样做。 So I recommend using it. 所以我建议使用它。
http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.