如何从Java中的.dat文件中读取一行然后需要分隔? [英] How do I read in a line from a .dat file in Java that then needs to be separated?

查看:660
本文介绍了如何从Java中的.dat文件中读取一行然后需要分隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在TextPad中使用Java读取一个.dat文件。 .dat文件有多行代码,但每行都有不同的信息片段,我需要在主要方法和对象类中使用不同的方法。我如何分离文件中提供的信息,并将单独的部分输入到我的任何类中?



我知道如何从.txt文件读取基本输入,但是它不能使用.dat文件。我不知道如何分离没有逗号的数据,也不允许更改.dat文件中的数据。



查看.dat文件在TextPad中,它显示为标准字符,而不是二进制。我试图阅读的代码如下:

1001Intro。到CompSci 4ALBERT,PETER A.比较信息系统A
1001Intro。到CompSci 4ALLENSON,SHEILA M. Comp Info系统B
1001Intro。到CompSci 4ANDERSON,ALENE T. Comp信息系统A
1001Intro。 CompSci 4HENDRIX,JAMES D. Lib艺术 - MIS C
1001Intro。到CompSci 4CANNON,FREDDY BB Comp信息系统B
1002Visual基本3 ALBERT,PETER A.比较信息系统C
1002Visual基本3ALLENSON,SHEILA M.比较信息系统D
1002Vualual 3ANDERSON,ALENE T Comp Information System A
1002Visual Basic 3HENDRIX,JAMES D. Lib Arts - MIS B
1002Visual Basic 3CANNON,FREDDY BB Comp Info System B
1003Cisco Networking I 4ALBERT,PETER A. Comp Info System C
1003思科网络I 4ALLENSON,SHEILA M.比较信息系统A
1003思科网络I 4ANDERSON,ALENE T. Comp信息系统A
1003思科网络I 4HENDRIX,JAMES D. Lib Arts - MIS D
1004Cisco Networking III3ALBERT,PETER A.比较信息系统B
1004Cisco网络III3ALLENSON,SHEILA M.比较信息系统C
1004Cisco网络III3ANDERSON,ALENE T.比较信息系统A
1004Cisco网络III3CANNON,FREDDY BB Comp Info系统B
100 4Cisco Networking III3HELLER,HELEN H. Lib Arts - MIS A
1004Cisco Networking III3HENDRIX,JAMES D. Lib Arts - MIS F



下面以第一行为例,标注各条信息:

CourseID 课程名称学分 年级



1001 介绍。 to CompSci 4 ALBERT,PETER A. Comp Info System A

> 注意:这个答案假设每列都是一个确定的长度(4位数的课程编号,20个字符的课程名称,1位数学分,20个字符的学生名称,20个字符的专业,1数字等级)



首先,您需要获取文件中每行的列表。



<$ (新的字符串[0]);新的文件(myfile.dat)。toPath())。toArray(new String [0]);

接下来,使用 substring()

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $子串(0,4));
String studentName = line.substring(4,24).trim();
//等等...

code $ $ $ $ $ $ $ $ $ $ $ $ > trim()函数删除结尾的空格。


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.

这篇关于如何从Java中的.dat文件中读取一行然后需要分隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