Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从外部文件向JComboBox添加项?_Java_File Io_Io_Text Processing_Jcombobox - Fatal编程技术网

Java 如何从外部文件向JComboBox添加项?

Java 如何从外部文件向JComboBox添加项?,java,file-io,io,text-processing,jcombobox,Java,File Io,Io,Text Processing,Jcombobox,在从外部文件向Java中的JComboBox添加项目时,我需要一些帮助 这是到目前为止我的代码: //Loading the Names: File Names_File = new File("Data" + File.separator + "Names.txt"); FileInputStream fis = null; BufferedInputStream bis = null; DataInputStream dis

在从外部文件向Java中的JComboBox添加项目时,我需要一些帮助

这是到目前为止我的代码:

 //Loading the Names:

        File Names_File = new File("Data" + File.separator + "Names.txt");
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;

        String str_Data = ""; //For storing the input from the file.

        try
        {
            fis = new FileInputStream(Names_File);

            // Here BufferedInputStream is added for fast reading.
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);


            str_Data = dis.readLine(); //Reading the line from the file.

            StringTokenizer st = new StringTokenizer(str_Data); //Tokenizing the line.

            //The below line adds only one item. The objective is adding all the items.

            //*** Requesting help here ***

            cmb_Name.addItem(st.nextToken("|"));

            //*** Requesting help here ***

            // Disposing and closing all the resources after using them.
            fis.close();
            bis.close();
            dis.close();
        }

        catch (FileNotFoundException e)
        {
            System.err.println("Error: File not found!");
            JOptionPane.showMessageDialog(null, "Error: File not found!", "Error Message",
                                          JOptionPane.ERROR_MESSAGE);
        }

        catch (IOException e)
        {
            System.err.println("Error: Unable to read from file!");
            JOptionPane.showMessageDialog(null, "Error: Unable to read from file!", "Error Message",
                                          JOptionPane.ERROR_MESSAGE);
        }
我的外部文件的格式主要如下:

 //Loading the Names:

        File Names_File = new File("Data" + File.separator + "Names.txt");
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;

        String str_Data = ""; //For storing the input from the file.

        try
        {
            fis = new FileInputStream(Names_File);

            // Here BufferedInputStream is added for fast reading.
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);


            str_Data = dis.readLine(); //Reading the line from the file.

            StringTokenizer st = new StringTokenizer(str_Data); //Tokenizing the line.

            //The below line adds only one item. The objective is adding all the items.

            //*** Requesting help here ***

            cmb_Name.addItem(st.nextToken("|"));

            //*** Requesting help here ***

            // Disposing and closing all the resources after using them.
            fis.close();
            bis.close();
            dis.close();
        }

        catch (FileNotFoundException e)
        {
            System.err.println("Error: File not found!");
            JOptionPane.showMessageDialog(null, "Error: File not found!", "Error Message",
                                          JOptionPane.ERROR_MESSAGE);
        }

        catch (IOException e)
        {
            System.err.println("Error: Unable to read from file!");
            JOptionPane.showMessageDialog(null, "Error: Unable to read from file!", "Error Message",
                                          JOptionPane.ERROR_MESSAGE);
        }
詹姆斯|罗伯特|爱丽丝

名称由“|”符号分隔

我上面的代码在此示例中只添加了一个元素,即(“James”)。我需要把这三个加在一起,可能是一个循环什么的。我的想法是,我不确定我的外部文件中会有多少个名称。因此,使用简单的计数器不会有帮助

非常感谢您的任何建议

提前感谢您的帮助

尝试以下方法:

   StringTokenizer st = new StringTokenizer(str_Data); //Tokenizing the line.

   while(st.hasMoreTokens()) {
        cmb_Name.addItem(st.nextToken("|"));
   }