File Operations in JSP

Source: Internet
Author: User

File Operations in JSP
1. File class
Objects of the File class are mainly used to obtain information about the File itself.
There are three methods to create a File object:
File (String filename );
File (String directoryPath, String filename );
File (File f, String filename );
Filename is the file name, directoryPath is the file path, and f is the file specified as a directory.
When a File is created using File (String filename), the File is considered to be in the same directory as the current application. Because the JSP engine is started and executed under the bin
The file is considered in the following directory: D: \ Tomcat \ jakarta-tomcat-4.0 \ bin \.
1. Get File Attributes
1. public String getName (): Get the object name.
2. public boolean canRead (): determines whether the object is readable.
3. public boolean canWrite (): determines whether a file can be written.
4. public boolean exits (): determines whether a file exists.
5. public long length (): Get the object length (in bytes ).
6. public String getAbsolutePath (): gets the absolute path of the object.
7. public String getParent (): Get the parent directory of the file.
8. public boolean isFile (): determines whether the file is a normal file, not a directory.
9. public boolean isDirectroy (): determines whether the file is a directory.
10. public boolean isHidden (): determines whether the object is a hidden object.
11. public long lastModified (): Get the last modification time of the object (the number of milliseconds from midnight on January 1, 1970 to the last modification time of the object)
2. Create a directory
(1) create a directory
File object call method: public boolean mkdir () to create a directory. If the directory is successfully created, true is returned; otherwise, false is returned (if the directory already exists, false is returned ).
(2) list files in the directory
If the File object is a directory, you can call the following method to list the files and subdirectories in the directory:
Public String [] list (): returns all files in the directory in the String format,
Public File [] listFiles (): returns all files in the directory as a File object.
(3) List objects of the specified type
You can use the following two methods of the File class to list objects of the specified type:
Public String [] list (FilenameFilter obj); this method returns all objects of the specified type in the directory in String format.
Public File [] listFiles (FilenameFilter obj); this method uses the File object to return all files of the specified type in the directory.
FilenameFile is an interface that has a method:
Public boolean accept (File dir, String name );
When an object implementing this interface is passed to the list method, when the list method is listing objects, the file will call the accept method to check whether the file meets the Directory and file name requirements specified by the accept method.
3. delete files and directories
The File object call method public boolean delete () can delete the files or directories represented by the current object. If the File object represents a directory, the directory must be an empty directory, returns true if the deletion is successful.
2. Use byte streams to read and write files
The java. io package provides a large number of stream classes. All byte input stream classes are subclasses of the InputStream (input stream) abstract class, And all byte output streams are subclasses of the OutputStream (output stream) abstract class.
Common InputStream methods:
Int read (): The input stream calls this method to read data of a single byte from the source. This method returns the byte value (0 ~ An integer between 255). If the byte is not read,-1 is returned.
Int read (byte B []): The input stream calls this method to try to read the number of B. length bytes from the source to B, and returns the actual number of bytes read. If the end of the file is reached,-1 is returned.
Int read (byte B [], int off, int len): The input stream calls this method to try to read len bytes from the source to B and return the actual number of bytes read.
If the end of the object is reached,-1 is returned. The off parameter specifies to store the read data starting from a location in the byte array.
Void close (): The input stream calls this method to close the input stream.
Long skip (long numBytes): This method is called by the input stream to skip numBytes bytes and return the number of actually skipped bytes.
Common Methods of the OutputStream class:
Void write (int n): the output stream calls this method to write a single byte to the output stream.
Void write (byte B []): the output stream calls this method to write a byte array to the output stream.
Void write (byte B [], int off, int len): Extracts len bytes from the offset off of the given byte array and writes them to the output stream.
Void close (): close the output stream
2.1.FileInputStream and FileOutputStream
The FileInputStream class is a simple input class derived from InputStream. All methods of this class are inherited from the InputStream class.
Two constructor methods:
FileInputStream (String name); name: File name
FileInputStream (File file); file: File object
The file name specified by the parameter name and file is used as the source of the input stream. The input stream reads the data in the source by calling the read method.
The class corresponding to the FileInputStream class is the FileOutputStream class. FileOutputStream provides basic file writing capabilities. Except for methods inherited from the OutputStream class,
The FileOutputStream class also has two common constructor methods:
FileOutputStream (String name );
FileOutputStream (File file );
2.2.BufferedInputStream and BufferedOutputStream
To improve read/write efficiency, FileInputStream streams are often used together with BufferedInputStream streams, while FileOutputStream streams are often used together with BufferedOutputStream streams.
A common constructor of BufferedInputStream is:
BufferedInputStream (InputStream in );

FileInputStream in = new fileinputstream()a.txt ");
BufferedInputStream buffer = new BufferedInputStream (in );

