Text files on a pure JSP operation server [1]

Source: Internet
Author: User
Tags date readable
js| Server

This article in the JSP program is based on the function of their own set up a separate program, we want to use what function, their combination of it.

Establishment/inspection and deletion of documents

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> the establishment, inspection and deletion of documents </title>
<body>
<%
String Path=request.getrealpath ("");
OUT.PRINTLN (path);
File F=new file (path, "File.txt");
Out.println (f);
Out.println (F.exists ());

if (f.exists ()) {//check if File.txt exists
F.delete ()//delete File.txt file
Out.println (path + "\\File.txt exists, deleted.) ");
}else{
F.createnewfile ()//Create a file named File.txt in the current directory
Out.println (path + "\\File.txt" does not exist, has been established. ");//The directory path where the output is currently located
}
%>

Establishment/inspection and deletion of catalogs

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> Directory Establishment/inspection and deletion </title>
<body>
<%
String Path=request.getrealpath ("");
Path=path + "\\Sub";//the directory path to be established
File D=new file (path);//Create a File object that represents the sub directory and get a reference to it
if (d.exists ()) {//Check whether the Sub directory exists
D.delete ();
OUT.PRINTLN ("Sub directory exists, deleted");
}else{
D.mkdir ();//Build Sub Directory
OUT.PRINTLN ("Sub directory not present, established");
}
%>
</body>

How to work with virtual directories in a JSP

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
How to work with virtual directories in <title>jsp </title>
<body>
Get the disk path for the virtual directory <br>
The location of the Web site home directory is <font color= #ff0000 ><%=request.getrealpath ("/")%></font><br>
The directory location where the JSP Web page resides <font color= #ff0000 ><%=request.getrealpath ("./")%></font><br>
The location of a directory on the directory where the JSP Web page resides <font color= #ff0000 ><%=request.getrealpath (". /")%></font><br>
</body>

Acquisition of file attributes
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.util.date,java.io.*"%>
<title> the acquisition of file properties </title>
<body>
<%
String Path=request.getrealpath ("/");
File F=new file (path, "ReadData.txt");
if (f.exists ()) {
%>
The properties of the <%=f.getname ()%> are as follows:<br><br>
File length: <%=f.length ()%>
<%=f.isfile ()? " Is file ": Not File"%><br>
<%=f.isdirectory ()? " is directory ": Not directory"%><br>
<%=f.canread ()? " Readable ": Unreadable"%><br>
<%=f.canwrite ()? " Writable ": Not writable"%><br>
<%=f.ishidden ()? " is hidden file ": Not hidden file"%><br>
The last modified date for the file is: <%=new date (f.lastmodified ())%><br>
<%
}else{
F.createnewfile ()//Create a file named ReaData.txt in the current directory
%>
The properties of the <%=f.getname ()%> are as follows:<br><br>
File length: <%=f.length ()%>
<%=f.isfile ()? " Is file ": Not File"%><br>
<%=f.isdirectory ()? " is directory ": Not directory"%><br>
<%=f.canread ()? " Readable ": Unreadable"%><br>
<%=f.canwrite ()? " Writable ": Not writable"%><br>
<%=f.ishidden ()? " is hidden file ": Not hidden file"%><br>
The last modified date for the file is: <%=new date (f.lastmodified ())%><br>
<%
}
%>
</body>

Ways to remove files in a directory

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> How to remove files from the directory--list files in the directory </title>
<body>
<%
String Path=request.getrealpath ("/");
File D=new files (path);//Create a FileName object in the current directory
File List[]=d.listfiles ()//To get an array of file objects representing all the files in the directory
Out.println ("<font color= #ff0000 >" + path + "file:</font><br> under directory");
for (int i=0;i<list.length;i++) {
if (List[i].isfile ()) {
Out.println (List[i].getname () + "<br>");
}
}
Out.println ("<br><font color= #ff0000 >" + path + "directory:</font><br>");
for (int i=0;i<list.length;i++) {
if (List[i].isdirectory ()) {
Out.println (List[i].getname () + "<br>");
}
}
%>
</body>

Determine if it is a blank file
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> Determine whether it is a blank file </title>
<body>
<%
String Path=request.getrealpath ("/");
Out.println (path);
FileReader fr=new FileReader (path + "\\AtEnd.txt");//Create a FileReader object, and instantiate it as FR
////To object generated by the FileReader class using the Read () method, You can read and remove one character from the character stream.
if (Fr.read () ==-1)//Determines if read to the end of the file
{
 out.print ("No data in AtEnd.txt file <br>");
} else{
 out.println ("There is data in AtEnd.txt file");

Fr.close ();
%>
</body>

Read all the file data
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*,java.lang.*"%>
<title> Read all the file data </title>
<body>
<%
String Path=request.getrealpath (".");
FileReader fr=new FileReader (path + "\\ReadData.txt");
The key is that in the reading process, you determine whether the character you read is at the end of the file, and that the character is not a break in the file, that is, whether the character value is 13.
int C=fr.read ()//reading a character from a file
Determine if the end of the file has been read
while (C!=-1) {
Out.print ((char) c);//output read data
C=fr.read ()//Continue reading data from file
if (c==13) {//To determine whether the break character
Out.print ("<br>");//Output Branch label
Fr.skip (1);//Skip one character
C=fr.read ()//Read a character
}
}
Fr.close ();
%>
</body>



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.