How Java reads binary files

Source: Internet
Author: User

  1. Public static void ReadFile (String fileName) {
  2. File File = new file (FileName);
  3. if (file.exists ()) {
  4. try {
  5. FileInputStream in = new FileInputStream (file);
  6. DataInputStream dis=New DataInputStream (in);
  7. byte[] itembuf = new byte[20];
  8. //Market code
  9. Dis.read (Itembuf, 0, 8);
  10. String Marketid =new String (Itembuf,0,8);
  11. //Market name
  12. Dis.read (Itembuf, 0, ); After the Read method reads a certain length, the data being read is removed from the stream, so the next read still starts at 0
  13. String marketname =new String (Itembuf,0,20);
  14. //Previous trading day date
  15. Dis.read (Itembuf, 0, 8);
  16. String lasttradingday = new String (Itembuf,0,8);
  17. //Current trading day Date
  18. Dis.read (Itembuf, 0, 8);
  19. String curtradingday = new String (Itembuf,0,8);
  20. //Trading status
  21. Dis.read (Itembuf, 0, 1);
  22. String marketstatus = new String (Itembuf,0,1);
  23. //Number of trading hours
  24. Short tradeperiodnum = Dis.readshort ();
  25. System.out.println ("Market code:" + Marketid);
  26. System.out.println ("Market Name:" + marketname);
  27. System.out.println ("Last trading day Date:" + Lasttradingday);
  28. System.out.println ("date of the current trading day:" + curtradingday);
  29. System.out.println ("date of the current trading day:" + curtradingday);
  30. SYSTEM.OUT.PRINTLN ("Trading Status:" + Marketstatus);
  31. System.out.println ("Number of trading Hours:" + tradeperiodnum);
  32. } catch (IOException e) {
  33. //TODO auto-generated catch block
  34. E.printstacktrace ();
  35. }finally{
  36. //close
  37. }
  38. }
  39. }

/**  * 随机读取文件内容  */ public static void readFileByRandomAccess(String fileName) {  RandomAccessFile randomFile = null;  try {   System.out.println("随机读取一段文件内容:");   // 打开一个随机访问文件流,按只读方式   randomFile = new RandomAccessFile(fileName, "r");   // 文件长度,字节数   long fileLength = randomFile.length();   // 读文件的起始位置   int beginIndex = (fileLength > 4) ? 4 : 0;   // 将读文件的开始位置移到beginIndex位置。   randomFile.seek(beginIndex);   byte[] bytes = new byte[10];   int byteread = 0;   // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。   // 将一次读取的字节数赋给byteread   while ((byteread = randomFile.read(bytes)) != -1) {    System.out.write(bytes, 0, byteread);   }  } catch (IOException e) {   e.printStackTrace();  } finally {   if (randomFile != null) {    try {     randomFile.close();    } catch (IOException e1) {    }   }  } }

How Java reads binary files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.