java文件IO操作

  1 package com.io;

  2 

  3 

  4 import java.io.File;

  5 import java.io.FileInputStream;

  6 import java.io.FileOutputStream;

  7 import java.io.FileWriter;

  8 import java.io.InputStream;

  9 import java.io.PrintWriter;

 10 

 11 public  class  FileOperate  {  

 12        /**  

 13          *  新建目录  

 14          *  @param  folderPath  String  如  c:/fqf  

 15          *  @return  boolean  

 16          */  

 17        public static void  newFolder(String  folderPath)  {  

 18            try  {  

 19                String  filePath  =  folderPath;  

 20                File  myFilePath  =  new File(filePath);  

 21                if  (!myFilePath.exists())  {  

 22                    myFilePath.mkdir();  

 23                }  

 24            }  

 25            catch  (Exception  e)  {  

 26                System.out.println("新建目录操作出错");  

 27                e.printStackTrace();  

 28            }  

 29        }  

 30        

 31     

 32  

 33        /**  

 34          *  新建文件  

 35          *  @param  filePathAndName  String  文件路径及名称  如c:/fqf.txt  

 36          *  @param  fileContent  String  文件内容  

 37          *  @return  boolean  

 38          */  

 39        public static void  newFile(String  filePathAndName,  String  fileContent)  {  

 40      

 41            try  {  

 42                String  filePath  =  filePathAndName;  

 43                File  myFilePath  =  new  File(filePath);  

 44                if  (!myFilePath.exists())  {  

 45                    myFilePath.createNewFile(); 

 46                    System.out.println("创建文件成功");

 47                }  

 48                FileWriter  resultFile  =  new  FileWriter(myFilePath);  

 49                PrintWriter  myFile  =  new  PrintWriter(resultFile);  

 50                myFile.println(fileContent);  

 51                System.out.println("写入文件成功");

 52                resultFile.close();  

 53            }  

 54            catch  (Exception  e)  {  

 55                System.out.println("新建文件操作出错");  

 56                e.printStackTrace();  

 57            }  

 58        }  

 59  

 60        /**  

 61          *  删除文件  

 62          *  @param  filePathAndName  String  文件路径及名称  如c:/fqf.txt  

 63          *  @param  fileContent  String  

 64          *  @return  boolean  

 65          */  

 66        public static  void  delFile(String  filePathAndName)  {  

 67            try  {  

 68                File myDelFile  = new File(filePathAndName);  

 69                myDelFile.delete();  

 70                System.out.println("删除文件成功!");

 71            }  

 72            catch  (Exception  e)  {  

 73                System.out.println("删除文件操作出错");  

 74                e.printStackTrace();  

 75            }  

 76        }  

 77      

 78        

 79       

 80  

 81        /**  

 82          *  删除文件夹  

 83          *  @param  filePathAndName  String  文件夹路径及名称  如c:/fqf  

 84          *  @param  fileContent  String  

 85          *  @return  boolean  

 86          */  

 87        public  void  delFolder(String  folderPath)  {  

 88            try  {  

 89                delAllFile(folderPath);  //删除完里面所有内容  

 90                File  myFilePath  =  new File(folderPath);  

 91                myFilePath.delete();  //删除空文件夹  

 92            }  

 93            catch  (Exception  e)  {  

 94                System.out.println("删除文件夹操作出错");  

 95                e.printStackTrace();  

 96            }  

 97        }  

 98  

 99        /**  

100          *  删除文件夹里面的所有文件  

101          *  @param  path  String  文件夹路径  如  c:/fqf  

102          */  

103        public  void  delAllFile(String  path)  {  

104            File  file  =  new  File(path);  

105            if  (!file.exists())  {  

106                return;  

107            }  

108            if  (!file.isDirectory())  //判读此抽象路径名表示的文件存在且 是一个目录

109            {  

110                return;  

111            }  

112            String[]  tempList  =  file.list();  

113            File  temp  =  null;  

114            for  (int  i  =  0;  i  <  tempList.length;  i++)  {  

115                if  (path.endsWith(File.separator))  {  

116                    temp  =  new  File(path  +  tempList[i]);  

117                }  

118                else  {  

119                    temp  =  new  File(path  +  File.separator  +  tempList[i]);  

120                }  

121                if  (temp.isFile())  {  

122                    temp.delete();  

123                }  

124                if  (temp.isDirectory())  {  

125                    delAllFile(path+"/"+  tempList[i]);//先删除文件夹里面的文件  

126                    delFolder(path+"/"+  tempList[i]);//再删除空文件夹  

127                }  

128            }  

129        }  

130  

131        /**  

132          *  复制单个文件  

133          *  @param  oldPath  String  原文件路径  如:c:/fqf.txt  

134          *  @param  newPath  String  复制后路径  如:f:/fqf.txt  

135          *  @return  boolean  

136          */  

137        public  void  copyFile(String  oldPath,  String  newPath)  {  

138            try  {  

139                int  bytesum  =  0;  

140                int  byteread  =  0;  

141                File  oldfile  =  new  File(oldPath);  

142                if  (oldfile.exists())  {  //文件存在时  

143                    InputStream  inStream  =  new  FileInputStream(oldPath);  //读入原文件  

144                    FileOutputStream  fs  =  new  FileOutputStream(newPath);  

145                    byte[]  buffer  =  new  byte[1444];  

146                    int  length;  

147                    while  (  (byteread  =  inStream.read(buffer))  !=  -1)  {  

148                        bytesum  +=  byteread;  //字节数  文件大小  

149                        System.out.println(bytesum);  

150                        fs.write(buffer,  0,  byteread);  

151                    }  

152                    inStream.close();  

153                }  

154            }  

155            catch  (Exception  e)  {  

156                System.out.println("复制单个文件操作出错");  

157                e.printStackTrace();  

158            }  

159        }  

160  

161        /**  

162          *  复制整个文件夹内容  

163          *  @param  oldPath  String  原文件路径  如:c:/fqf  

164          *  @param  newPath  String  复制后路径  如:f:/fqf/ff  

165          *  @return  boolean  

166          */  

167        public  void  copyFolder(String  oldPath,  String  newPath)  {  

168            try  {  

169                (new  File(newPath)).mkdirs();  //如果文件夹不存在  则建立新文件夹  

170                File  a=new  File(oldPath);  

171                String[]  file=a.list();  

172                File  temp=null;  

173                for  (int  i  =  0;  i  <  file.length;  i++)  {  

174                    if(oldPath.endsWith(File.separator)){  

175                        temp=new  File(oldPath+file[i]);  

176                    }  

177                    else{  

178                        temp=new  File(oldPath+File.separator+file[i]);  

179                    }  

180                    if(temp.isFile()){  

181                        FileInputStream  input  =  new  FileInputStream(temp);  

182                        FileOutputStream  output  =  new  FileOutputStream(newPath  +  "/"  +  

183                                (temp.getName()).toString());  

184                        byte[]  b  =  new  byte[1024  *  5];  

185                        int  len;  

186                        while  (  (len  =  input.read(b))  !=  -1)  {  

187                            output.write(b,  0,  len);  

188                        }  

189                        output.flush();  

190                        output.close();  

191                        input.close();  

192                    }  

193                    if(temp.isDirectory()){//如果是子文件夹  

194                        copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);  

195                    }  

196                }  

197            }  

198            catch  (Exception  e)  {  

199                System.out.println("复制整个文件夹内容操作出错");  

200                e.printStackTrace();  

201            }  

202        }  

