正如我上面所说,我有几个问题。我正在尝试导出我的数组列表' Inventory'到.txt文件。 这是代码,欢迎任何帮助!
import java.io.FileWriter.*;
import java.io.*;
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 (IOExeption e){
AlertOut.setText("I'm afraid your file could not be writen at this time.");
} finally {
try {
writer.close();
} catch (IOException ignore){}
}
public static void main(String args[]) {
第12行:找不到符号:Class IOExeption。
第18行:解析时到达文件末尾
第20行:非法开始表达。
以下是完整代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
import java.io.FileWriter.*;
import java.io.*;
/**
*
* @author matthewmurchison
*/
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> ();
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
NameIn = new javax.swing.JTextField();
IDIn = new javax.swing.JTextField();
DesIn = new javax.swing.JTextField();
NumIn = new javax.swing.JTextField();
jLabel14 = new javax.swing.JLabel();
jLabel15 = new javax.swing.JLabel();
AddGo = new javax.swing.JButton();
ExportGo = new javax.swing.JButton();
AlertOut = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
jLabel2.setText("Name:");
jLabel3.setText("ID");
jLabel4.setText("Descripion");
jLabel5.setText("Number");
NameIn.setText("jTextField1");
IDIn.setText("jTextField2");
IDIn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
IDInActionPerformed(evt);
}
});
DesIn.setText("jTextField3");
NumIn.setText("jTextField4");
jLabel14.setText("Add");
jLabel15.setText("Export to .txt");
AddGo.setText("Add");
AddGo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AddGoActionPerformed(evt);
}
});
ExportGo.setText("Export");
ExportGo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExportGoActionPerformed(evt);
}
});
AlertOut.setToolTipText("");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(0, 0, Short.MAX_VALUE)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(jLabel14)
.add(245, 245, 245))
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(jLabel15)
.add(AddGo)
.add(ExportGo))
.add(233, 233, 233))))
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(layout.createSequentialGroup()
.add(225, 225, 225)
.add(jLabel1)
.add(117, 117, 117))
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel2)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel4)
.add(jLabel5)
.add(jLabel3))
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(DesIn)
.add(NumIn)
.add(NameIn)
.add(IDIn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 202, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))))
.add(layout.createSequentialGroup()
.add(150, 150, 150)
.add(AlertOut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 225, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(149, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 22, Short.MAX_VALUE)
.add(jLabel14)
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel2)
.add(NameIn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel3)
.add(IDIn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel4)
.add(DesIn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(NumIn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel5))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(AddGo)
.add(18, 18, 18)
.add(jLabel15)
.add(40, 40, 40)
.add(ExportGo)
.add(18, 18, 18)
.add(AlertOut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(194, 194, 194))
);
pack();
}// </editor-fold>
private void IDInActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
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;
try {
writer = new FileWriter("Inventory.txt");
for(String str: Inventory) {
writer.write(str);
}
writer.close();
}catch (IOException e){
AlertOut.setText("I'm afraid your file could not be writen at this time.");
} finally {
try {
writer.close();
} catch (IOException ignore){}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(mVentory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(mVentory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(mVentory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(mVentory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new mVentory().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton AddGo;
private javax.swing.JLabel AlertOut;
private javax.swing.JTextField DesIn;
private javax.swing.JButton ExportGo;
private javax.swing.JTextField IDIn;
private javax.swing.JTextField NameIn;
private javax.swing.JTextField NumIn;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
// End of variables declaration
}
答案 0 :(得分:0)
我能解决的唯一问题是你在第12行拼错了IOException
。