如何将 ^M 分成单独的行?
我是java编程新手。我编写了一个程序来交互汽车中的数据。我在使用原始数据而不是熟数据时遇到了问题。
public static void readFile(String fromFile) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(fromFile));
//... Loop as long as there are input lines.
String line = null;
while ((line=reader.readLine()) != null ) {
if (line.length() >= 2) {
Status.LineToken = new StringTokenizer (line);
Status.CheckToken = Status.LineToken.nextToken();
Log.level1(line);
if ( Status.CheckToken.contains("41")) {
Mode01.modeSwitch();
} else if (Status.CheckToken.contains("42")) {
Mode02.modeSwitch();
} else if ( Status.CheckToken.contains("43")) {
Mode03.modeSwitch();
} else if (Status.CheckToken.contains("44")) {
Mode04.modeSwitch();
} else if ( Status.CheckToken.contains("45")) {
Mode05.modeSwitch();
} else if ( Status.CheckToken.contains("46")) {
//is there a mode 6?
} else if ( Status.CheckToken.contains("47")) {
//is there a mode 7?
} else if ( Status.CheckToken.contains("48")) {
// mode 8 is for control of a vehicle. Unknown params at this time.
} else if ( Status.CheckToken.contains("49")) {
Mode09.modeSwitch();
} else if (line.endsWith(">")) {
//Send data to OBD unit
} else if (Status.LineToken != null) {
//blank line catch
}
}
}
reader.close(); // Close to unlock.
newDataIsAvailable = true;
}
当我使用像这样的输入数据作为“FileReader(fromFile)”时,上面的代码效果很好:
>0100
41 00 BE 1F B8 10
>0101
41 01 00 07 65 00
但是我在转换原始代码时遇到问题:
^M^M>0100^M41 00 BE 1F B8 10 ^M^M>0101^M41 01 00 07 65 00
所以基本上,问题是我需要在 reader.readline 上设置一个行分隔符在 ^M 处创建新的数据行。我不太确定该怎么做。
I am new to java programming. I have a program which I wrote to interperate data from my car. I am running into problems with using raw data instead of cooked data.
public static void readFile(String fromFile) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(fromFile));
//... Loop as long as there are input lines.
String line = null;
while ((line=reader.readLine()) != null ) {
if (line.length() >= 2) {
Status.LineToken = new StringTokenizer (line);
Status.CheckToken = Status.LineToken.nextToken();
Log.level1(line);
if ( Status.CheckToken.contains("41")) {
Mode01.modeSwitch();
} else if (Status.CheckToken.contains("42")) {
Mode02.modeSwitch();
} else if ( Status.CheckToken.contains("43")) {
Mode03.modeSwitch();
} else if (Status.CheckToken.contains("44")) {
Mode04.modeSwitch();
} else if ( Status.CheckToken.contains("45")) {
Mode05.modeSwitch();
} else if ( Status.CheckToken.contains("46")) {
//is there a mode 6?
} else if ( Status.CheckToken.contains("47")) {
//is there a mode 7?
} else if ( Status.CheckToken.contains("48")) {
// mode 8 is for control of a vehicle. Unknown params at this time.
} else if ( Status.CheckToken.contains("49")) {
Mode09.modeSwitch();
} else if (line.endsWith(">")) {
//Send data to OBD unit
} else if (Status.LineToken != null) {
//blank line catch
}
}
}
reader.close(); // Close to unlock.
newDataIsAvailable = true;
}
The above code works great when I use input data like this as "FileReader(fromFile)":
>0100
41 00 BE 1F B8 10
>0101
41 01 00 07 65 00
But I'm having problems converting the raw code:
^M^M>0100^M41 00 BE 1F B8 10 ^M^M>0101^M41 01 00 07 65 00
So basically, the problem is that I need a line delimiter on the reader.readline set to create a new line of data at ^M. I'm not really sure how to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
readLine()< /code>
对于行结尾的构成相当特殊,请使用
read()
代替。readLine()
is a rather particular about what constitutes a line ending. Useread()
instead.您可以单行读取整个文件,然后使用 String.split("^M") 将其分割以创建一个字符串数组,每个字符串代表一个“行”,然后分别处理该数组的每个元素。
You can read the whole file a single line, then split it up using String.split("^M") to create a array of Strings, each representing a "line," then process each element of the array separately.