JAvaファイル操作大全


 .            
Java   
/** *//**              
     * @return  
     * @throws IOException  
     */  
    public String getInputMessage() throws IOException...{   
        System.out.println("       ∶");   
        byte buffer[]=new byte[1024];   
        int count=System.in.read(buffer);   
        char[] ch=new char[count-2];//        ,       
        for(int i=0;i<count-2;i++)   
            ch[i]=(char)buffer[i];   
        String str=new String(ch);   
        return str;   
    }  
Java   
/** *//**             
     * @return 
     * @throws IOException 
     */  
    public String getInputMessage() throws IOException...{  
        System.out.println("       ∶");  
        byte buffer[]=new byte[1024];  
        int count=System.in.read(buffer);  
        char[] ch=new char[count-2];//        ,      
        for(int i=0;i<count-2;i++)  
            ch[i]=(char)buffer[i];  
        String str=new String(ch);  
        return str;  
    }  

           ,             ,       。 


 .     

1.            
Java   
/** *//**             
     * @param src        
     * @param dest         
     * @throws IOException    
     */  
    public void copyFile(String src,String dest) throws IOException...{   
        FileInputStream in=new FileInputStream(src);   
        File file=new File(dest);   
        if(!file.exists())   
            file.createNewFile();   
        FileOutputStream out=new FileOutputStream(file);   
        int c;   
        byte buffer[]=new byte[1024];   
        while((c=in.read(buffer))!=-1)...{   
            for(int i=0;i<c;i++)   
                out.write(buffer[i]);           
        }   
        in.close();   
        out.close();   
    }  
Java   
/** *//**            
     * @param src       
     * @param dest        
     * @throws IOException   
     */  
    public void copyFile(String src,String dest) throws IOException...{  
        FileInputStream in=new FileInputStream(src);  
        File file=new File(dest);  
        if(!file.exists())  
            file.createNewFile();  
        FileOutputStream out=new FileOutputStream(file);  
        int c;  
        byte buffer[]=new byte[1024];  
        while((c=in.read(buffer))!=-1)...{  
            for(int i=0;i<c;i++)  
                out.write(buffer[i]);          
        }  
        in.close();  
        out.close();  
    }  

       ,      ,          ,  txt,xml,jpg,doc      

 .    

1.  PrintStream    
Java   
/** *//**  
     *         
     */  
    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();   
        }   
    }  
Java   
/** *//** 
     *        
     */  
    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    
Java   
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++)......{   
            StringBuffer sb=new StringBuffer();   
            sb.append("   "+i+" :             ,           ");   
            out.write(sb.toString().getBytes("utf-8"));   
        }           
        out.close();   
    }  
Java   
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++)......{  
            StringBuffer sb=new StringBuffer();  
            sb.append("   "+i+" :             ,           ");  
            out.write(sb.toString().getBytes("utf-8"));  
        }          
        out.close();  
    }  

             ,        。 

 .      
Java   
/** *//**       
 * @param path       
 * @param oldname          
 * @param newname       
 */  
public void renameFile(String path,String oldname,String newname)...{   
    if(!oldname.equals(newname))...{//              ,            
        File oldfile=new File(path+"/"+oldname);   
        File newfile=new File(path+"/"+newname);   
        if(newfile.exists())//                    ,          
            System.out.println(newname+"    !");   
        else...{   
            oldfile.renameTo(newfile);   
        }    
    }            
}  
Java   
/** *//**      
 * @param path      
 * @param oldname         
 * @param newname      
 */  
public void renameFile(String path,String oldname,String newname)...{  
    if(!oldname.equals(newname))...{//              ,           
        File oldfile=new File(path+"/"+oldname);  
        File newfile=new File(path+"/"+newname);  
        if(newfile.exists())//                    ,         
            System.out.println(newname+"    !");  
        else...{  
            oldfile.renameTo(newfile);  
        }   
    }           
}  


 .       


              ,                  ,            ,           。 
Java   
/** *//**        
 * @param filename      
 * @param oldpath      
 * @param newpath      
 * @param cover                        ,          ,cover=true       ,       
 */  
