转载:在JAVA中文件和目录都被抽象为FILE来处理
package com.io; |
002 |
003 |
004 | import java.io.File; |
005 | import java.io.FileInputStream; |
006 | import java.io.FileOutputStream; |
007 | import java.io.FileWriter; |
008 | import java.io.InputStream; |
009 | import java.io.PrintWriter; |
010 |
011 | public class FileOperate { |
012 | /** |
013 | * 新建目录 |
014 | * @param folderPath String 如 c:/fqf |
015 | * @return boolean |
016 | */ |
017 | public static void newFolder(String folderPath) { |
018 | try { |
019 | String filePath = folderPath; |
020 | File myFilePath = new File(filePath); |
021 | if (!myFilePath.exists()) { |
022 | myFilePath.mkdir(); |
023 | } |
024 | } |
025 | catch (Exception e) { |
026 | System.out.println( "新建目录操作出错" ); |
027 | e.printStackTrace(); |
028 | } |
029 | } |
030 | |
031 | |
032 | |
033 | /** |
034 | * 新建文件 |
035 | * @param filePathAndName String 文件路径及名称 如c:/fqf.txt |
036 | * @param fileContent String 文件内容 |
037 | * @return boolean |
038 | */ |
039 | public static void newFile(String filePathAndName, String fileContent) { |
040 | |
041 | try { |
042 | String filePath = filePathAndName; |
043 | File myFilePath = new File(filePath); |
044 | if (!myFilePath.exists()) { |
045 | myFilePath.createNewFile(); |
046 | System.out.println( "创建文件成功" ); |
047 | } |
048 | FileWriter resultFile = new FileWriter(myFilePath); |
049 | PrintWriter myFile = new PrintWriter(resultFile); |
050 | myFile.println(fileContent); |
051 | System.out.println( "写入文件成功" ); |
052 | resultFile.close(); |
053 | } |
054 | catch (Exception e) { |
055 | System.out.println( "新建文件操作出错" ); |
056 | e.printStackTrace(); |
057 | } |
058 | } |
059 | |
060 | /** |
061 | * 删除文件 |
062 | * @param filePathAndName String 文件路径及名称 如c:/fqf.txt |
063 | * @param fileContent String |
064 | * @return boolean |
065 | */ |
066 | public static void delFile(String filePathAndName) { |
067 | try { |
068 | File myDelFile = new File(filePathAndName); |
069 | myDelFile.delete(); |
070 | System.out.println( "删除文件成功!" ); |
071 | } |
072 | catch (Exception e) { |
073 | System.out.println( "删除文件操作出错" ); |
074 | e.printStackTrace(); |
075 | } |
076 | } |
077 | |
078 | |
079 | |
080 | |
081 | /** |
082 | * 删除文件夹 |
083 | * @param filePathAndName String 文件夹路径及名称 如c:/fqf |
084 | * @param fileContent String |
085 | * @return boolean |
086 | */ |
087 | public void delFolder(String folderPath) { |
088 | try { |
089 | delAllFile(folderPath); //删除完里面所有内容 |
090 | File myFilePath = new File(folderPath); |
091 | myFilePath.delete(); //删除空文件夹 |
092 | } |
093 | catch (Exception e) { |
094 | System.out.println( "删除文件夹操作出错" ); |
095 | e.printStackTrace(); |
096 | } |
097 | } |
098 | |
099 | /** |
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 | } |