JSP file operations (file stream)

Object File class is mainly used to get some information about the file itself. Such as a directory file is located, and the length of the file read and write access to the file, which does not involve a file read and write operations.
Constructor: File (String filename);
File(String directoryPath,String filename);
File (File f, String filename) ;


specific types of files:
public String [] List (the FilenameFilter obj): Returns the specified type directory in the directory as a string of all files.
public File [] listFiles (FilenameFilter obj ): Returns the specified types of all files in the directory with File object.
FilenameFilter is an interface, the interface has a method:
public Boolean Accept (File the dir, String name);
exemplified jsp pages code:
<%!
         class FileJSP implements FilenameFilter
         {
         String str = null;
         FileJSP(String s)
         {
         str = "."+s;
         }
         public boolean accept(File dir,String name)
         {
         return name.endsWith(str);
         }
         }
          %>
          JSP file directory <br>:
          <%
           FileJSP file_jsp new 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]);
           }
           %>


byte stream read and write files:
All bytes of the input stream InputStream abstract class is a subclass of the class, and all the bytes output OutputStream classes are subclasses of the abstract class.
FileInputStream, FileOutputStream classes are from InputStream, OutputStream class inherited.


In order to improve the efficiency of reading and writing, FileInputStream BufferedInputStream often used in conjunction with the flow.
FileOuputStream flow and BufferedOutputStream often used in conjunction with the flow.
Corresponding construction method are: BufferedInputStream (InputStream in);
    BufferedOutputStream (OutputStream out);
Example code: (write JSP pages)
<%
           File f6 = new File("F:/SF/Test1.java");
           try
           {
           /* FileOutputStream outfile = new FileOutputStream(f6);
           BufferedOutputStream bufferout = new BufferedOutputStream(outfile);
           byte [] b = "! Hello, nice to meet you Yeah <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();
          
           }
           the catch (IOException E) {}
            %>


character stream read and write files:
  When a byte stream read and write text, as the byte stream can not directly operate Unicode characters, so the character stream provided java. As the characters occupy two bytes in the file, if you use a byte stream to read improper garbled phenomenon, the use of the character stream can avoid this phenomenon. In Unicode characters, a character is seen as a character.
All the characters in the input stream classes are subclasses of the abstract class Reader, and all character output stream classes are subclasses of the abstract class Writer.
FileReader classes and FileWriter classes, BufferedReader classes and BufferedWriter Class
Code Example:
  ! <%
     Public void WriteContent (String STR, File F)
     {
     the try {
     FileWriter outfile = new new FileWriter (F);
     BufferedWriter BUFFEROUT = new new BufferedWriter (outfile);
     BUFFEROUT. Write (STR);
     bufferout.close ();
     outfile.close ();
     } the catch (IOException E) {}
     } public String readContent (File F) { the StringBuffer the StringBuffer new new STR = ();
    
    
    
    
     {the try
     the FileReader in the FileReader new new = (F);
     the BufferedReader the BufferedReader of BUFFERIN new new = (in);
     String TEMP;
     the while (! (bufferin.readLine TEMP = ()) = null)
     {
     str.append (TEMP + "<br>") ;
     }
     bufferin.close ();
     in.close ();
     } the catch (IOException E) {}
     return new new String (STR);
     }
     %>


stream:
the object class and DataOutputStream DataInputStream class are created and the data input data stream the output stream. They allow java program reads the original data according to machine-independent style. That is, when reading a value, this value should not have to care about is the number of bytes.
The method is configured to: DataInputStream (InputStream in);
   DataOutputStream(OutputStream out);

Reproduced in: https: //my.oschina.net/u/2552902/blog/543839

Guess you like

Origin blog.csdn.net/weixin_34195364/article/details/92326641