FileOutputStream out = new FileOutputStream(“ B .txt ");
BufferedOutputStream buffer = new BufferedOutputStream (out
Note that after writing, you must call the flush method to save the cached data to the file.
3. Use the volume stream to read and write files
Byte streams cannot directly operate on Unicode characters. Therefore, Java provides the bytes stream. Because Chinese characters occupy two bytes in the file, if byte streams are used, garbled characters may occur if improper reading occurs.
This can be avoided. In Unicode characters, a Chinese character is considered as a character.
All character input stream classes are subclasses of the Reader (input stream) abstract class, And all character output streams are subclasses of the Writer (output stream) abstract class.
Common Methods in Reader class:
Int read (): The input stream calls this method to read a character from the source. This method returns an INTEGER (0 ~ An integer between 65535 and a Unicode character value). If the character is not read,-1 is returned.
Int read (char B []): The input stream calls this method to read B. length characters from the source to the character array B, and returns the actual number of characters read. If the end of the file is reached,-1 is returned.
Int read (char B [], int off, int len): The input stream calls this method to read len characters from the source and store them in character array B. The actual number of characters read is returned.
If the end of the file is reached,-1 is returned. The off parameter specifies where the read method stores data from array B.
Void close (): The input stream calls this method to close the input stream.
Long skip (long numBytes): This method is called by the input stream to skip numBytes characters and return the number of actually skipped characters.
Common Methods in Writer class:
Void write (int n): write a character to the input stream.
Void write (char B []): write a character array to the input stream.
Void write (char B [], int off, int length): Extracts len characters from the offset off of the given character array and writes them to the output stream.
Void close (): close the output stream.
3.1 FileReader and FileWriter classes
The FileReader class is a simple input class derived from Reader. All methods of this class are inherited from the Reader class.
Two constructor methods:
FileReader (String name );
FileReader (File file );
The class corresponding to the FileReader class is the FileWriter class. FileWriter provides basic file writing capabilities. In addition to methods inherited from the FileWriter class, FileWriter
Class also has two common constructor Methods
FileWriter (String name );
FileWriter (File file );
3.2 BufferedReader and BufferedWriter
To improve read/write efficiency, FileReader streams are often used with BufferedReader streams, while FileWriter streams are often used with BufferedWriter streams.
The BufferedReader stream can also use the String readLine () method to read a row;
BufferedWriter streams can also use the void write (String s, 0000ff, int length) method to write part of String s into the file,
Use newLine () to write a line separator to the file.
4. Back-to-pressure compaction stream
The object created by the PushbackReader class is the back-pressure upstreaming stream. The back-to-pressure stream can use unread (char ch) to compress a character into the stream. The back-to-pressure character is the back-to-pressure stream.
The first character to be read when the read () method is called. The back-to-pressure stream can be used to monitor read information. When you read an unwanted information, you do not need to process it.
Information, and then read the information.
PushbackReader (Reader in );
5 Data Streams
Objects Created by DataInputStream and DataOutputStream are called data input streams and data output streams. These two streams are very useful. They allow
Read the original Java data in order according to the machine-independent style. That is to say, when we read a value, we don't have to worry about how many bytes the value should be.
DataInputStream class and DataOutputStream constructor:
DataInputStream (InputStream in): points the created data input stream to an input stream specified by the parameter in.
DataOutputStream (OutnputStream out): points the created data output stream to an output stream specified by the parameter out.
6. Object stream
The ObjectInputStream and ObjectOutputStream classes are subclasses of the DataInputStream and DataOutputStream classes respectively. ObjectInputStream
Objects Created by class and ObjectOutputStream are called object input streams and object output streams. The Object output stream uses the writeObject (Object obj) method to write an Object obj
To a file, the object input stream uses readObject () to read an object to the program.
The constructor methods of the ObjectInputStream class and the ObjectOutputStream class are:
ObjectInputStream (InputStream in );
ObjectOutputStream (OutputStream out );
7 RandomAccessFile class
The stream created by the RandomAccessFile class is different from the previous input and output streams. The RandomAccessFile class is neither a subclass of the InputStream class of the input stream class nor
Is a subclass stream of the output stream class OutputStram class.
A RandomAccessFile stream can be used as a source or destination. In other words, when we want to perform read/write operations on a file, we can create
You can use the RandomAccessFile stream to read data from this file and write data to this file.
Two construction methods of the RandomAccessFile class:
RandomAccessFile (String name, String mode): The parameter name is used to determine a file name and provide the source (also the stream destination) of the created stream ),
If the mode parameter is set to "r" (read-only) or "rw" (read/write), the access right of the created stream to the file is determined.
RandomAccessFile (File file, String mode): The parameter file is a File object that provides the source (also the stream destination) of the created stream ),
If the mode parameter is set to "r" (read-only) or "rw" (read/write), the access right of the created stream to the file is determined.
8. File Upload
When a user uploads a File to the server through a JSP page, the JSP page must contain a File-type form, and the form must set the value of ENCTYPE
"Multipart/form-data ",

The JSP Engine allows the built-in object request calling method getInputStream () to obtain an input stream, which reads all Information uploaded by the customer, including the file content and form field information.
9. File Download
The JSP built-in object response call method getOutputStream () can obtain an output stream pointing to the customer. The server writes the file to the stream, and the customer can download the file.

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.