About JSP file path problems (EXT) __jsp

Source: Internet
Author: User
Tags readable readline
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
Establishment, inspection and deletion of <title> files </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.*"%>
Establishment/inspection and deletion of <title> directory </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>
Gets the disk path corresponding to 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.*"%>
The acquisition of <title> 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 a directory--list files in a 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 a blank file
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> judge whether it is a blank file </title>
<body>
<%
String Path=request.getrealpath ("/");
OUT.PRINTLN (path);
FileReader fr=new FileReader (path + "//atend.txt");//Create the FileReader object and instantiate it as FR
Objects generated by the FileReader class use the Read () method to read and remove one character from the character stream.
if (Fr.read () ==-1)//Determines whether the end of the file has been read
{
Out.print ("No data in AtEnd.txt file <br>");
}else{
OUT.PRINTLN ("Data in AtEnd.txt document");
}
Fr.close ();
%>
</body>


Read all the file data
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*,java.lang.*"%>
<title> reads 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>
read Data on one line
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> file Read </title>
<body>
<%
String Path=request.getrealpath ("");//Get the path to the current directory
FileReader fr=new FileReader (path + "//file//inc//t.txt");//Create the FileReader object and instantiate it as FR
BufferedReader br=new BufferedReader (FR);//Establish BufferedReader object and instantiate as BR
String Line=br.readline ()//reading a line from a file
Determines whether the read string is not empty
while (Line!=null) {
Out.println (line + "<br>")//Output data read from file
Line=br.readline ()//Continue reading a row of data from a file
}
Br.close ()//Close BufferedReader Object
Fr.close ()//close file
%>
</body>


Skipping characters in a file does not read
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> byte does not read </title>
<body>
<%
String Path=request.getrealpath (".");
FileReader fr=new FileReader (path + "//readdata.txt");
Fr.skip (2);//Skip 2 bytes
int C=fr.read ()//read one byte
while (C!=-1) {
Out.print ((char) c);
C=fr.read ();
}
Fr.close ();
%>
</body>


writing data to a file
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> writes data to a file </title>
<body>
<%
String Path=request.getrealpath (".");
FileWriter fw=new FileWriter (path + "//writedata.txt");//Create FileWriter object and instantiate FW
Writing a string to a file
Fw.write ("Hello, everyone.") ");
Fw.write ("This book is the JSP Programming skill");
Fw.write ("Please give me a lot of advice." ");
Fw.write ("email:stride@sina.com");
Fw.close ();

FileReader fr=new FileReader (path + "//writedata.txt");
BufferedReader br=new BufferedReader (FR);//Establish BufferedReader object and instantiate as BR
String Line=br.readline ();
Reading a row of data
Out.println (line + "<br>");
Br.close ()//Close BufferedReader Object
Fr.close ();
%>
</body>


The data branch that will be written to the file
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> the data that will be written to the file branch </title>
<body>
<%
String Path=request.getrealpath (".");
FileWriter fw=new FileWriter (path + "//writedata.txt");
BufferedWriter bw=new BufferedWriter (FW);
Bw.write ("Hello, everyone.") ");
Bw.write ("This book is the JSP Programming technique".) ");
Bw.newline ()/Break
Bw.write ("Please give me a lot of advice." ");
Bw.newline ()/Break
Bw.write ("email:stride@sina.com");
Bw.flush ()//Update data to File
Fw.close ()//close file stream
Out.println ("Write file content is:<br>");
FileReader fr=new FileReader (path + "//writedata.txt");
BufferedReader br=new BufferedReader (FR);
String line=br.readline ();//reading a row of data
while (Line!=null) {
Out.println (line + "<br>");
Line=br.readline ();
}
Fr.close ();
%>
</body>
How to append data to a file
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> the data that will be written to the file branch </title>
<body>
<%
String Path=request.getrealpath (".");
Randomaccessfile rf=new randomaccessfile (path + "//writedata.txt", "RW");//define object for a class Randomaccessfile and instantiate
Rf.seek (Rf.length ());//move pointer to end of file
Rf.writebytes ("/nappend a line to the file!");
Rf.close ()//close file stream
Out.println ("Write file content is:<br>");
FileReader fr=new FileReader (path + "//writedata.txt");
BufferedReader br=new BufferedReader (FR);//Bufferedread object to read files
String Line=br.readline ();
while (Line!=null) {
Out.println (line + "<br>");
Line=br.readline ();
}
Fr.close ()//close file
%>
</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.