1 Packagetext;2 3 ImportJava.io.*;4 5 //text file Operation class6 Public classfile {7 8 PrivateString FileName;9 Ten PublicString GetFileName () One { A returnFileName; - } - //Construction the Privatefile file; - - Publicfile (String fileName) - { + This. FileName =FileName; - +File =NewFile (fileName); A } at - //Create a file - Public voidCreate ()throwsIOException - { - - in if(!file.exists ())//If the file does not exist - { to file.createnewfile (); +System.out.println ("File creation succeeded! "); - } the Else * { $System.out.println ("File already exists! ");Panax Notoginseng } - } the //Deleting Files + Public voidDelete () A { theFile File =NewFile (fileName); + - if(File.exists ())//If the file exists $ { $ File.delete (); -System.out.println ("Delete succeeded! "); - } the Else - {WuyiSYSTEM.OUT.PRINTLN ("file does not exist!) "); the } - } Wu //Modify Content - Public voidEdit (String stredit)throwsIOException//the modifications are transferred in the form of parameters . About { $ if(File.exists ()) - { -FileWriter FW =NewFileWriter (file); - ABufferedWriter BFW =NewBufferedWriter (FW); + the Bfw.write (stredit); - $ bfw.close (); the } the Else the { theSYSTEM.OUT.PRINTLN ("file does not exist!) "); - } in } the //Append Content the Public voidAddLine (String strLine)throwsIOException//Add new lines, and transfer content About { the if(File.exists ()) the { theString str =readFile (); + -FileWriter FW =NewFileWriter (file); the BayiBufferedWriter BFW =NewBufferedWriter (FW); the the //bfw.newline (); - -Bfw.write (str+ "\ n" +strLine); the the bfw.close (); the } the Else - { theSYSTEM.OUT.PRINTLN ("file does not exist!) "); the } the }94 PublicString ReadFile ()throwsIOException the { theString RTN = ""; the 98 if(File.exists ()) About { -FileReader FR =Newfilereader (file);101 102BufferedReader br =NewBufferedReader (FR);103 104String s = ""; the 106 while((s = br.readline ())! =NULL)107 {108Rtn + = s + "\ n";109 } the br.close ();111 } the Else113 { theSYSTEM.OUT.PRINTLN ("file does not exist!) "); the } the returnRtn;117 }118}
View Code
1019 Classroom Content