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

Source: Internet
Author: User

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

By reading the CSV file header, to determine whether the file is a CSV file type, in general, only through the file suffix to determine the type of the file, it is unreasonable, as long as the file suffix can not identify the file is not exactly the correct file format, the executable file suffix changed. CSV if it is by judging the file suffix to identify the file type, this certainly does not work, because the EXE file format is definitely not a CSV format, if the first to determine the file header so that you can locate this file is not the type of file we need, to avoid parsing the wrong file. It is also possible to protect the server in some way.

/* * System abbrev: * System Name: * Component No: * Component Name: * File Name:Util.java * Author:qiuz        henping * date:2014-11-30 * Description: <description> *//* updation record 1: * updation Date : 2014-11-30 * updator:qiuzhenping * Trace No: <trace no> * updation No: <updation no> * Updati On Content: <list all contents of updation and All methods updated.> */package Com.qiuzhping.util;import java.io.Fi leinputstream;/** * <description functions in a word> * by reading the CSV file header to determine whether the file is a CSV file type, generally only by the file suffix to determine the type of the file, * this is unreasonable, as long as you change the file suffix can not identify whether the file is the correct file format, the executable file suffix is changed. CSV * If it is by judging the file suffix to identify the file type, this certainly does not work, because the EXE file format is definitely not CSV format, if you judge in advance * out of this file header so that you can locate this file is not the type of file we need, to avoid parsing the wrong file. It can also, to some extent, protect the security of the server.  * <detail description> * * @author qiuzhenping * @version [version NO, 2014-11-30] * @see [related classes/methods] * @since [Product/module version] */public class Util {/** <description functions In a word> * Bytes to Hex String * Convert byte array to 16 binary string * <detail description> * @author qiuzhenping * @param src * @return [Parameters description] * @return String [return type description] * @exception throws [Exception] [Exception des Cription] * @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 ();} /** <description Functions in a word> * Judge this fileinputstream is CSV file * Determines whether the file stream header contains the specified information, thus confirming that the file is not the correct class of files Type * <detail description> * @author qiuzhenping * @param is * @return [Parameters description] *@return Boolean [Return type description] * @exception throws [Exception] [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"),//csv file header's first 4 bytes} catch (Exception e) { E.printstacktrace ();} return false;} public static void Main (string[] args) throws Exception {String src = "c:/datatemp/url using. csv"; FileInputStream is = new FileInputStream (SRC); System.out.println (Judgeiscsv (IS)); src = "c:/datatemp/url using. csv"; is = new FileInputStream (SRC); System.out.println (Judgeiscsv (IS));}}
Reprint Please specify: http://blog.csdn.net/qiuzhping/article/details/41626295

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

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.