• 数据文件包解析工具类 RandomAccessFile


    public class ReadTextFile {

      public static void main(String[] args) {
        pic2txt();
        parseFrmFile();

        //url2pic();
       }

    private static void parseFrmFile() {
      File floder = new File("D:\shuaka");
      if(floder.isDirectory()){
        File files[] = floder.listFiles(new FilenameFilter() {
          @Override
          public boolean accept(File dir, String name) {
            if(name!=null&&name.endsWith(".frm")){
              return true;
            }else{
              return false;
            }
          }
        });
        RandomAccessFile in = null;
        for (int i = 0; i < files.length; i++) {
          try {
            in = new RandomAccessFile(files[i],"r");
            parseFrmImagesAndData(in);
          } catch (Exception e) {
            e.printStackTrace();
          }finally{
            try {
            in.close();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      }
    }
    }

    private static void parseFrmImagesAndData(RandomAccessFile in) throws IOException{
    String line = null;


    while((line = in.readLine())!=null){
    //System.out.println(line);
    if(line.contains("image-length")){
    int image_length = Integer.parseInt(line.split(":")[1]);
    System.out.println("图片长度:"+image_length);
    //in.seek(in.getFilePointer());
    byte b[] = new byte[(int) in.length()];
    in.read(b);
    File image = new File("D:\vehicle_data\11.jpg");
    FileOutputStream out = new FileOutputStream(image);
    out.write(b);
    out.flush();
    out.close();
    }else if(line.contains("data-length")){
    String data_length = String.valueOf(line.split(":")[1]);

    String struct = in.readLine();
    System.out.println("数据长度:"+data_length);
    File data = new File("D:\vehicle_data\"+System.currentTimeMillis()+".text");
    FileOutputStream out = new FileOutputStream(data);
    out.write(struct.getBytes());
    out.flush();
    out.close();
    }
    }
    }


    private static void pic2txt() {
    try {
    InputStream in = getInputStream("http://190.112.28.72/BK002/XRGSUMFTF/0/2018/05/24/BK002XRGSUMFTF020180524111324972.jpg");
    FileOutputStream out = new FileOutputStream(new File("D:/shuaka/11.txt"));
    System.out.println("image-length"+":"+in.available());
    out.write(("image-length"+":"+in.available()).getBytes());
    out.write(" ".getBytes());
    byte[] b = new byte[1024];
    int len = 0;
    while((len=in.read(b))!=-1){
    out.write(b,0,len);
    }
    out.flush();
    out.close();
    in.close();
    } catch (Exception e) {
    e.printStackTrace();
    }

    }

    public static void url2pic(){
    String url ="http:xx.xx.xx.xx//test/24972.jpg";
    try {
    URL imgurl = new URL(url);
    InputStream in = new DataInputStream(imgurl.openStream());
    FileOutputStream out = new FileOutputStream(new File("D:/shuaka/url2pic.jpg"));
    byte[] b = new byte[1024];
    int len = 0;
    while((len=in.read(b))!=-1){
    out.write(b,0,len);
    }
    out.flush();
    out.close();
    in.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static InputStream getInputStream(String url){
    try {
    URL imgurl = new URL(url);
    return new DataInputStream(imgurl.openStream());
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return null;
    }

    }

  • 相关阅读:
    HTTP的传输编码(Transfer-Encoding:chunked)
    单向链表寻找入环点算法的证明
    Java容器解析系列(17) LruCache详解
    Java容器解析系列(16) android内存优化之SparseArray
    Java容器解析系列(15) HashTable Dictionary & Properties
    Java容器解析系列(14) IdentityHashMap详解
    Swift开发之iOS11下UIToolBar非正常显示问题
    Python爬虫之Scrapy框架爬取XXXFM音频文件
    ARKit文档翻译之ARTrackable协议
    ARKit文档翻译之ARAnchor类
  • 原文地址:https://www.cnblogs.com/yzlsthl/p/9098953.html
Copyright © 2020-2023  润新知