JAVA对文件和文件夹的操作

原文链接

[代码] [Java]代码

001import java.io.File;
002 
003import java.io.FileInputStream;
004import java.io.FileOutputStream;
005import java.io.FileWriter;
006import java.io.InputStream;
007import java.io.PrintWriter;
008 
009public class FileOperate {
010    public FileOperate() {
011    }
012 
013    /**
014     * 新建目录
015     *
016     * @param folderPath
017     *            String 如 c:/fqf
018     * @return boolean
019     */
020    public void newFolder(String folderPath) {
021        try {
022            String filePath = folderPath;
023            filePath = filePath.toString();
024            java.io.File myFilePath = new java.io.File(filePath);
025            if (!myFilePath.exists()) {
026                myFilePath.mkdir();
027            }
028        } catch (Exception e) {
029            System.out.println("新建目录操作出错");
030            e.printStackTrace();
031        }
032    }
033 
034    /**
035     * 新建文件
036     *
037     * @param filePathAndName
038     *            String 文件路径及名称 如c:/fqf.txt
039     * @param fileContent
040     *            String 文件内容
041     * @return boolean
042     */
043    public void newFile(String filePathAndName, String fileContent) {
044 
045        try {
046            String filePath = filePathAndName;
047            filePath = filePath.toString();
048            File myFilePath = new File(filePath);
049            if (!myFilePath.exists()) {
050                myFilePath.createNewFile();
051            }
052            FileWriter resultFile = new FileWriter(myFilePath);
053            PrintWriter myFile = new PrintWriter(resultFile);
054            String strContent = fileContent;
055            myFile.println(strContent);
056            resultFile.close();
057 
058        } catch (Exception e) {
059            System.out.println("新建目录操作出错");
060            e.printStackTrace();
061 
062        }
063 
064    }
065 
066    /**
067     * 删除文件
068     *
069     * @param filePathAndName
070     *            String 文件路径及名称 如c:/fqf.txt
071     * @param fileContent
072     *            String
073     * @return boolean
074     */
075    public void delFile(String filePathAndName) {
076        try {
077            String filePath = filePathAndName;
078            filePath = filePath.toString();
079            java.io.File myDelFile = new java.io.File(filePath);
080            myDelFile.delete();
081 
082        } catch (Exception e) {
083            System.out.println("删除文件操作出错");
084            e.printStackTrace();
085 
086        }
087 
088    }
089 
090    /**
091     * 删除文件夹
092     *
093     * @param filePathAndName
094     *            String 文件夹路径及名称 如c:/fqf
095     * @param fileContent
096     *            String
097     * @return boolean
098     */
099    public void delFolder(String folderPath) {
100        try {
101            delAllFile(folderPath); // 删除完里面所有内容
102            String filePath = folderPath;
103            filePath = filePath.toString();
104            java.io.File myFilePath = new java.io.File(filePath);
105            myFilePath.delete(); // 删除空文件夹
106 
107        } catch (Exception e) {
108            System.out.println("删除文件夹操作出错");
109            e.printStackTrace();
110 
111        }
112 
113    }
114 
115    /**
116     * 删除文件夹里面的所有文件
117     *
118     * @param path
119     *            String 文件夹路径 如 c:/fqf
120     */
121    public void delAllFile(String path) {
122        File file = new File(path);
123        if (!file.exists()) {
124            return;
125        }
126        if (!file.isDirectory()) {
127            return;
128        }
129        String[] tempList = file.list();
130        File temp = null;
131        for (int i = 0; i < tempList.length; i++) {
132            if (path.endsWith(File.separator)) {
133                temp = new File(path + tempList[i]);
134            } else {
135                temp = new File(path + File.separator + tempList[i]);
136            }
137            if (temp.isFile()) {
138                temp.delete();
139            }
140            if (temp.isDirectory()) {
141                delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
142                delFolder(path + "/" + tempList[i]);// 再删除空文件夹
143            }
144        }
145    }
146 
147    /**
148     * 复制单个文件
149     *
150     * @param oldPath
151     *            String 原文件路径 如:c:/fqf.txt
152     * @param newPath
153     *            String 复制后路径 如:f:/fqf.txt
154     * @return boolean
155     */
156    public void copyFile(String oldPath, String newPath) {
157        try {
158            int bytesum = 0;
159            int byteread = 0;
160            File oldfile = new File(oldPath);
161            if (oldfile.exists()) { // 文件存在时
162                InputStream inStream = new FileInputStream(oldPath); // 读入原文件
163                FileOutputStream fs = new FileOutputStream(newPath);
164                byte[] buffer = new byte[1444];
165                int length;
166                while ((byteread = inStream.read(buffer)) != -1) {
167                    bytesum += byteread; // 字节数 文件大小
168                    System.out.println(bytesum);
169                    fs.write(buffer, 0, byteread);
170                }
171                inStream.close();
172            }
173        } catch (Exception e) {
174            System.out.println("复制单个文件操作出错");
175            e.printStackTrace();
176 
177        }
178 
179    }
180 
181    /**
182     * 复制整个文件夹内容
183     *
184     * @param oldPath
185     *            String 原文件路径 如:c:/fqf
186     * @param newPath
187     *            String 复制后路径 如:f:/fqf/ff
188     * @return boolean
189     */
190    public void copyFolder(String oldPath, String newPath) {
191 
192        try {
193            (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
194            File a = new File(oldPath);
195            String[] file = a.list();
196            File temp = null;
197            for (int i = 0; i < file.length; i++) {
198                if (oldPath.endsWith(File.separator)) {
199                    temp = new File(oldPath + file[i]);
200                } else {
201                    temp = new File(oldPath + File.separator + file[i]);
202                }
203 
204                if (temp.isFile()) {
205                    FileInputStream input = new FileInputStream(temp);
206                    FileOutputStream output = new FileOutputStream(newPath
207                            + "/" + (temp.getName()).toString());
208                    byte[] b = new byte[1024 * 5];
209                    int len;
210                    while ((len = input.read(b)) != -1) {
211                        output.write(b, 0, len);
212                    }
213                    output.flush();
214                    output.close();
215                    input.close();
216                }
217                if (temp.isDirectory()) {// 如果是子文件夹
218                    copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
219                }
220            }
221        } catch (Exception e) {
222            System.out.println("复制整个文件夹内容操作出错");
223            e.printStackTrace();
224 
225        }
226 
227    }
228 
229    /**
230     * 移动文件到指定目录
231     *
232     * @param oldPath
233     *            String 如:c:/fqf.txt
234     * @param newPath
235     *            String 如:d:/fqf.txt
236     */
237    public void moveFile(String oldPath, String newPath) {
238        copyFile(oldPath, newPath);
239        delFile(oldPath);
240 
241    }
242 
243    /**
244     * 移动文件到指定目录
245     *
246     * @param oldPath
247     *            String 如:c:/fqf.txt
248     * @param newPath
249     *            String 如:d:/fqf.txt
250     */
251    public void moveFolder(String oldPath, String newPath) {
252        copyFolder(oldPath, newPath);
253        delFolder(oldPath);
254 
255    }
256}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值