views:

54

answers:

3

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.

+1  A: 

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.

tlayton
A: 
line.replace("\r", "\n")
Ignacio Vazquez-Abrams
A: 

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

trashgod