This article collects commonly used file operation methods, including file establishment / checking, deletion, catalog establish / check and delete, take out files in the directory, file properties, and read data by line by line.
directory creation / check and deletion Title> head> <% string path = request.getRealPath (""); path = path "// Sub"; // will be created directory path file d = new file (path); // Establish a File object representative of the SUB directory and get one reference IF (d.exists ()) {// check if the SUB directory exists (); out.println ("Sub directory, deleted"); } else {d.mkdir (); // Established subdirectory Out.println ("Sub directory does not exist, established");}%> body> html>
file properties Obtain Title> head> <% string path = request.getRealPath ("/"); file f = new file (path, "readdata.txt"); if (f.exists ()) { %> <% = f.getName ()%> following properties:
file length is:? <% = f.length ()%> <% = f.isFile () "is a file": "Not a file"%> <% = f.Indirectory ()? "Is a directory": "Not a directory"%> <% = f.canread ()? "Read": "Unreadable Take "%> <% = f.canwrite ()?" Can be written ":" Not writable "%> <% = f.ishidden ()?" Is a hidden file: "Not hidden File "%> File's last modification date is: <% = new date (f.lastmodified ())%> <%} else {f.createNewFile (); // Create one in the current directory a file named ReaData.txt%> <% = f.getName ()%> following properties:
file length is: <% = f.length ()%> <% = f.isFile ( "Yes File": "Not the file"%> <% = f.Indirectory ()? "" Is a directory ":" Not a directory "%> <% = f.canread ()?" Read ":" Do not read "%> <% = f.canwrite ()?" Write ":" Not Write "%> <% = f.ishidden ()?" Hidden file ":" Not hidden file "%> File last modification date is: <% = new date (f.lastmodified ())%> <%}%> body> html > Method for removing files in the directory
Remove the file in the directory - Column Files in the directory title> head> <% string path = request.getRealPath ("/"); file d = new file (path); // Establish the file object file in the current directory. File. List [] = d.listfiles (); // acquire file object array of all files on behalf of the directory Out.println ("" PATH "directory: font> "); for (int i = 0; i .isfile ()) {Out.Println (List .Getname () " " }}} out.println (" " "directory: font> "); for (int i = 0; i .IsDirectory ()) {out.println (List .Getname () " ");}}}%> body> html> judgment Is it blank file?
Judging whether it is blank file title> head> <% string path = request.getRealPath ("/"); out.println (path); fileReader fr = new fileReader (Path //atend.txt") ;// established FileReader object And instantiate to the object generated by the FR // to use the read () method, which can read the next character from the character stream. if (fr.read () == - 1) // determines if the end of the file is read {OUT.PRINT ("There is no data " in atend.txt file ");} else {Out.println (" Atend Data in .txt file ");} fr.close ();%> body> html> Read all file data b>
<% @ page contenttype = "text / html; charSet = GB2312"%> <% @ page import = "java.io. *, java.lang. *"%> Read all file data title> head> <% string path = request.getRealPath ("."); FileReader fr = new fileReader (PATH "/ /READDATA.TXT "); // The key is during the reading process, to determine if the read characters have reached the end of the file, and this character is not a broken line in the file, ie it is determined whether the character value is 13.
INT c = fr.read (); // reads a character // from the file to determine if the file ends are read, while (c! = - 1) {Out.print ((char) c); // Output Data c = fr.read (); // Continue to read data IF from the file (c == 13) {// Judgment whether it is a broken character out.print (" "); // Output Branch Label fr.skip (1); // Skose a character //c=fr.read();// read a character}} fr.close ();%> body> html> a line Read data
file reading title> Head> <% string path = request.getRealPath ("); // get the path of the current directory FileReader fr = new fileReader (Path " //file/inc//t.txt "; Establish a FileReader object, and instantiate to fr bufferedreader br = new bufferedreader (fr); // Establish a BufferedReader object, and instantiate to br string line = br.readline (); // Read a line from the file // judgment Whether the character string read is not empty while (line! = Null) {outputln (line " "); / / Output data read from the file line = br.readline (); / / Continue to read a line data from the file} br.close (); // Close the bufferedReader object fr.close (); // Turn file%> body> html>
Write data into file title> head> <% string path = request.getRealPath ("."); FileWriter FW = New FileWriter (Path "//WriteData.txt" // Established FileWriter object, and instantiate FW // Write string to file fw.write ("Hello!"); Fw.write ("This book is" JSP programming skill "); fw.write (" Please give me a lot! "); Fw.write (" Email: stride@sina.com "); fw.close (); fileReader fr = new fileReader (Path " //writeData.txt "); bufferedReader br = New BufferedReader (fr); // Establish a BufferedReader object, and instance BR string line = br.readline (); // read a line of data out.println (line " "); br.close (); // Close BufferedReader object fr.close ();%> body> html> Write the data branch of the file
Write the data branch of the file Title> head> <% string path = request.getRealPath ("."); FileWriter FW = New FileWriter (Path "//writeData.txt"); bufferedWriter BW = New BufferedWriter (fw); BW .write ("Hello!"); bw.write ("This book is" JSP programming skills "."); bw.newline (); // break BW.WRITE ("Please give me more advice!"); bw. NewLine (); // Broken BW.WRITE ("email: stride@sina.com"); bw.flush (); // update data to file fw.close (); // Turn off file stream Out.println "Write file content: "); fileReader fr = new fileReader (Path "//WriteData.txt"); bufferedreader br = new bufferedReader (fr); string line = br.readline (); // Read a line of data while (line! = Null) {Out.println (line " "); line = br.readline ();} fr.close ();%> body> html> How to add data to files