Implementation of the "lris.commons" File Reader Filecontentgetter

Source: Internet
Author: User

Filecontentgetter is a class used in the original xqq.commons for file reading, which integrates some common methods for reading table files or text files.

Some of the methods used in the DRP project, which were first applied to the work of the Unit, also use the tools in Drp.commons.util. After that, xuqiaoqiao.cc projects, SS18 projects, Lina projects, and graduation theses are used extensively in Apocalypto projects.

The code is now refactored.

Filecontentgetter Scenario: Read a text file (txt,html, etc.) or a tabular file (CSV file), save the content as String,list<string>, or list<map< string,string>> (Tabular file)

Main methods:

(1) String filecontentgetter.tostring (file file);//read As String

(2) list<string> filecontentgetter.tostringlist (file file);//read as String list

(3) list<map<string,string>> filecontentgetter.tostringmaplist (file file);//read as tabular data

Implementation of method (1)
public static string toString (file file, string charset, long Max_value) {try (BufferedReader BufferedReader = new Buffe    Redreader (New FileReader (file, Charset.forname (Charset))) {StringBuilder ret = new StringBuilder ();    Bufferedreader.lines (). Parallel (). Limit (Max_value). ForEach (Ret::append);  return ret.tostring ();  } catch (IOException e) {e.printstacktrace (); } return null;

Where parameter file is specified, CharSet labels the character set used for parsing, and Max_value indicates the maximum number of read rows.

In the IO block, the BufferedReader class is used for reading, and its definition is written to the parentheses behind the try, thus omitting the step of closing the stream and simplifying the code.

In the content of this block, for multi-line files, using the form of StringBuilder stitching, using the Bufferreader lines method, the IO is converted into a more operational stream, and the limit method of the stream to set the maximum number of read rows, The function consumer, which is added to RET by Ret::append, is passed into the processing of the stream, enabling the content to be appended to the RET.

Implementation of method (2)

  

public static list<string> tostringlist (file file, String CharSet, long Max_value) {    try (BufferedReader buffer Edreader = new BufferedReader (new FileReader (file, Charset.forname (Charset))) {return Bufferedreader.lines (). Paralle  L (). Limit (Max_value). Collect (Collectors.tolist ());  } catch (IOException e) {e.printstacktrace (); } return null;

Method 2 is similar to Method 1, but the operation of StringBuilder is omitted, because stream can be converted directly to list and transformed directly by Collect method.

Implementation of method (3)

Method 3 is used to read the table file, in general, for a single data, in the form of map storage, more convenient for the operation of the data, that is, each data can be accessed according to its directory name. There is a problem here, some files are not directories, or some files, in the final data operation, can be directly ranked access, compared to search directory name access, it is more convenient.

That is, method 3 needs to implement two methods, one can read the first row represents the directory file (Withhead), and the second can be automatically generated directory (Autohead).

Withhead
public static list<map<string, string>> tostringmaplist_withhead (file file, string charset, string separate , long Max_value) {        try (bufferedreader BufferedReader = new BufferedReader (new FileReader (file, Charset.forname ( CharSet))) {            string[] head = Bufferedreader.readline (). split (separate);            Return Bufferedreader.lines (). Parallel (). Limit (Max_value). Map (A1, {                map<string, string> tempmap = new Hashmap<> ();                string[] Stringarraytemp = a1.split (separate);                Intstream.range (0, Stringarraytemp.length). ForEach (A2-Tempmap.put (Head[a2], stringarraytemp[a2]));                return tempmap;            }). Collect (Collectors.tolist ());        } catch (IOException e) {            e.printstacktrace ();        }        return null;    }

After the stream is opened, a row is read, and after the partition is separate by the specified delimiter, staging is used to represent the list of directories.

Then read all the remaining rows, perform a split operation on each line, find the rank of the content in the directory list to look for the corresponding directory, and deposit in the current map, and eventually collect all the map as a list, as the return value

Autohead
public static list<map<string, string>> tostringmaplist_autohead (file file, string charset, string separate , long Max_value) {        try (bufferedreader BufferedReader = new BufferedReader (new FileReader (file, Charset.forname ( CharSet)) {            return Stream.concat (Stream.of (Firstline), Bufferedreader.lines (). Parallel (). Limit (Max_value)). Map (A1, {                map<string, string> tempmap = new hashmap<> ();                string[] Stringarraytemp = a1.split (separate);                Intstream.range (0, Math.min (head.length, Stringarraytemp.length)). ForEach (A2-Tempmap.put (heads_of_auto_name[ A2], stringarraytemp[a2]);                return tempmap;            }). Collect (Collectors.tolist ());        } catch (IOException e) {            e.printstacktrace ();        }        return null;    }

Additional Custom Directory list required

  private static final string[] Heads_of_auto_name =            {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "O", " P "," Q "," R "," S "," T "," U "," V "," W "," X "," Y "," Z "," AA "," AB "," AC "," AD "," AE "," AF "," AG "," AH "," AI "," AJ "," AK "," A L "," AM "," an "," AO "," APS "," AQ "," AR "," as "," at "," AU "};

It's easier to operate, instead of having to read a line first, and get the corresponding directory directly from the directory list.

The above two methods have a repeating part, which can be extracted

  

 public static list<map<string, string>> tostringmaplist_withhead (file file, string charset, string separate , String Nullplace, long Max_value) {try (BufferedReader BufferedReader = new BufferedReader (new FileReader (file,            Charset.forname (Charset))) {string[] head = Bufferedreader.readline (). split (separate);        Return Getmaps (Separate, Max_value, BufferedReader, head);        } catch (IOException e) {e.printstacktrace ();    } return null; } public static list<map<string, string>> tostringmaplist_autohead (file file, string charset, String Separat  E, String nullplace, long Max_value) {try (BufferedReader BufferedReader = new BufferedReader (new FileReader (file,        Charset.forname (Charset))) {return getmaps (separate, Max_value, BufferedReader, heads_of_auto_name);        } catch (IOException e) {e.printstacktrace ();    } return null; } private static List<map<striNg, string>> getmaps (String separate, long max_value, BufferedReader BufferedReader, string[] headsofautoname) { Return Bufferedreader.lines (). Parallel (). Limit (Max_value). Map (A1, {map<string, string> Tempmap            = new Hashmap<> ();            string[] Stringarraytemp = a1.split (separate); Intstream.range (0, Stringarraytemp.length). ForEach (A2-Tempmap.put (Headsofautoname[a2], STRINGARRAYTEMP[A2]))            ;        return tempmap;    }). Collect (Collectors.tolist ()); }

  

Implementation of the "lris.commons" File Reader Filecontentgetter

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.