導航:首頁 > 編程語言 > java文件夾操作

java文件夾操作

發布時間:2023-01-17 13:03:13

java遍歷指定文件夾下的所有子文件夾怎麼操作

import java.io.File ;
import java.io.IOException ;
public class FileDemo11{
public static void main(String args[]){
File my = new File("d:" + File.separator) ;// 操作路徑,可以有外部參數決定的
print(my) ;
}
public static void print(File file){// 遞歸調用
if(file!=null){// 判斷對象是否為空
if(file.isDirectory()){// 如果是目錄
File f[] = file.listFiles() ;// 列出全部的文件
if(f!=null){// 判斷此目錄能否列出
for(int i=0;i<f.length;i++){
print(f[i]) ;// 因為給的路徑有可能是目錄,所以,繼續判斷
}
}
}else{
System.out.println(file) ;// 輸出路徑
}
}
}
};

⑵ Java怎麼移動文件夾里的文件到指定文件

是的,用File類的renameTo方法即可,注意目標文件名一定要合法,否則失敗!

/**
* 移動文件到指定目錄
*
* @param oldPath
* String 如:c:/fqf.txt
* @param newPath
* String 如:d:/fqf.txt
*/
public static void moveFile(String oldPath, String newPath) {
File(oldPath, newPath);
delFile(oldPath);

}

/**
* 移動文件到指定目錄
*
* @param oldPath
* String 如:c:/fqf.txt
* @param newPath
* String 如:d:/fqf.txt
*/
public static void moveFolder(String oldPath, String newPath) {
Folder(oldPath, newPath);
delFolder(oldPath);
}

⑶ java怎樣保存打開就可執行的文件

會生成.

  1. class文件,只能用反編譯軟體看,在你的eclipse工作空間,一般是workspace下的工程中,路徑時你自己指定的.

  2. 編譯輸出路徑的默認位置,普通工程:bin,web工程:WEB-INF/classes,maven工程:target/classes.

⑷ Java File的操作,復制文件夾的方法!

你寫的兩個程序都不太嚴謹,我給你寫一個復制文件和復制文件夾的標準例子吧。
//復制文件
package com.cdd.file;
import java.io.*;
public class FileText {
public static void main(String[] args) {
String sfpath = "E://word.txt"; // 指定文件地址
String dfpath = "F://word.txt";
File sFile = new File(sfpath); // 創建文件對象
File dFile = new File(dfpath);
try {
dFile.createNewFile(); // 新建文件
FileInputStream fis = new FileInputStream(sFile);
FileOutputStream fout = new FileOutputStream(dFile);
byte[] date = new byte[512]; // 創建位元組數組
int rs = -1;
while ((rs = fis.read(date)) > 0) { // 循環讀取文件
fout.write(date, 0, rs); // 向文件寫數據
}
fout.close();
fis.close(); // 關閉流
System.out.println("文件復製成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}

//復制文件夾
package com.cdd.util;
import java.io.*;
public class FileUtil {
private static void (File[] files, File d) {
if (!d.exists()) //如果指定目錄不存在
d.mkdir(); //創建目錄
for (int i = 0; i < files.length; i++) { //循環遍歷要復制的文件夾
if (files[i].isFile()) { //如果文件夾中是文件
try {
FileInputStream fis = new FileInputStream(files[i]); //創建FileInputStream對象
FileOutputStream out = new FileOutputStream(new File(d
.getPath()
+ File.separator + files[i].getName())); //復制後文件的保存路徑
int count = fis.available();
byte[] data = new byte[count];
if ((fis.read(data)) != -1) { //讀取文件
out.write(data); //將讀取的信息寫入文件中
}
out.close(); //關閉流
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (files[i].isDirectory()) { //如果文件夾中是一個路徑
File des = new File(d.getPath() + File.separator
+ files[i].getName()); //在復制後路徑中創建子文件夾
des.mkdir();
(files[i].listFiles(), des); //再次調用本方法
}
}
System.out.println("文件夾復製成功");
}
public static void main(String[] args) {
File sourFile = null, desFile = null;
String sourFolder = "E://word"; //指定要進行復制的文件夾
String desFolder = "E://"; //指定復制後的文件夾地址
sourFile = new File(sourFolder);
if (!sourFile.isDirectory() || !sourFile.exists()) { //如果原文件夾不存在
System.out.println("原文件夾不存在");
}
desFile = new File(desFolder);
desFile.mkdir();
(sourFile.listFiles(), desFile); //調用文件夾復制方法
}
}

⑸ javaweb上傳時手動創建存放文件的文件夾應該怎麼做

上傳保存操作是在後台Java代碼里處理的。

你在保存前使用Java代碼在伺服器上創建文件夾就好了啊。

Filedir=newFile("要創建的文件夾的位置");
dir.mkdirs();

你需要先獲得工程當前路徑,然後自己拼文件夾的位置就好了。如何獲得當前工程的路徑就和你是否使用了框架有關系了,一些框架提供了快捷獲取。

比如最基本的Servlet獲取:在servlet中獲得項目路徑

⑹ 如何使用java壓縮文件夾成為zip包

在JDK中有一個zip工具類:

java.util.zip Provides classes for reading and writing the standard ZIP and
GZIP file formats.

使用此類可以將文件夾或者多個文件進行打包壓縮操作。

在使用之前先了解關鍵方法:

ZipEntry(String name) Creates a new zip entry with the specified name.

使用ZipEntry的構造方法可以創建一個zip壓縮文件包的實例,然後通過ZipOutputStream將待壓縮的文件以流的形式寫進該壓縮包中。具體實現代碼如下:

importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipOutputStream;
/**
*將文件夾下面的文件
*打包成zip壓縮文件
*
*@authoradmin
*
*/
publicfinalclassFileToZip{

privateFileToZip(){}

/**
*將存放在sourceFilePath目錄下的源文件,打包成fileName名稱的zip文件,並存放到zipFilePath路徑下
*@paramsourceFilePath:待壓縮的文件路徑
*@paramzipFilePath:壓縮後存放路徑
*@paramfileName:壓縮後文件的名稱
*@return
*/
publicstaticbooleanfileToZip(StringsourceFilePath,StringzipFilePath,StringfileName){
booleanflag=false;
FilesourceFile=newFile(sourceFilePath);
FileInputStreamfis=null;
BufferedInputStreambis=null;
FileOutputStreamfos=null;
ZipOutputStreamzos=null;

if(sourceFile.exists()==false){
System.out.println("待壓縮的文件目錄:"+sourceFilePath+"不存在.");
}else{
try{
FilezipFile=newFile(zipFilePath+"/"+fileName+".zip");
if(zipFile.exists()){
System.out.println(zipFilePath+"目錄下存在名字為:"+fileName+".zip"+"打包文件.");
}else{
File[]sourceFiles=sourceFile.listFiles();
if(null==sourceFiles||sourceFiles.length<1){
System.out.println("待壓縮的文件目錄:"+sourceFilePath+"裡面不存在文件,無需壓縮.");
}else{
fos=newFileOutputStream(zipFile);
zos=newZipOutputStream(newBufferedOutputStream(fos));
byte[]bufs=newbyte[1024*10];
for(inti=0;i<sourceFiles.length;i++){
//創建ZIP實體,並添加進壓縮包
ZipEntryzipEntry=newZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//讀取待壓縮的文件並寫進壓縮包里
fis=newFileInputStream(sourceFiles[i]);
bis=newBufferedInputStream(fis,1024*10);
intread=0;
while((read=bis.read(bufs,0,1024*10))!=-1){
zos.write(bufs,0,read);
}
}
flag=true;
}
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}catch(IOExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}finally{
//關閉流
try{
if(null!=bis)bis.close();
if(null!=zos)zos.close();
}catch(IOExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}
}
}
returnflag;
}

publicstaticvoidmain(String[]args){
StringsourceFilePath="D:\TestFile";
StringzipFilePath="D:\tmp";
StringfileName="12700153file";
booleanflag=FileToZip.fileToZip(sourceFilePath,zipFilePath,fileName);
if(flag){
System.out.println("文件打包成功!");
}else{
System.out.println("文件打包失敗!");
}
}

}

⑺ java 如何打開一個文件夾

給你一段文件操作的例子

package com.file.sample;

import java.io.*;

public class FileOperate {
public FileOperate() {
}

/**
* 新建目錄
*
* @param folderPath
* String 如 c:/fqf
* @return boolean
*/
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();
}
}

/**
* 新建文件
*
* @param filePathAndName
* String 文件路徑及名稱 如c:/fqf.txt
* @param fileContent
* String 文件內容
* @return boolean
*/
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();

}

}

/**
* 刪除文件
*
* @param filePathAndName
* String 文件路徑及名稱 如c:/fqf.txt
* @param fileContent
* String
* @return boolean
*/
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();

}

}

/**
* 刪除文件夾
*
* @param filePathAndName
* String 文件夾路徑及名稱 如c:/fqf
* @param fileContent
* String
* @return boolean
*/
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();

}

}

