java 解析dat文件,如何从Java中的.dat文件中读取一行然后需要分隔?

本文介绍如何使用Java读取不包含逗号分隔的.dat文件。通过substring方法,根据预定义的列宽来分离每行数据,分别获取课程ID、学生姓名等信息,并将这些信息输入到不同的类中。示例代码展示了如何处理每行数据。
摘要由CSDN通过智能技术生成

I am attempting to read a .dat file using Java in TextPad. The .dat file has multiple lines of code but each line has separate pieces of information that I need for different methods in my main method and object classes. How do I separate the information provided in the file and input the separate pieces into any of my classes?

I know how to read basic input from a .txt file but it is not working with the .dat file. I do not know how to separate data that does not have commas, and I am not allowed to change the data in the .dat file.

When viewing the .dat file in TextPad it shows up as standard characters, not as binary. The code I am trying to read is below:

1001Intro. to CompSci 4ALBERT, PETER A. Comp Info System A

1001Intro. to CompSci 4ALLENSON, SHEILA M. Comp Info System B

1001Intro. to CompSci 4ANDERSON, ALENE T. Comp Info System A

1001Intro. to CompSci 4HENDRIX, JAMES D. Lib Arts - MIS C

1001Intro. to CompSci 4CANNON, FREDDY B.B. Comp Info System B

1002Visual Basic 3ALBERT, PETER A. Comp Info System C

1002Visual Basic 3ALLENSON, SHEILA M. Comp Info System D

1002Visual Basic 3ANDERSON, ALENE T. Comp Info System A

1002Visual Basic 3HENDRIX, JAMES D. Lib Arts - MIS B

1002Visual Basic 3CANNON, FREDDY B.B. Comp Info System B

1003Cisco Networking I 4ALBERT, PETER A. Comp Info System C

1003Cisco Networking I 4ALLENSON, SHEILA M. Comp Info System A

1003Cisco Networking I 4ANDERSON, ALENE T. Comp Info System A

1003Cisco Networking I 4HENDRIX, JAMES D. Lib Arts - MIS D

1004Cisco Networking III3ALBERT, PETER A. Comp Info System B

1004Cisco Networking III3ALLENSON, SHEILA M. Comp Info System C

1004Cisco Networking III3ANDERSON, ALENE T. Comp Info System A

1004Cisco Networking III3CANNON, FREDDY B.B. Comp Info System B

1004Cisco Networking III3HELLER, HELEN H. Lib Arts - MIS A

1004Cisco Networking III3HENDRIX, JAMES D. Lib Arts - MIS F

The individual pieces of information are labeled below using line 1 from above as an example:

CourseID CourseName Credits StudentName Major Grade

1001Intro. to CompSci 4ALBERT, PETER A. Comp Info System A

解决方案

Note: This answer assumes that each column is a definite length (4-digit course ID, 20-character course name, 1-digit credits, 20-character student name, 20-character major, 1-digit grade)

First, you'll want to get a list of every line in the file.

String[] lines = Files.readAllLines(new File("myfile.dat").toPath()).toArray(new String[0]);

Next, process each line by using the substring() method:

for(String line: lines) {

int courseId = Integer.parseInt(line.substring(0, 4));

String studentName = line.substring(4, 24).trim();

// etc...

}

The trim() function removes trailing whitespace.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值