简体   繁体   English

我的.txt文件程序的数组列表给了我几个错误

[英]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构造函数也可以抛出IOExceptionhttp://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.

相关问题 我的Java程序给我99个错误 - My java program is giving me 99 wrong errors java程序将txt文件传递到数组列表中以设置程序的其余部分时遇到了困难 - Difficulty with java program passing the txt file into an array list to setup the rest of my program 我的程序给我一个StackOverflowError - My program is giving me a StackOverflowError 我的Java程序给我一个IndexOutOfBoundsException - My java program is giving me an IndexOutOfBoundsException 我的Java程序给了我StringIndexOutOfBoundsException - my java program is giving me a StringIndexOutOfBoundsException 编写一个从List.txt文件中读取列表的Java程序,该程序应将排序后的数组输出到SortedList.txt文件 - Write a java program that reads a list from a List.txt file and the program should output the sorted array to SortedList.txt file 我不知道为什么我的程序给我花括号错误。 我怎样才能解决这个问题 ? 请帮忙 - I don't know why my program is giving me curly brace errors. How can I fix this ? Please help 清单给我错误 - Manifest giving me errors OutputStream代码没有给出任何错误但缺少txt文件 - OutputStream code not giving any errors but txt file is missing 我的简单数组程序未提供预期的输出 - My simple array program not giving the expected output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM