Read and Write TXT files in Java

Source: Internet
Author: User

We use an actual example to illustrate how Java reads and writes TXT files.

The travel subsidy is calculated as follows:
50 RMB per day within 30 days,
Within 30 days and 60 days, 50 yuan is granted every day for the first 30 days, and 10 yuan is granted every day for the remaining 30 days,
Within 60 days and 90 days, the first 30 days will receive 50 yuan a day, 31 to 60 more than 10 yuan a day, and the remaining 60 days will receive 10 yuan a day,
And so on .....
For example, if you are on a business trip for 28 days, the renewal fee is 28*50;
For 45 days on business trip, the renewal fee is: 30*50 + 15*60
For 67 days on a business trip, the renewal fee is 30*50 + 30*60 + 7*70

Requirements: for the sake of flexibility, daily renewal fee (for example, 50) is required. After the conditions are met, the daily renewal fee (for example, 10) is charged from D: \ test \ config. read from the properties file, config. properties:
Base = 50
Step = 10
Read the information of the person on a business trip from the D: \ test \ src.txt file. The personnel information includes the name of the person on a business trip and the date on which the business trip ends. Each field is distinguished by space, for example:
Zhang San 2010-9-17 2010-10-15
Li Si 2010-9-5 2010-10-30
Wang Wu 2010-9-20 2010-11-2
Based on the above information, calculate the number of travel days for each person, and use the license fee to write the result to D: \ test \ result.txt. the content of result.txt includes the name of the person on which the business trip ends and the number of days on which the business trip ends, for example:
Zhang San 28 1400
Li Si 55 3000
Wang Wu 2010-9-20 2010-11-2 43 2280

The operating source code is attached below

Memberinfo class

Package COM. dengsilinming. read. file;/*** todo comment of memberinfo *** @ author dengsilinming * @ version $ ID: memberinfo. java 10:30:40 $ **/public class memberinfo {// name private string membername; // business trip start date private string startdate; // business trip end date private string enddate; // number of business days private long countday; // The error payable private long totalmoney; Public String getmembername () {return membername;} public void setmembername (string membername) {This. membername = membername;} Public String getstartdate () {return startdate;} public void setstartdate (string startdate) {This. startdate = startdate;} Public String getenddate () {return enddate;} public void setenddate (string enddate) {This. enddate = enddate;} public long getcountday () {return countday;} public void setcountday (long countday) {This. countday = countday;} public long gettotalmoney () {return totalmoney;} public void settotalmoney (long totalmoney) {This. totalmoney = totalmoney ;}}

Operatetxt class

Package COM. dengsilinming. read. file; import Java. io. bufferedinputstream; import Java. io. bufferedreader; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. filereader; import Java. io. filewriter; import Java. io. ioexception; import Java. io. inputstream; import Java. text. dateformat; import Java. text. parseexception; import Java. text. simpledateformat; import Java. Util. arraylist; import Java. util. date; import Java. util. list; import Java. util. properties;/*** todo comment of operatetxt *** @ author dengsilinming * @ version $ ID: operatetxt. java 10:40:40 $ **/public class operatetxt {Private Static final string base_url = "D:/test/config. properties "; Private Static final string input_url =" D:/test/src.txt "; Private Static final string output_url =" D:/test/result.txt ";/*** read config. properties file * @ Param path file path * @ return */Private Static string readbaseinfo (string path) {try {inputstream is = new bufferedinputstream (New fileinputstream (new file (PATH); properties Properties = new properties (); properties. load (is); string base = properties. getproperty ("base"); string step = properties. getproperty ("Step"); Return base + "," + step;} c Atch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} return "";}/*** configure src.txt file, and calculate the travel cost per person * @ Param base * @ Param STAP * @ Param path * @ return memberinfolist */Private Static list <memberinfo> parsesrctxt (long base, long STAP, string path) {try {list <memberinfo> memberinfolist = new arraylist <memberinfo> (); string line; file srcfile = new fil E (PATH); filereader = new filereader (srcfile); bufferedreader = new bufferedreader (filereader); While (line = bufferedreader. Readline ())! = NULL) {string [] memberarr = line. split (""); memberinfo member = new memberinfo (); member. setmembername (memberarr [0]); member. setstartdate (memberarr [1]); member. setenddate (memberarr [2]); dateformat df = new simpledateformat ("yyyy-mm-dd"); Date startdate = DF. parse (memberarr [1]); Date enddate = DF. parse (memberarr [2]); long day = (enddate. gettime ()-startdate. gettime ()/(60*60*24*1000); member. se Ttotalmoney (calculateplus (base, STAP, day); member. setcountday (day); memberinfolist. add (member);} filereader. close (); bufferedreader. close (); Return memberinfolist;} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} catch (parseexception e) {e. printstacktrace ();} return NULL;}/*** calculation fee * @ Param base daily fee * @ Param STAP grade increase * @ Param day Travel days * @ return */Private Static long calculate (long base, long STAP, long day) {int time = (INT) (day/30 ); // The number of cycles Long Count = 0; // total charge payable for (INT I = 0; I <= time; I ++) {if (I = 0) {If (day <= 30) Count = base * day; else count = base * 30;} else {If (day-30 * I <= 30) {COUNT = count + (base + STAP * I) * (day-30 * I);} else count = count + (base + STAP * I) * 30 ;}} return count ;}/ * ** Write content to the result.txt file * @ Param memberlist * @ Param outputurl */Private Static void writetofile (list <memberinfo> memberlist, string outputurl) {try {// write filewriter = new filewriter (new file (outputurl) to the TXT file; For (memberinfo Member: memberlist) {string writetext = member. getmembername () + "" + member. getstartdate () + "" + member. getenddate () + "" + member. getcountday () + "" + Member. gettotalmoney () + "\ r \ n"; filewriter. write (writetext);} filewriter. close ();} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {string [] baseandsteparr = readbaseinfo (base_url ). split (","); // read the configuration file content list <memberinfo> memberlist = parsesrctxt (Long. parselong (baseandsteparr [0]), long. parselong (baseandsteparr [1]), input_url); // parse the TXT file content if (memberlis T! = NULL & memberlist. Size ()> 0) {writetofile (memberlist, output_url); // create a TXT file and write content to it }}}

Config. Properties

Src.txt

Result.txt

Note: For Java's method of reading the properties of the configuration file, see Java's method of reading properties.

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.