JSP file operations

Source: Internet
Author: User

This document describes common file operations, including file creation, check, and deletion, Directory Creation, check, and deletion, obtaining files in the directory, obtaining file attributes, and reading data row by row.
File Creation/check and Deletion
<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. Io. *" %>

<HTML>

<Head>

<Title> file creation, check, and deletion </title>

</Head>

<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 whether file.txt exists

F. Delete (); // Delete the file.txt File

Out. println (path + "\ file.txt exists and has been deleted. ");

} Else {

F. createnewfile (); // create a file named file.txt in the current directory.

Out. println (path + "\ file.txt does not exist. It has been created. "); // Output the current directory path

}

%>

Directory creation/check and Deletion
<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. Io. *" %>

<HTML>

<Head>

<Title> Directory Creation, check, and deletion </title>

</Head>

<Body>

<%

String Path = request. getrealpath ("");

Path = path + "\ Sub"; // directory path to be created

File d = new file (PATH); // create a file object that represents the sub directory and obtain a reference

If (D. exists () {// check whether the sub directory exists

D. Delete ();

Out. println ("sub directory exists, deleted ");

} Else {

D. mkdir (); // create a sub directory

Out. println ("sub directory does not exist, created ");

}

%>

</Body>

</Html>

How to process virtual directories in JSP
<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. Io. *" %>

<HTML>

<Head>

<Title> how to handle virtual directories in JSP </title>

</Head>

<Body>

Obtain the disk path corresponding to the virtual directory <br>

<Font color = # ff0000> <% = request. getrealpath ("/") %> </font> <br>

<Font color = # ff0000> <% = request. getrealpath ("./") %> </font> <br>

<Font color = # ff0000> <% = request. getrealpath ("../") %> </font> <br>

</Body>

</Html>

File Attribute acquisition

<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. util. Date, java. Io. *" %>

<HTML>

<Head>

<Title> File Attribute acquisition </title>

</Head>

<Body>

<%

String Path = request. getrealpath ("/");

File F = new file (path, "readdata.txt ");

If (F. exists ()){

%>

The attributes of <% = f. getname () %> are as follows: <br>

File length: <% = f. Length () %>

<% = F. isfile ()? "Yes file": "Not a file" %> <br>

<% = F. isdirectory ()? "Yes directory": "Not a directory" %> <br>

<% = F. Canread ()? "Readable": "unreadable" %> <br>

<% = F. canwrite ()? "Writable": "not writable" %> <br>

<% = F. ishidden ()? "Hidden file": "Not hidden file" %> <br>

The last modification date of the file is: <% = new date (F. lastmodified () %> <br>

<%

} Else {

F. createnewfile (); // create a file named readata.txt in the current directory.

%>

The attributes of <% = f. getname () %> are as follows: <br>

File length: <% = f. Length () %>

<% = F. isfile ()? "Yes file": "Not a file" %> <br>

<% = F. isdirectory ()? "Yes directory": "Not a directory" %> <br>

<% = F. Canread ()? "Readable": "unreadable" %> <br>

<% = F. canwrite ()? "Writable": "not writable" %> <br>

<% = F. ishidden ()? "Hidden file": "Not hidden file" %> <br>

The last modification date of the file is: <% = new date (F. lastmodified () %> <br>

<%

}

%>

</Body>

</Html>

How to retrieve files in a directory
<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. Io. *" %>

<HTML>

<Head>

<Title> how to retrieve files in a directory-list files in a directory </title>

</Head>

<Body>

<%

String Path = request. getrealpath ("/");

File d = new file (PATH); // create the file object of the file in the current directory

File list [] = D. listfiles (); // retrieves an array of file objects representing all files in the directory

Out. println ("<font color = # ff0000>" + path + "files in the directory: </font> <br> ");

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 Directory: </font> <br> ");

For (INT I = 0; I <list. length; I ++ ){

If (list <I>. isdirectory ()){

Out. println (list <I>. getname () + "<br> ");

}

}

%>

</Body>

</Html>

Determines whether the file is blank.

<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. Io. *" %>

<HTML>

<Head>

<Title> determine whether the file is a blank file </Title

 

>

</Head>

<Body>

<%

String Path = request. getrealpath ("/");

Out. println (PATH );

Filereader Fr = new filereader (path + "\ atend.txt"); // create a filereader object and instantiate it as fr

// Use the read () method for the object generated by the filereader class to read the next character from the delimiter stream.

If (Fr. Read () =-1) // determines whether the object has been read to the end.

{

Out. Print ("there is no data in the atend.txt file <br> ");

} Else {

Out. println ("data in the atend.txt file ");

}

Fr. Close ();

%>

</Body>

</Html>

<B> Read all file data </B>

<Ccid_nobr>

<Table width = "400" border = "1" cellspacing = "0" cellpadding = "2"

Bordercolorlight = "black" bordercolordark = "# ffffff" align = "center">

<Tr>

<TD bgcolor = "e6e6e6" class = "code" style = "font-size: 9pt">

<PRE> <ccid_code> <% @ page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. Io. *, java. Lang. *" %>

<HTML>

<Head>

<Title> Read all file data </title>

</Head>

<Body>

<%

String Path = request. getrealpath (".");

Filereader Fr = new filereader (path + "\ readdata.txt ");

// The key is to determine whether the characters read have reached the end of the file during the reading process,

Whether the character is a broken line character in the file determines whether the character value is 13.

Int c = Fr. Read (); // read a character from the file

// Determine whether the object has been read to the end

While (C! =-1 ){

Out. Print (char) C); // output the read data

C = Fr. Read (); // continue reading data from the file

If (C = 13) {// determines whether the row is a broken line character

Out. Print ("<br>"); // output branch label

Fr. Skip (1); // skip one character

// C = Fr. Read (); // read a character

}

}

Fr. Close ();

%>

</Body>

</Html>

Read data in one row
<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. Io. *" %>

<HTML>

<Head>

<Title> File Reading </title>

</Head>

<Body>

<%

String Path = request. getrealpath (""); // obtain the path of the current directory

Filereader Fr = new filereader (path + "\ file \ Inc \ t.txt"); // create a filereader object and instantiate it as fr

Bufferedreader BR = new bufferedreader (FR); // create a bufferedreader object and instantiate it as Br

String line = Br. Readline (); // read a string from a file

// Judge whether the read string is not empty

While (line! = NULL ){

Out. println (LINE + "<br>"); // output the data read from the file.

Line = Br. Readline (); // continue reading a row of data from the file

}

BR. Close (); // close the bufferedreader object

Fr. Close (); // close the file

%>

</Body>

</Html>

Skipping characters in the file is not read

<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. Io. *" %>

<HTML>

<Head>

<Title> skipped bytes are not read </title>

</Head>

<Body>

<%

String Path = request. getrealpath (".");

Filereader Fr = new filereader (path + "\ readdata.txt ");

Fr. Skip (2); // skip 2 bytes

Int c = Fr. Read (); // read a byte

While (C! =-1 ){

Out. Print (char) C );

C = Fr. Read ();

}

Fr. Close ();

%>

</Body>

</Html>

Write data to a file
<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. Io. *" %>

<HTML>

<Head>

<Title> write data to a file </title>

</Head>

<Body>

<%

String Path = request. getrealpath (".");

Filewriter fw = new filewriter (path + "\ writedata.txt"); // create a filewriter object and instantiate the FW

// Write a string to a file

FW. Write ("Hello! ");

FW. Write ("This book is JSP programming skills");

FW. Write ("Please advise! ");

FW. Write ("Email: stride@sina.com ");

FW. Close ();

Filereader Fr = new filereader (path + "\ writedata.txt ");

Bufferedreader BR = new bufferedreader (FR); // create a bufferedreader object and instantiate it as Br

String line = Br. Readline ();

// Read a row of data

Out. println (LINE + "<br> ");

BR. Close (); // close the bufferedreader object

Fr. Close ();

%>

</Body>

</Html>

Data branches that write files
<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. Io. *" %>

<HTML>

<Head>

<Title> data branches that will write files </title>

</Head>

<Body>

<%

String Path = request. getrealpath (".");

Filewriter fw = new filewriter (path + "\ writedata.txt ");

Bufferedwriter BW = new bufferedwriter (FW );

Bw. Write ("Hello! ");

Bw. Write ("This book is" JSP programming skills ". ");

Bw. newline (); // disconnected

Bw. Write ("Please advise! ");

Bw. newline (); // disconnected

Bw. Write ("Email: stride@sina.com ");

Bw. Flush (); // update data to a file

FW. Close (); // close the file stream

Out. println ("written file content: <br> ");

Filereader Fr = new filereader (path + "\ writedata.txt ");

Bufferedreader BR = new bufferedreader (FR );

String line = Br. readlin

E (); // read a row of data

While (line! = NULL ){

Out. println (LINE + "<br> ");

Line = Br. Readline ();

}

Fr. Close ();

%>

</Body>

</Html>

How to append data to a file
<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. Io. *" %>

<HTML>

<Head>

<Title> data branches that will write files </title>

</Head>

<Body>

<%

String Path = request. getrealpath (".");

Randomaccessfile Rf = new randomaccessfile (path + "\ writedata.txt", "RW ");

// Define an object of the randomaccessfile class and instantiate it.

RF. Seek (RF. Length (); // move the pointer to the end of the file

RF. writebytes ("\ nappend a line to the file! ");

RF. Close (); // close the file stream

Out. println ("written file content: <br> ");

Filereader Fr = new filereader (path + "\ writedata.txt ");

Bufferedreader BR = new bufferedreader (FR); // read the bufferedread object of the object

String line = Br. Readline ();

While (line! = NULL ){

Out. println (LINE + "<br> ");

Line = Br. Readline ();

}

Fr. Close (); // close the file

%>

</Body>

</Html> </I>

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.