/**
* 刪除文件夾裡面的所有文件
*
* @param path
* String 文件夾路徑 如 c:/fqf
*/
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]);// 再刪除空文件夾
}
}
}

/**
* 復制單個文件
*
* @param oldPath
* String 原文件路徑 如:c:/fqf.txt
* @param newPath
* String 復制後路徑 如:f:/fqf.txt
* @return boolean
*/
public void File(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();

}

}

/**
* 復制整個文件夾內容
*
* @param oldPath
* String 原文件路徑 如:c:/fqf
* @param newPath
* String 復制後路徑 如:f:/fqf/ff
* @return boolean
*/
public void Folder(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()) {// 如果是子文件夾
Folder(oldPath + "/" + file[i], newPath + "/" + file[i]);
}
}
} catch (Exception e) {
System.out.println("復制整個文件夾內容操作出錯");
e.printStackTrace();

}

}

/**
* 移動文件到指定目錄
*
* @param oldPath
* String 如:c:/fqf.txt
* @param newPath
* String 如:d:/fqf.txt
*/
public void moveFile(String oldPath, String newPath) {
File(oldPath, newPath);
delFile(oldPath);

}

/**
* 移動文件到指定目錄
*
* @param oldPath
* String 如:c:/fqf.txt
* @param newPath
* String 如:d:/fqf.txt
*/
public void moveFolder(String oldPath, String newPath) {
Folder(oldPath, newPath);
delFolder(oldPath);

}

public static void main(String[] args){
FileOperate filedemo=new FileOperate();
filedemo.delAllFile("d:/test");
}
}

閱讀全文

與java文件夾操作相關的資料

熱點內容
voc文件夾 瀏覽:862
租廣東聯通伺服器注意什麼雲空間 瀏覽:932
javascript高級程序設計pdf 瀏覽:289
pwm單片機原理 瀏覽:346
ai演算法在線修復圖片 瀏覽:979
scratch編程中如何做射擊游戲 瀏覽:476
at89c51編程器 瀏覽:341
項目經理叫醒程序員 瀏覽:342
autocad旋轉命令 瀏覽:660
手機版wpsoffice怎麼打包文件夾 瀏覽:579
在成都學車用什麼app 瀏覽:818
grep命令管道 瀏覽:426
java修改重啟 瀏覽:567
單片機供電方案 瀏覽:770
airpodspro一代怎麼連接安卓 瀏覽:218
豌豆莢app上有什麼游戲 瀏覽:283
公路商店app標簽選什麼 瀏覽:339
linuxoracle命令行登錄 瀏覽:227
android深度休眠 瀏覽:173
php微信開發例子 瀏覽:846