gpt4 book ai didi

java - 如何返回java文件中的行

转载 作者:行者123 更新时间:2023-12-02 05:42:29 25 4
gpt4 key购买 nike

我需要从外部文件(java)将项目添加到组合框。我的问题是它只读取第一行,这是我的代码:

File Names_File = new File("Data" + File.separator + "C://Users//Hatem//Desktop//FichAdr.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;

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

try
{
fis = new FileInputStream("C://Users//Hatem//Desktop//FichAdr.txt");

// 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 ***

jComboBox1.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);
}

我是编程新手,有人可以帮我解决这个问题吗?谢谢。

最佳答案

你应该使用像这样的循环:

while((str_data = dis.readLine()) != null){
parse your line here
}

额外的括号提高了指令的优先级。

关于java - 如何返回java文件中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24398280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com