203  

204        /**  

205          *  移动文件到指定目录  

206          *  @param  oldPath  String  如:c:/fqf.txt  

207          *  @param  newPath  String  如:d:/fqf.txt  

208          */  

209        public  void  moveFile(String  oldPath,  String  newPath)  {  

210            copyFile(oldPath,  newPath);  

211            delFile(oldPath);  

212        }  

213      

214        /**  

215          *  移动文件到指定目录  

216          *  @param  oldPath  String  如:c:/fqf.txt  

217          *  @param  newPath  String  如:d:/fqf.txt  

218          */  

219        public  void  moveFolder(String  oldPath,  String  newPath)  {  

220            copyFolder(oldPath,  newPath);  

221            delFolder(oldPath);  

222      

223        }  

224  

225            // 拷贝文件

226        private void copyFile2(String source, String dest) {

227        try {

228            File in = new File(source);

229            File out = new File(dest);

230            FileInputStream inFile = new FileInputStream(in);

231            FileOutputStream outFile = new FileOutputStream(out);

232            byte[] buffer = new byte[1024];

233            int i = 0;

234            while ((i = inFile.read(buffer)) != -1) {

235            outFile.write(buffer, 0, i);

236        }//end while

237            inFile.close();

238            outFile.close();

239        }//end try

240        catch (Exception e) {

241     

242        }//end catch

243        }//end copyFile

244 }

 

你可能感兴趣的:(java)