读两遍

欢马劈雪     最近更新时间:2020-01-02 10:19:05

629
从文本文件中读取两位十六进制序列,并显示其等效的十进制

import java.io.BufferedReader;

import java.io.FileReader;//N o w  J a v a  .   c o m 提供

import java.io.IOException;


public class Main {


  public Main() {

    FileReader file;

    try {

      file = new FileReader("hexfile.txt");

      BufferedReader buff = new BufferedReader(file);


      boolean eof = false;

      while (!eof) {/**from N o w J a v a . c o m - 时代Java**/

        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 + " ");

    
展开阅读全文