java怎么创建目录(删除/修改/复制目录及文
答案:2 悬赏:30 手机版
解决时间 2021-01-27 21:32
- 提问者网友:孤凫
- 2021-01-27 02:39
java怎么创建目录(删除/修改/复制目录及文
最佳答案
- 五星知识达人网友:纵马山川剑自提
- 2021-01-27 03:20
import java.io.*;
public class FileOperate {
public FileOperate() {
}
public 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 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 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 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 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 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 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 void moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
public void moveFolder(String oldPath, String newPath) {
copyFolder(oldPath, newPath);
delFolder(oldPath);
}
}
public class FileOperate {
public FileOperate() {
}
public 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 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 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 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 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 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 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 void moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
public void moveFolder(String oldPath, String newPath) {
copyFolder(oldPath, newPath);
delFolder(oldPath);
}
}
全部回答
- 1楼网友:走死在岁月里
- 2021-01-27 03:48
这需要导入java.io类
1. import java.io.*;
public class fileoperate {
public fileoperate() {
}
public 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();
}
}
2.public 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();
}
}
3.删除文件
public 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();
}
}
4.public 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();
}
}
5.public 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]);//再删除空文件夹
}
}
}
6.public 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();
}
}
6.public 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();
}
}
7.public void movefile(string oldpath, string newpath) {
copyfile(oldpath, newpath);
delfile(oldpath);
}
public void movefolder(string oldpath, string newpath) {
copyfolder(oldpath, newpath);
delfolder(oldpath);
}
}
java中删除目录事先要删除目录下的文件或子目录。用递归就可以实现。
public void del(string filepath) throws ioexception{
file f = new file(filepath);//定义文件路径
if(f.exists() && f.isdirectory()){//判断是文件还是目录
if(f.listfiles().length==0){//若目录下没有文件则直接删除
f.delete();
}else{//若有则把文件放进数组,并判断是否有下级目录
file delfile[]=f.listfiles();
int i =f.listfiles().length;
for(int j=0;j
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