java file read and write operations Encyclopedia

Transfer from http://blog.sina.com.cn/s/blog_4a9f789a0100ik3p.html

A console access to information input by the user

     public String getInputMessage () throws IOException ... {
         System.out.println ( "Please enter your command:");
         byte Buffer [] = new new byte [1024];
         int COUNT = System.in.read (Buffer);
         char [] = new new CH char [COUNT-2]; // last two bits at the end, not omitted
         for (int I = 0; I <COUNT-2; I ++)
             CH [I] = (char) Buffer [I];
         String String new new STR = (CH);
         return STR;
     }
     may return information input by the user, that no deficiencies support Chinese input, to be further improved.

     Second, copy the files
     1. file stream way of copying files

     public void copyFile (String src, dest String) throws IOException ... {
         FileInputStream in = new new FileInputStream (src);
         File File = new new File (dest);
         IF (! File.Exists ())
             file.createNewFile ();
         a FileOutputStream new new OUT = a FileOutputStream (File);
         int C;
         byte Buffer [] = new new byte [1024];
         the while ((C = in.read (Buffer)) = -! . 1) {...
             for (int I = 0; I <C; I ++)
                 out.write (Buffer [I]);        
         }
         in.close ();
         the out.close ();
     }
     this method has been tested to support Chinese process, and can be copied many types, such as txt, xml, jpg, doc other formats

     III. Write File

     1. The use of PrintStream to write files


     public void PrintStreamDemo()...{
         try ...{
             FileOutputStream out=new FileOutputStream("D:/test.txt");
             PrintStream p=new PrintStream(out);
             for(int i=0;i<10;i++)
                 p.println("This is "+i+" line");
         } catch (FileNotFoundException e) ...{
             e.printStackTrace();
         }
     }
     2.利用StringBuffer写文件
public void StringBufferDemo() throws IOException......{
         File file=new File("/root/sms.log");
         if(!file.exists())
             file.createNewFile();
         FileOutputStream out=new FileOutputStream(file,true);        
         for (int I = 0; I <10000; I ++) {......
             the StringBuffer the StringBuffer new new SB = ();
             sb.append ( "This is the first" + i + "row: the various methods previously described do not off time, why always strange question ");
             out.write (sb.toString () the getBytes. (" UTF-. 8 "));
         }        
         the out.close ();
     }
     the encoding method which can be set to use, effective solution to the Chinese problem.
IV. Rename the file
    
     public void RenameFile (String path, String oldname, String newname) ... {
         IF (! Oldname.equals (newname)) {// ... new file name and file name before is not the same, only necessary rename
             File = oldfile to new new File (path + "/" + oldname);
             File = newfile new new File (path + "/" + newname);
             IF (newfile.exists ()) // if there is already a directory in the the same name and new file, you can not rename
                 System.out.
             else...{
                 oldfile.renameTo(newfile);
             }
         }         
     }

  V. Transfer file directory
     transfer files directory is not equivalent to copy files, copy the file is two directories exist after the file copy, transfer files and directories after it is transferred, only the existence of the file in the new directory.
    
     void changeDirectory public (String filename, oldpath String, String newpath is, Boolean Cover) {...
         IF ... {(oldpath.equals (newpath is)!)
             File = oldfile to new new File (oldpath + "/" + filename);
             File newfile file new new = (newpath is + "/" + filename);
             IF (newfile.exists ()) {// ... If in the directory to be transferred, to be transferred file already exists
                 if (cover) // covering
                     oldfile.renameTo (newfile );
                 the else
                     System.out.println ( "already exists in the new directory:" + filename);
             }
             the else {...
                 oldfile.renameTo (newfile);
             }
         }       
     }
     Six read file
     1. FileInputStream read files using

    
     public FileInputStreamDemo String (String path) throws IOException {...
         File File = new new File (path);
         IF (File.Exists () || file.isDirectory ()! )
             the throw a FileNotFoundException new new ();
         the FileInputStream new new FIS = the FileInputStream (File);
         byte [] = buf new new byte [1024];
         the StringBuffer the StringBuffer new new SB = ();
         the while ((fis.read (buf))! = -. 1) {...
             sb.append (new new String (buf));    
             buf = new new byte [1024]; // regenerate, and to avoid duplication of data last read
         }
         return sb.toString ();
     }
2. use BufferedReader read

     In the IO operation, use BufferedReader and efficiency will be a little higher BufferedWriter


    
     public String BufferedReaderDemo(String path) throws IOException...{
         File file=new File(path);
         if(!file.exists()||file.isDirectory())
             throw new FileNotFoundException();
         BufferedReader br=new BufferedReader(new FileReader(file));
         String temp=null;
         StringBuffer sb=new StringBuffer();
         temp=br.readLine();
         while(temp!=null)...{
             sb.append(temp+" ");
             temp=br.readLine();
         }
         return sb.toString();
     }


     3. Use dom4j read xml file

    
     public Document readXml(String path) throws DocumentException, IOException...{
         File file=new File(path);
         BufferedReader bufferedreader = new BufferedReader(new FileReader(file));
         SAXReader saxreader = new SAXReader();
         Document document = (Document)saxreader.read(bufferedreader);
         bufferedreader.close();
         return document;
     }
     七.创建文件(文件夹)


1. Create a folder  
     public void createDir (String path) {...
         File dir = new new File (path);
         IF (dir.exists ()!)
             Dir.mkdir ();
     }

2. Create a new file
     public void createFile ( path String, filename String) throws IOException ... {
         file file = new new file (path + "/" + filename);
         IF (File.Exists (!))
             file.createNewFile ();
     }
     . eight deleting files (directory)
1 delete the file     
     public void delfile (String path, filename String) ... {
         file file = new new file (path + "/" + filename);
         IF (File.Exists () && file.isFile ())
             File.delete ();
     }

2. delete the directory
To take advantage delete File class () method to delete a directory, you must ensure that no files or subdirectories under that directory, or delete failed, so in practical applications, we want to delete the directory, you must use a recursive delete all subdirectories under that directory and files, and then delete the directory.  
     void delDir public (String path) {...
         File the dir = new new File (path);
         IF (dir.exists ()) {...
             File [] = tmp dir.listFiles ();
             for (int I = 0; I <tmp.length; I ++) {...
                 IF (tmp [I] .isDirectory ()) {...
                     delDir (path + "/" + tmp [I] .getName ());
                 }
                 the else {...
                     tmp [I] .Delete ();
                 }
             }
             dir.delete ();
         }
     }

Reproduced in: https: //my.oschina.net/youfen/blog/3058481

Guess you like

Origin blog.csdn.net/weixin_34392906/article/details/91871839