File Operations in JSP (File Stream)

Source: Internet
Author: User

Objects of the file class are mainly used to obtain information about the file itself, such as the directory where the file is located, the length of the file, and the read/write permissions of the file, and do not involve read/write operations on the file.
Constructor: file (string filename );
File (string directorypath, string filename );
File (file F, string filename );

File of the specified type:
Public String [] list (filenamefilter OBJ): returns all objects of the specified type in the directory in the string format.
Public file [] listfiles (filenamefilter OBJ): Use a file object to return all files of the specified type in the directory.
Filenamefilter is an interface that has a method:
Public Boolean accept (File Dir, string name );
Sample Code on the JSP page:
<%!
Class filejsp implements filenamefilter
{
String STR = NULL;
Filejsp (string S)
{
STR = "." + S;
}

Public Boolean accept (File Dir, string name)
{
Return name. endswith (STR );
}
}
%>
<Br> JSP files in the directory:
<%
Filejsp file_jsp = new filejsp ("jsp ");
String file_name [] = dir1.list (file_jsp );
For (INT I = 0; I <file_name.length; I ++)
{
Out. Print ("<br>" + file_name [I]);
}
%>

Use byte streams to read and write files:
All byte input stream classes are subclasses of the inputstream abstract class, And all byte output stream classes are subclasses of the outputstream abstract class.
The fileinputstream and fileoutputstream classes are inherited from the inputstream and outputstream classes respectively.

To improve read/write efficiency, fileinputstream is often used with bufferedinputstream streams.
Fileouputstream is often used with bufferedoutputstream.
The corresponding constructor methods are bufferedinputstream (inputstream in );
Bufferedoutputstream (outputstream out );
Sample Code: (written on the JSP page)
<%
File F6 = new file ("F:/SF/test1.java ");
Try
{
/* Fileoutputstream OUTFILE = new fileoutputstream (F6 );
Bufferedoutputstream bufferout = new bufferedoutputstream (OUTFILE );
Byte [] B = "Hello, glad to meet you! <Br> Nice to meet you ". getbytes ();
Bufferout. Write (B );
Bufferout. Flush ();
Bufferout. Close ();
OUTFILE. Close ();*/
Fileinputstream in = new fileinputstream (F6 );
Bufferedinputstream Bufferin = new bufferedinputstream (in );
Byte C [] = new byte [90];
Int n = 0;
While (n = Bufferin. Read (c ))! =-1)
{
String temp = new string (C, 0, N );
Out. Print (temp );
}
Bufferin. Close ();
In. Close ();

}
Catch (ioexception e ){}
%>

Use the volume stream to read and write files:
When byte streams are used to read and write texts, Java provides a writable stream because byte streams cannot directly operate Unicode characters. because the Chinese character occupies 2 bytes in the file, garbled characters may occur if the byte stream is used improperly. This phenomenon can be avoided if the byte stream is used. in Unicode characters, a Chinese character is considered as a character.
All character input stream classes are subclasses of the reader abstract class, And all character output stream classes are subclasses of the writer abstract class.
Filereader and filewriter, bufferedreader and bufferedwriter
Sample Code:
<%!
Public void writecontent (string STR, file F)
{
Try {
Filewriter OUTFILE = new filewriter (f );
Bufferedwriter bufferout = new bufferedwriter (OUTFILE );
Bufferout. Write (STR );
Bufferout. Close ();
OUTFILE. Close ();
} Catch (ioexception e ){}
}

Public String readcontent (file F)
{
Stringbuffer STR = new stringbuffer ();
Try {
Filereader in = new filereader (f );
Bufferedreader Bufferin = new bufferedreader (in );
String temp;
While (temp = Bufferin. Readline ())! = NULL)
{
Str. append (temp + "<br> ");
}
Bufferin. Close ();
In. Close ();
} Catch (ioexception e ){}
Return new string (STR );
}
%>

Data Stream:
Objects Created by the datainputstream and dataoutputstream classes are input streams and output streams respectively. they allow programs to read original Java data in a machine-independent style. that is to say, when reading a value, you do not have to worry about how many bytes the value should be.
Constructor: datainputstream (inputstream in );
Dataoutputstream (outputstream out );

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.