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 ");}}