设为首页 加入收藏

TOP

java file 文件操作 operate file of java (二)
2014-11-24 07:23:28 】 浏览:5626
Tags:java file 文件 操作 operate
ut.println("新建文件操作出错");
113 e.printStackTrace();
114 }
115 }
116
117 /**
118 * 删除一个文件
119 *
120 * @param filePath
121 * 要删除文件的绝对路径 如:c:\\hongten\\Hello.txt
122 */
123 public void deleteFile(String filePath) {
124 try {
125 File preDelFile = new File(filePath);
126 if (preDelFile.exists()) {
127 preDelFile.delete();
128 } else {
129 System.out.println(filePath + "不存在!");
130 }
131 } catch (Exception e) {
132 System.out.println("删除文件操作出错");
133 e.printStackTrace();
134 }
135 }
136
137 /**
138 * 删除一个文件夹
139 *
140 * @param folderPath
141 * 要删除的文件夹的绝对路径 如:c:\\hongten
142 */
143 public void deleteFolder(String folderPath) {
144 try {
145 delAllFiles(folderPath);
146 File preDelFoder = new File(folderPath);
147 if (preDelFoder.exists()) {
148 preDelFoder.delete();
149 } else {
150 System.out.println(folderPath + "不存在!");
151 }
152 } catch (Exception e) {
153 System.out.println("删除文件操作出错");
154 e.printStackTrace();
155 }
156 }
157
158 /**
159 * 删除一个文件夹下的所有文件
160 *
161 * @param folderPath
162 * 要删除的文件夹的绝对路径 如:c:\\hongten
163 */
164 public void delAllFiles(String folderPath) {
165 File file = new File(folderPath);
166 if (!file.exists()) {
167 return;
168 }
169 if (!file.isDirectory()) {
170 return;
171 }
172 String[] tempList = file.list();
173 File temp = null;
174 for (int i = 0; i < tempList.length; i++) {
175 if (folderPath.endsWith(File.separator)) {
176 temp = new File(folderPath + tempList[i]);
177 } else {
178 temp = new File(folderPath + File.separator + tempList[i]);
179 }
180 if (temp.isFile()) {
181 temp.delete();
182 }
183 if (temp.isDirectory()) {
184 delAllFiles(folderPath + "/" + tempList[i]);// 先删除文件夹里面的文件
185 deleteFolder(folderPath + "/" + tempList[i]);// 再删除空文件夹
186 }
187 }
188 }
189
190 /**
191 * 单个文件的复制
192 *
193 * @param oldPath
194 * 原路径 如:c:\\Hello.java
195 * @param newPath
196 * 新路径 如:f:\\Hello.java
197 */
198 public void copyFile(String oldPath, String newPath) {
199 try {
200 int bytesum = 0;
201 int byteread = 0;
202 File oldfile = new File(oldPath);
203 if (oldfile.exists()) { // 文件存在时
204 InputStream inStream = new FileInputStream(oldPath); // 读入原文件
205 FileOutputStream fs = new FileOutputStream(newPath);
206 byte[] buffer = new byte[1444];
207 // int length = 0;
208 while ((byteread = inStream.read(buffer)) != -1) {
209 bytesum += byteread; // 字节数 文件大小
210 fs.write(buffer, 0, byteread);
211 }
212 inStream.close();
213 }
214 } catch (Exception e) {
215 S
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇三步学会Java Socket编程(三) 下一篇Java equals()和hashCode()的作用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目