Java implementation reads file contents (different types)

Source: Internet
Author: User

In some projects a large amount of data is often required to be read from a file, such as an XML file, a TXT file, a CSV file

1. Read the local XML file, you need to pay attention to the corresponding path

//reads the XML file, xmlfile the path to read the fileDocumentbuilderfactory factory =documentbuilderfactory.newinstance ();D ocumentbuilder builder=Factory.newdocumentbuilder ();D ocument Document=Builder.parse (xmlfile); NodeList NodeList= document.getElementsByTagName (Thistag);//node collection for the specified label (THISTAG) for(ITN i =0;i<nodelist.getlength (); i++){      //cycle through the information for each nodeNode node =Nodelist.item (i); NamedNodeMap attributes=node.getattributes ();  for(intj = 0; J < Attributes.getlength (); J + +) {Node attribute=Attributes.item (j); System.out.println (Attribute.getnodename ()+ ":"                          +Attribute.getnodevalue ()); }}

Note: the getElementsByTagName () method is only a method that belongs to document and element

Therefore, when you find the corresponding node for a certain nodes, you need to first cast to element

Element nodetoelement == nodetoelement.getelementsbytagname (Thistag); // Thistag for the specified label

2. Read TXT file

The general data store is a key-value pair in the way the file is recorded, the developer is mostly based on the known key, from the file to get the corresponding value.

For example, the contents of Config.txt are:

Name=jack

Sex=boy

To read the contents of the file from a Java program

New // relative paths are used here String Config_file_fullpath =new= config.get ("name"); // name is Jack
Processing of acquired data
//...

3. Read the. csv file

CSV files are generally tabular, multi-row and multi-column data, columns corresponding to the corresponding different attributes, the Java implementation of row-by-line reading the value of each column cell.

Need to use jar package (Javacsv.jar)

Csvreader Csv_reader =NULL;Chardelimiter = ', ';Try{Csv_reader=NewCsvreader (Paraminfofile, delimiter);//paraminfofile csv file path for readCsv_reader.setescapemode (csvreader.escape_mode_doubled);      Csv_reader.readheaders (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }         Try {                     while(Csv_reader.readrecord ()) {String str= Csv_reader.get (0); if(Str.startswith ("#")){                            Continue; } String Paramtype= Csv_reader.get (0);//The first column of the row data String Class1= Csv_reader.get (1);//The second column of data for the row, and so on String variable= Csv_reader.get (2);                    }                } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }

Java implementation reads file contents (different types)

Related Article

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.