Determine whether the file is in CSV format by reading the file header

Source: Internet
Author: User

Determine whether the file is in CSV format by reading the file header
Determine whether the file is in CSV format by reading the file header

It is unreasonable to read the CSV file header and determine whether the file belongs to the CSV file type. Generally, the file type is determined only by the file suffix, you only need to change the file suffix to identify whether the file is in the correct format and change the executable file suffix. CSV is definitely not feasible if you identify the file type by determining the file suffix, because the file format of exe is definitely not CSV, if you determine the file header in advance, you can identify whether the file is the file type we need and avoid parsing the wrong file. It can also protect the security of the server to some extent.

/** System Abbrev: * system Name: * Component No: * Component Name: * File name: Util. java * Author: Qiuzhenping * Date: 2014-11-30 * Description:
 
  
* // * Updation record 1: * Updation date: 2014-11-30 * Updator: Qiuzhenping * Trace No:
  
   
* Updation No:
   
    
* Updation Content:
    
     
*/Package com. qiuzhping. util; import java. io. FileInputStream ;/***
     
      
* Read the CSV file header to determine whether the file belongs to the CSV file type. Generally, the file type is determined only by the file suffix. * This is unreasonable, you only need to change the file suffix to identify whether the file is in the correct format and change the executable file suffix. CSV * If you identify the file type by determining the file suffix, it will certainly not work because the file format of exe is definitely not CSV, if you determine the * file header in advance, you can locate whether the file is the file type we need and avoid parsing the wrong file. It can also protect the security of the server to some extent. *
      
        ** @ Author Qiuzhenping * @ version [Version NO, 2014-11-30] * @ see [Related classes/methods] * @ since [product/module version] */public class Util {/**
       
         * Bytes to Hex String * convert the byte array to a hexadecimal String *
        
          * @ Author Qiuzhenping * @ param src * @ return [Parameters description] * @ return String [Return type description] * @ exception throws [Exception description] * @ see [Related classes # Related methods # Related properties] */public static String bytes2HexString (byte [] src) {StringBuilder stringBuilder = new StringBuilder (); if (src = null | src. length <= 0) {return null;} for (int I = 0; I <src. length; I ++) {int v = src [I] & 0xFF; String hv = Integer. toHexString (v); if (hv. length () <2) {stringBuilder. append (0);} stringBuilder. append (hv);} System. out. println ("bytes2HexString =" + stringBuilder. toString (). toUpperCase (); return stringBuilder. toString (). toUpperCase ();}/**
         
           * Judge this FileInputStream is csv file * determines whether the file stream header contains the specified information to confirm whether the file is of the correct file type *
          
            * @ Author Qiuzhenping * @ param is * @ return [Parameters description] * @ return boolean [Return type description] * @ exception throws [Exception description] * @ see [Related classes # Related methods # Related properties] */public static boolean judgeIsCSV (FileInputStream is) {try {byte [] B = new byte [4]; is. read (B, 0, B. length); return bytes2HexString (B ). contains ("5B75726C"); // The first 4 bytes in the header of the CSV file} catch (Exception e) {e. printStackTrace ();} return false;} public static void main (String [] args) throws Exception {String src = "C:/dataTemp/urluse .csv "; fileInputStream is = new FileInputStream (src); System. out. println (judgeIsCSV (is); src = "C:/dataTemp/urluse .csv"; is = new FileInputStream (src); System. out. println (judgeIsCSV (is ));}}
          
         
        
       
      
     
    
   
  
 
Reprinted Please note: http://blog.csdn.net/qiuzhping/article/details/41626295

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.