JSP file operations (new, delete, read, write) __js

Source: Internet
Author: User
Tags mkdir readline
There are many places in the java/jsp to use the operation of the file, file operation is very simple, the following list of some common methods of file operation
JSP file operations commonly used methods:
MkDir () method is used to create a folder
Delete () method to remove folders and files
The exists () method is to determine whether a folder or file exists
The CreateNewFile () method is to create a file
The Listfiles () method is to get the files under the folder
The Read () method reads the file
The ReadLine () method is to read the file in rows
The write () method writes a character or string to a file

Here's a sample description of these methods
1. New and deleted directory
MkDir () Method:
<%@ page import= "java.io.*"%>
<%
String Path = Request.getrealpath ("/file/");
String subpath = path+ "Mulu";
File ml = new file (subpath);
if (Ml.exists ())
{
Ml.delete ();
Out.println (path + "folder Mulu has been deleted!");
}
Else
{
Ml.mkdir ();
Out.println (path + "folder Mulu Create success!");
}
%>
2. New and deleted files
CreateNewFile () Method:
<%@ page import= "java.io.*"%>
<%
String Path = Request.getrealpath ("/file/");
File FileName = new file (path, "File.txt");
if (Filename.exists ())
{
Filename.delete ();
Out.println (path + "file File.txt file has been deleted!");
}
Else
{
Filename.createnewfile ();
Out.println (path + "file File.txt Create success!");
}
%>


3. Get the files under the folder
Listfiles () Method:
<%@ page import= "java.io.*"%>
<%
String Path = Request.getrealpath ("/file/");
File FL = new file (path);
File list[] = Fl.listfiles ();
Out.println ("file list:<br>");
for (int i=0 i < list.length; i++)
{
Out.println (List[i].getname () + "<br>");
}
%>

4. Read the contents of the file
Read () Method:
<%@ page import= "java.io.*"%>
<%
String Path = Request.getrealpath ("/file/");
FileReader FR = new FileReader (path + "File.txt");
Single byte read
int c = Fr.read ();
while (c!=-1)//Determine whether the end of the file has been read
{
Out.print ((char) c); Data read from the output
c = Fr.read (); Reading data from a file
if (c = = 13)//Determine if the break byte
{
Out.print ("<BR>"); Output Branch Label
Fr.skip (1); Skip One byte
c = Fr.read (); Read one byte
}
}
Fr.close ();
%>
You can also read a file by reading a row
ReadLine () Method:
<%
BufferedReader br = new BufferedReader (FR);
String BRL = Bufferedread.readline ();
while (Brl!=null)
{
Out.println (brl+ "<br>");
BRL = Bufferedread.readline ();
}
Brl.close ();
Fr.close ();
%>

5. Write to File
Write () Method:
<%@ page import= "java.io.*"%>
<%
String Path = Request.getrealpath ("/file/");
FileWriter FW = new FileWriter (path + "File.txt");
Fw.write ("hello!");
Fw.write ("I wish you all a happy learning jsp." ");
Fw.write ("I hope this article can give you an understanding of the ' JSP file Operation ' Help.") ");
Fw.close ();
%>
<a href= "Http://localhost:8080/k/file/File.txt" > View files </a>
Just click on the "View file" connection to see the string you just wrote!
Related Article

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.