Reads text data by number of rows and returns a string array or JSON array

Source: Internet
Author: User
Tags readfile

/**
* Read file, return string
*
* @param path
* @return
*/
public static list<string> ReadFile (String path) {
list<string> dataList = new arraylist<string> ();
File File = new file (path);
BufferedReader reader = null;
try {
Read the contents of the file in the behavior unit, read a whole line one at a time;
reader = new BufferedReader (new FileReader (file));
String tempstring = null;
int line = 1;
Read one line at a time until NULL is read to the end of the file
while ((tempstring = Reader.readline ()) = null) {
Show line Numbers
System.out.println ("line" +line+ ":" +tempstring);
Datalist.add (tempstring);
line++;
}
Reader.close ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (reader! = null) {
try {
Reader.close ();
} catch (IOException E1) {
}
}
}
return dataList;

}


/**
* Convert data into JSON file
*
* @param path
* @param array
*/
public static void Writejsonfile (String path, Jsonarray array) {
File File = new file (path);
FileOutputStream fout = null;
try {
Fout = new FileOutputStream (file);
for (int j = 0; J < Array.size (); j + +) {
Fout.write (Array.getjsonobject (j). ToString (). GetBytes ());
Fout.write ("\ n". GetBytes ());
}
Fout.close ();
} catch (Exception E1) {
E1.printstacktrace ();
}finally{
if (fout!=null) {
try {
Fout.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}



public static void Madeozonedatatojsonfile (String ozonedatafilepath, String jsonlistpath) {
J.add (1);
list<string> list = ReadFile (Ozonedatafilepath);
Jsonarray jsonlist = new Jsonarray ();
for (String str:list) {
String a = str;
String concentration= "", estate= "", date= "", time= "";
String arg[] = Str.split ("\\s+");
CONCENTRATION=ARG[0];
DATE=ARG[2];
ESTATE=ARG[1];
TIME=ARG[3];
System.out.println (Arg[0]);
Jsonobject Jo = new Jsonobject ();


Jo.put ("concentration", concentration);
Jo.put ("date", date);
Jo.put ("Time", time);
Jo.put ("Estate", estate);

Jsonlist.add (Jo);
System.out.println (str);
}

Writejsonfile (jsonlistpath,jsonlist);

}



Reads text data by number of rows and returns a string array or JSON array

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.