Read two-digit hexadecimal sequences from a text file and displays their decimal equivalents - Java File Path IO

Java examples for File Path IO:Text File

Description

Read two-digit hexadecimal sequences from a text file and displays their decimal equivalents

Demo Code

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {

  public Main() {
    FileReader file;/*from   w w  w .j a  v  a 2 s. c  om*/
    try {
      file = new FileReader("hexfile.txt");
      BufferedReader buff = new BufferedReader(file);

      boolean eof = false;
      while (!eof) {
        String line = buff.readLine();
        if (line == null) {
          eof = true;
        } else {
          readLine(line);
        }
      }
      buff.close();
    } catch (IOException ex) {
      System.out.println("IO error " + ex.getMessage());
    }
  }

  private void readLine(String code) {
    try {
      for (int j = 0; j + 1 < code.length(); j += 2) {
        String sub = code.substring(j, j + 2);
        int num = Integer.parseInt(sub, 16);
        if (num == 255) {
          return;
        }
        System.out.print(num + " ");
      }
    } finally {
      System.out.println("**");
    }
  }

  public static void main(String[] arguments) {
    new Main();
  }
}

Result


Related Tutorials