This article summarizes common file operations in JSP. Share it for your reference. The details are as follows:
File operations in JSP: FILE class
String path=("/");// Pass the parameter "/" to return to the root directory of the web applicationString tmp_path=path+"tmp"; File f1=new File(tmp_path);//Create the FILE class and specify the path as tmp_path();//Create a directoryFile f2=new File(tmp_path,"");//Create FILE class, specify the path as //tmp_path+""();//Create the file specified by f2File f3=new File(tmp_path,""); (); File f4=new File(tmp_path,""); ();
in:
The length() method of the File object can calculate the file size
The isFile() method can determine whether it is a file
The isDirectory() method can determine whether it is a folder
getName() can get the name of the file folder
CanRead() is readable
CanWrite() is writable
Is it Hidden() hidden?
lastModified() last modified date, returns an object of the Date class
Reading of files
Example 1:
String path=("/"); File fp=new File(path,"");//Define a fileFileInputStream fistream=new FileInputStream(fp);//Define a file input stream to bind a filebyte buf[]=new byte[10000]; int bytesum=(buf,0,10000)//Write the byte file into the buf array and return the number of bytes writtenString str_file=new String(buf,0,bytesum); (str_file); ();
Example 2:
String path=("/"); File fp=new File(path,""); FileReader freader=new FileReader(fp): BufferedReader bfdreader=new BufferedReader(freader); String str_line=(); while(str_line!=null){ (str_line); ("<br>"); str_line=(); } (); ();
File writing:
Example 1:
String path=("/"); File fp=new File(path,""); FileWriter fwriter=new FileWriter(fp); ("GBK");//Set character encodingString str_file=("textarea"); (str_file); ();
Example 2:
String path=("/"); File fp=new FIle(path,""); FileWriter fwriter=new FIleWriter(fp); BufferedWriter bfwriter=new BufferedWriter(fwriter); ("GBK"); String str_file=("textarea"); (str_file,0,str_file.length()); (); ();
I hope this article will be helpful to everyone's JSP programming.