while ((line = reader.readLine()) != null)读取多行文件时,每行数据组数相同,用空格隔开,为什么会数组越界
时间: 2024-03-15 15:47:49 浏览: 28
可能是因为你在使用String的split方法将每行数据按照空格分割成数组时,有些行的数据中含有多余的空格或者没有数据,导致分割出的字符串数组长度不一致,进而导致数组越界。你可以在使用split方法前,先使用trim方法去除每行数据的首尾空格,再使用正则表达式进行分割,以避免这种情况的发生。例如,你可以使用下面的代码进行分割:
```
String[] array = line.trim().split("\\s+");
```
其中,trim方法用于去除首尾空格,而split方法使用正则表达式"\\s+"表示按照一个或多个空格进行分割。这样处理后,每行数据分割出的字符串数组长度应该是相同的,不会引起数组越界。
相关问题
读取文件的内容有多行,每行包含“账号”和“密码”用空格隔开,账号和密码需要分别读取
好的,这里是一个可以读取多行账号和密码的示例代码:
```
try {
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(" ");
String username = parts[0];
String password = parts[1];
System.out.println("Username: " + username + ", Password: " + password);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
```
这里使用了`split()`方法将每行内容分割成两部分,然后分别读取账号和密码。希望对您有所帮助!
private void updateTable() { int count=0; List<user> userList=new ArrayList<>(); //读取文件信息,加入到列表,下面读列表塞到页面上数据 File inputFile = new File("chufang.txt"); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(inputFile)); String currentLine; while((currentLine = reader.readLine()) != null) { String[] arr = currentLine.split("\\s+"); if(arr.length >= 5 ) { user user =new user(); user.setZiduan1(arr[0]); user.setZiduan2(arr[1]); user.setZiduan3(arr[2]); user.setZiduan4(arr[3]); user.setZiduan5(arr[4]); userList.add(user); } } alluserList=userList; String[][] datas = new String[userList.size()][5]; for (int i = 0; i < userList.size(); i++) { datas[i][0] = userList.get(i).getZiduan1(); datas[i][1] = userList.get(i).getZiduan2(); datas[i][2] = userList.get(i).getZiduan3(); datas[i][3] = userList.get(i).getZiduan4()+""; datas[i][4] =userList.get(i).getZiduan5()+""; } table.setModel(new DefaultTableModel(datas, headers)); reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
这段代码是 `updateTable()` 方法,用于更新表格的数据。具体的更新逻辑如下:
1. 初始化一个计数器 `count`,并创建一个空的 `userList` 列表,用于存储从文件中读取的数据。
2. 创建一个 File 对象 `inputFile`,表示要读取的文件。
3. 使用 BufferedReader 对象 `reader` 读取文件,并逐行读取文件内容。
4. 对于每一行,使用空格作为分隔符,将其拆分为一个字符串数组 `arr`。
5. 如果拆分后的数组长度大于等于 5,表示该行数据有效,创建一个 `user` 对象,并将拆分后的数据赋值给对应的字段。
6. 将 `user` 对象添加到 `userList` 列表中。
7. 将 `userList` 赋值给全局变量 `alluserList`,以便其他方法使用。
8. 根据 `userList` 的大小创建一个二维数组 `datas`,并将列表中的数据赋值给 `datas`。
9. 使用 `DefaultTableModel` 设置表格的数据模型为 `datas`,并将其应用于表格。
10. 关闭文件读取流。
11. 捕获可能的异常,并打印异常堆栈跟踪信息。
总的来说,这段代码的作用是从文件中读取数据,并将读取到的数据更新到表格中显示。读取文件时,将每一行拆分为字符串数组,并将数组中的元素赋值给对应的字段,然后将 `user` 对象添加到列表中。最后,将列表中的数据赋值给表格的数据模型,以实现表格数据的更新。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)