File read/write operations

Source: Internet
Author: User
Two methods are written:
 
Write the string content to the TXT file and use the linefeed system. getproperty ("line. separator"); // line feed mark
 
One is to read the number of lines in the TXT file.
Package COM. foresee. JK; import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. file; import Java. io. filereader; import Java. io. filewriter; public class fileoperater {/*** number of lines reading TXT files * @ Param filepath * @ return */public static int getfiletxtlinecount (string filepath) {try {string S = ""; int linecount = 0; file F = new file (filepath); If (! F. exists () {system. err. print ("file does not exist"); Return 0;} bufferedreader input = new bufferedreader (New filereader (f); While (S = input. readline ())! = NULL) {linecount ++;} input. close (); Return linecount;} catch (exception e) {e. printstacktrace ();} return 0;}/*** write content into the TXT file * @ Param filepath * @ Param content */public static void writetxt (string filepath, string content) {string S = new string (); string S1 = new string (); string CRLF = system. getproperty ("line. separator "); // line feed mark try {file F = new file (filepath); If (F. exists () {// system. out. print ("file exists ");} Else {system. out. print ("file does not exist"); F. createnewfile (); // creation if it does not exist} bufferedreader input = new bufferedreader (New filereader (f);/* While (S = input. readline ())! = NULL) {S1 + = S + CRLF;} */comment out the content without appending. // system. out. println (S1); input. close (); S1 + = CRLF + content; bufferedwriter output = new bufferedwriter (New filewriter (f); output. write (S1); output. close ();} catch (exception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {fileoperater. writetxt ("d :\\ XH \\ log.txt", "lsdfsdfl ");}}

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.