2023年12月3日发(作者:)
java解析dat_JAVA中怎么读取DAT文件中的内容匿名用户1级2016-12-23 回答DAT估计是个二进制 或者文本 跟普通读取文件是一样的读取上来 你再对文件格式进行拆分 首先你要了解 它的格式是什么 你可以用NOTEPAD++或者 C32ASM打开 看看public class ReadFromFile {/*** 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。*/public static void readFileByBytes(String fileName) {File file = new File(fileName);InputStream in = null;try {n("以字节为单位读取文件内容,一次读一个字节:");// 一次读一个字节in = new FileInputStream(file);int tempbyte;while ((tempbyte = ()) != -1) {(tempbyte);}();} catch (IOException e) {tackTrace();return;}try {n("以字节为单位读取文件内容,一次读多个字节:");// 一次读多个字节byte[] tempbytes = new byte[100];int byteread = 0;in = new FileInputStream(fileName);ailableBytes(in);// 读入多个字节到字节数组中,byteread为一次读入的字节数while ((byteread = (tempbytes)) != -1) {(tempbytes, 0, byteread);}} catch (Exception e1) {tackTrace();} finally {if (in != null) {try {();} catch (IOException e1) {}}}}/*** 以字符为单位读取文件,常用于读文本,数字等类型的文件*/public static void readFileByChars(String fileName) {File file = new File(fileName);Reader reader = null;try {n("以字符为单位读取文件内容,一次读一个字节:");// 一次读一个字符reader = new InputStreamReader(new FileInputStream(file));int tempchar;while ((tempchar = ()) != -1) {// 对于windows下,rn这两个字符在一起时,表示一个换行。// 但如果这两个字符分开显示时,会换两次行。// 因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。if (((char) tempchar) != 'r') {((char) tempchar);}}();} catch (Exception e) {tackTrace();}try {n("以字符为单位读取文件内容,一次读多个字节:");// 一次读多个字符char[] tempchars = new char[30];int charread = 0;reader = new InputStreamReader(new FileInputStream(fileName));// 读入多个字符到字符数组中,charread为一次读取字符数while ((charread = (tempchars)) != -1) {// 同样屏蔽掉r不显示if ((charread == )&& (tempchars[ - 1] != 'r')) {(tempchars);} else {for (int i = 0; i
if (tempchars[i] == 'r') {continue;} else {(tempchars[i]);}}}}} catch (Exception e1) {tackTrace();} finally {if (reader != null) {try {();} catch (IOException e1) {}}}}/*** 以行为单位读取文件,常用于读面向行的格式化文件*/public static void readFileByLines(String fileName) {File file = new File(fileName);BufferedReader reader = null;try {n("以行为单位读取文件内容,一次读一整行:");reader = new BufferedReader(new FileReader(file));String tempString = null;int line = 1;// 一次读入一行,直到读入null为文件结束while ((tempString = ne()) != null) {// 显示行号n("line " + line + ": " + tempString);line++;}();} catch (IOException e) {tackTrace();} finally {if (reader != null) {try {();} catch (IOException e1) {}}}}/*** 随机读取文件内容*/public static void readFileByRandomAccess(String fileName) {RandomAccessFile randomFile = null;try {n("随机读取一段文件内容:");// 打开一个随机访问文件流,按只读方式randomFile = new RandomAccessFile(fileName, "r");// 文件长度,字节数long fileLength = ();// 读文件的起始位置int beginIndex = (fileLength > 4) ? 4 : 0;// 将读文件的开始位置移到beginIndex位置。(beginIndex);byte[] bytes = new byte[10];int byteread = 0;// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。// 将一次读取的字节数赋给bytereadwhile ((byteread = (bytes)) != -1) {(bytes, 0, byteread);}} catch (IOException e) {tackTrace();} finally {if (randomFile != null) {try {();} catch (IOException e1) {}}}}/*** 显示输入流中还剩的字节数*/private static void showAvailableBytes(InputStream in) {try {n("当前字节输入流中的字节数为:" + ble());} catch (IOException e) {tackTrace();}}public static void main(String[] args) {String fileName = "C:/temp/";leByBytes(fileName);leByChars(fileName);leByLines(fileName);leByRandomAccess(fileName);}}


发布评论