Java File Operations Encyclopedia

Source: Internet
Author: User
Tags date continue readable readline
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 a blank file
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> determine if 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 <br> in AtEnd.txt file");
}else{
OUT.PRINTLN ("Data in AtEnd.txt document");
}
Fr.close ();
%>
</body>
<B> Read all the file data </B>
<ccid_nobr>
<table width= "border=" 1 "cellspacing=" 0 "cellpadding=" 2 "
Bordercolorlight = "BLACK" Bordercolordark = "#FFFFFF" align= "Center" >
<tr>
&LT;TD bgcolor= "E6e6e6" class= "code" style= "Font-size:9pt" >
<pre><ccid_code> <%@ 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 in the read process, to determine whether the character read has been to the end of the file,
And this character is not a break in the file, that is to determine 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> Skip bytes 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> Write data to 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 advise!") ");
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> data to 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 advise!") ");
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> data to be written to the file branch </title>
<body>
<%
String Path=request.getrealpath (".");
Randomaccessfile rf=new randomaccessfile (path + "\\WriteData.txt", "RW");
Defines a class-randomaccessfile object and instantiates
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>



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.