public static List<String> dataInputStream(String filepath) throws IOException { // File file = new File(filepath); // DataInputStream dps = new DataInputStream(new FileInputStream(file)); URL url = new URL(filepath); URLConnection conn = url.openConnection(); InputStream stream = conn.getInputStream(); DataInputStream dps = new DataInputStream(stream); StringBuilder hexData = new StringBuilder(); byte bt = 0; // long fileLength = file.length(); long fileLength = conn.getContentLength(); // long fileLength = url.getFile().length(); System.out.println(fileLength); List<String> fileContent = new ArrayList<String>(); for (int i = 0; i < fileLength; i++) { // 每行只存254个字符 if (i % 127 == 0) { fileContent.add(hexData.toString()); hexData = new StringBuilder(); } bt = dps.readByte(); // 以十六进制的无符号整数形式返回一个字符串表示形式。 String str = Integer.toHexString(bt); if (str.length() == 8) { // 去掉补位的f str = str.substring(6); } if (str.length() == 1) { str = "0" + str; } hexData.append(str.toUpperCase()); } fileContent.add(hexData.toString()); return fileContent; }
来源:oschina
链接:https://my.oschina.net/liudandan/blog/4492176