public void changeDirectory(String filename,String oldpath,String newpath,boolean cover)...{   
    if(!oldpath.equals(newpath))...{   
        File oldfile=new File(oldpath+"/"+filename);   
        File newfile=new File(newpath+"/"+filename);   
        if(newfile.exists())...{//        ,            
            if(cover)//     
                oldfile.renameTo(newfile);   
            else  
                System.out.println("         :"+filename);   
        }   
        else...{   
            oldfile.renameTo(newfile);   
        }   
    }          
}  
Java   
/** *//**       
 * @param filename     
 * @param oldpath     
 * @param newpath     
 * @param cover                        ,          ,cover=true       ,      
 */  
public void changeDirectory(String filename,String oldpath,String newpath,boolean cover)...{  
    if(!oldpath.equals(newpath))...{  
        File oldfile=new File(oldpath+"/"+filename);  
        File newfile=new File(newpath+"/"+filename);  
        if(newfile.exists())...{//        ,           
            if(cover)//    
                oldfile.renameTo(newfile);  
            else  
                System.out.println("         :"+filename);  
        }  
        else...{  
            oldfile.renameTo(newfile);  
        }  
    }         
}  


 .    

1.  FileInputStream     

2.  BufferedReader   

 IO  ,  BufferedReader BufferedWriter        

3.  dom4j  xml   
Java   
/** *//**      xml    
 * @param path       
 * @return  
 * @throws DocumentException  
 * @throws IOException  
 */  
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;   
}  
Java   
/** *//**      xml   
 * @param path      
 * @return 
 * @throws DocumentException 
 * @throws IOException 
 */  
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.     
Java   
/** *//**       
    * @param path      
    */  
   public void createDir(String path)...{   
       File dir=new File(path);   
       if(!dir.exists())   
           dir.mkdir();   
   }  
Java   
/** *//**      
    * @param path     
    */  
   public void createDir(String path)...{  
       File dir=new File(path);  
       if(!dir.exists())  
           dir.mkdir();  
   }  


2.      
Java   
/** *//**       
     * @param path     
     * @param filename      
     * @throws IOException  
     */  
    public void createFile(String path,String filename) throws IOException...{   
        File file=new File(path+"/"+filename);   
        if(!file.exists())   
            file.createNewFile();   
    }  
Java   
/** *//**      
     * @param path    
     * @param filename     
     * @throws IOException 
     */  
    public void createFile(String path,String filename) throws IOException...{  
        File file=new File(path+"/"+filename);  
        if(!file.exists())  
            file.createNewFile();  
    }  


 .    (  ) 

1.    
Java   
/** *//**      
 * @param path     
 * @param filename      
 */  
public void delFile(String path,String filename)...{   
    File file=new File(path+"/"+filename);   
    if(file.exists()&&file.isFile())   
        file.delete();   
}  
Java   
/** *//**     
 * @param path    
 * @param filename     
 */  
public void delFile(String path,String filename)...{  
    File file=new File(path+"/"+filename);  
    if(file.exists()&&file.isFile())  
        file.delete();  
}  


2.     
   File  delete()       ,                 ,      ,        ,       ,                     ,        。
Java   
/** *//**         
    * @param path  
    */  
   public void delDir(String path)...{   
       File dir=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());   
               }   
               else...{   
                   tmp[i].delete();   
               }   
           }   
           dir.delete();   
       }   
   }  
Java   
/** *//**        
    * @param path 
    */  
   public void delDir(String path)...{  
       File dir=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());  
               }  
               else...{  
                   tmp[i].delete();  
               }  
           }  
           dir.delete();  
       }  
   }  

3、   
Java   
/** *//**     
  * @param path  
  * @return  
  * @throws IOException  
  */  
 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();   
 }  
Java   
/** *//**    
  * @param path 
  * @return 
  * @throws IOException 
  */  
 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();  
 }  

Java   
/** *//**     
  * @param path  
  * @return  
  * @throws IOException  
  */  
 public String FileInputStreamDemo(String path) throws IOException...{   
     File file=new File(path);   
     if(!file.exists()||file.isDirectory())   
         throw new FileNotFoundException();   
     FileInputStream fis=new FileInputStream(file);   
     byte[] buf = new byte[1024];   
     StringBuffer sb=new StringBuffer();   
     while((fis.read(buf))!=-1)...{   
         sb.append(new String(buf));       
         buf=new byte[1024];//    ,               
     }   
     return sb.toString();   
 }