`
- 浏览:
663393 次
- 性别:
- 来自:
杭州
-
package
OALogic.sql.data;
import
java.io.*;
public
class
FileOperate {
public
FileOperate() {
}
public
static
void
main(String args[]){
newFolder(
"D:/100"
);
}
public
static
void
newFolder(String folderPath) {
try
{
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath =
new
java.io.File(filePath);
if
(!myFilePath.exists()) {
myFilePath.mkdir();
}
}
catch
(Exception e) {
System.out.println(
"新建目录操作出错"
);
e.printStackTrace();
}
}
public
static
void
newFile(String filePathAndName,
String fileContent) {
try
{
String filePath = filePathAndName;
filePath = filePath.toString();
File myFilePath =
new
File(filePath);
if
(!myFilePath.exists()) {
myFilePath.createNewFile();
}
FileWriter resultFile =
new
FileWriter(myFilePath);
PrintWriter myFile =
new
PrintWriter(resultFile);
String strContent = fileContent;
myFile.println(strContent);
resultFile.close();
}
catch
(Exception e) {
System.out.println(
"新建目录操作出错"
);
e.printStackTrace();
}
}
public
static
void
delFile(String filePathAndName) {
try
{
String filePath = filePathAndName;
filePath = filePath.toString();
java.io.File myDelFile =
new
java.io.File(filePath);
myDelFile.delete();
}
catch
(Exception e) {
System.out.println(
"删除文件操作出错"
);
e.printStackTrace();
}
}
public
static
void
delFolder(String folderPath) {
try
{
delAllFile(folderPath);
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath =
new
java.io.File(filePath);
myFilePath.delete();
}
catch
(Exception e) {
System.out.println(
"删除文件夹操作出错"
);
e.printStackTrace();
}
}
public
static
void
delAllFile(String path) {
File file =
new
File(path);
if
(!file.exists()) {
return
;
}
if
(!file.isDirectory()) {
return
;
}
String[] tempList = file.list();
File temp =
null
;
for
(
int
i =
0
; i < tempList.length; i++) {
if
(path.endsWith(File.separator)) {
temp =
new
File(path + tempList[i]);
}
else
{
temp =
new
File(path + File.separator + tempList[i]);
}
if
(temp.isFile()) {
temp.delete();
}
if
(temp.isDirectory()) {
delAllFile(path+
"/"
+ tempList[i]);
delFolder(path+
"/"
+ tempList[i]);
}
}
}
public
static
void
copyFile(String oldPath, String newPath) {
try
{
int
bytesum =
0
;
int
byteread =
0
;
File oldfile =
new
File(oldPath);
if
(oldfile.exists()) {
InputStream inStream =
new
FileInputStream(oldPath);
FileOutputStream fs =
new
FileOutputStream(newPath);
byte
[] buffer =
new
byte
[
1444
];
int
length;
while
( (byteread = inStream.read(buffer)) != -
1
) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer,
0
, byteread);
}
inStream.close();
}
}
catch
(Exception e) {
System.out.println(
"复制单个文件操作出错"
);
e.printStackTrace();
}
}
public
static
void
copyFolder(String oldPath, String newPath) {
try
{
(
new
File(newPath)).mkdirs();
File a=
new
File(oldPath);
String[] file=a.list();
File temp=
null
;
for
(
int
i =
0
; i < file.length; i++) {
if
(oldPath.endsWith(File.separator)){
temp=
new
File(oldPath+file[i]);
}
else
{
temp=
new
File(oldPath+File.separator+file[i]);
}
if
(temp.isFile()){
FileInputStream input =
new
FileInputStream(temp);
FileOutputStream output =
new
FileOutputStream(newPath +
"/"
+
(temp.getName()).toString());
byte
[] b =
new
byte
[
1024
*
5
];
int
len;
while
( (len = input.read(b)) != -
1
) {
output.write(b,
0
, len);
}
output.flush();
output.close();
input.close();
}
if
(temp.isDirectory()){
copyFolder(oldPath+
"/"
+file[i],
newPath+
"/"
+file[i]);
}
}
}
catch
(Exception e) {
System.out.println(
"复制整个文件夹内容操作出错"
);
e.printStackTrace();
}
}
public
static
void
moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
public
static
void
moveFolder(String oldPath, String newPath) {
copyFolder(oldPath, newPath);
delFolder(oldPath);
}
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
在 Java 中,可以使用 File 类的 delete 方法来实现文件删除。例如: `File f = new File("c:\\Autoexec.bat");` `f.delete();` 四、文件夹操作 文件夹操作包括创建文件夹、删除文件夹等。在 Java 中,可以使用 ...
在Java编程语言中,处理文本文件是常见...综上所述,这个Java程序可能是一个简单的文本编辑器,提供了基本的文件操作功能,包括创建、打开、复制和剪切文本文件。理解这些概念对于任何Java开发者来说都是基础且重要的。
综上所述,使用Java实现资源管理器涉及的知识点包括Java文件I/O操作、剪贴板API、GUI编程(Swing或JavaFX)、文件系统的遍历、线程安全以及测试技术。通过这个项目,开发者不仅可以提升编程技能,还能深入理解操作...
剪切(移动)文件涉及`renameTo()`方法,但这个方法在跨越文件系统边界时可能不工作,此时需要先复制再删除源文件。 7. **异常处理**:文件操作可能出现各种异常,如`FileNotFoundException`,`IOException`等,...
在本项目中,"JavaFile文件操作"着重介绍了如何使用`File`类进行文件的基本操作,包括创建文件、复制文件或文件夹、删除文件或文件夹以及剪切文件或文件夹。以下是对这些功能的详细说明: 1. **创建文件** 使用`...
Java中的`File`类是处理文件和目录的基本工具,它提供了丰富的API来创建、读取、写入、删除以及管理文件和目录。在Java中,字节流是用于处理二进制数据,如图片、音频、视频等,而字符流则用于处理文本数据。在本...
这个小程序旨在提供与Windows资源管理器相似的用户体验,让用户能够在Java环境下方便地浏览、创建、复制、移动、删除和重命名文件及目录。 首先,我们需要理解Java的基础知识。Java是一种广泛使用的面向对象的编程...
在Java编程语言中,`File`类是处理文件和目录的核心工具,它位于`java.io`包中。`File`类提供了丰富的API来创建、删除、重命名文件以及管理文件和目录的属性。以下是对`File`类常用方法的详细说明: 1. **创建文件...
4. **文件操作**:支持常见的文件操作,如复制、剪切、粘贴、重命名、删除和移动。 5. **同步与备份**:能自动或手动将文件同步到云存储服务,或在多台设备之间进行备份。 6. **文件分享**:便于通过电子邮件、...
它涉及到对文件的创建、读取、写入、复制、移动、删除以及压缩和解压缩等操作。以下将详细讲解这些知识点: 1. 文件创建与删除: 使用`java.io.File`类可以创建新的文件或目录。`File`对象代表一个文件路径名或者...
9. **剪贴板操作**:Java的Clipboard类支持复制、剪切和粘贴操作,使得文件和文件夹可以在不同的位置之间移动或复制。 10. **国际化和本地化**:为了适应不同地区的用户,资源管理器可能实现了国际化(i18n)和本地...
3. **文件操作**: 实现文件的复制、剪切、查看属性和新建文件操作,我们需要使用到Java的`java.io`和`java.nio`包。例如,`File`类提供了读取、写入、创建、删除文件的基本操作;`Files`类提供了更高级的文件和目录...
5. **剪切/删除**:删除选中的文本或将其移动到剪贴板。 6. **查找/替换**:在文档中搜索特定文本并替换为其他内容。 7. **字体样式**:更改文本的字体、大小、颜色等样式。 8. **撤销/重做**:提供撤销和重做功能,...
在编程实现上,项目采用Java语言,利用其丰富的类库如`JFileChooser`进行文件操作,通过异常处理机制(如`try-catch`块)来处理可能出现的错误,确保程序的稳定性和健壮性。例如,在打开或保存文件时,如果发生异常...
在Android平台上,开发一个文件管理器是一个常见的任务,它允许用户查看、操作和管理设备上的文件和文件夹。本文将详细讲解如何实现一个基础的Android文件管理器,涵盖文件夹浏览、文件拷贝复制等功能。 首先,我们...
本文将围绕“Android文件系统浏览器”这一主题,深入探讨其功能、工作原理以及如何实现对SDCard的文件浏览。 首先,我们要理解Android文件系统的结构。Android采用Linux内核,因此其文件系统与传统的Linux文件系统...
【标题解析】:“WEB在线文件管理 WEB在线文件管理”这个标题明确指出了我们要讨论的核心主题,即通过Web界面实现对文件和文件夹的管理和操作。这通常涉及到一个基于Web的文件管理系统,允许用户通过浏览器进行文件...