如何将 ^M 分成单独的行?

发布于 2024-09-06 12:03:17 字数 2206 浏览 3 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰沐子 2024-09-13 12:03:18

readLine()< /code>对于行结尾的构成相当特殊,请使用 read() 代替。

readLine() is a rather particular about what constitutes a line ending. Use read() instead.

飘过的浮云 2024-09-13 12:03:18

您可以单行读取整个文件,然后使用 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.

高速公鹿 2024-09-13 12:03:18
line.replace("\r", "\n")
line.replace("\r", "\n")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文