FIle file = new File("/image/123.jpg");

成都創(chuàng)新互聯(lián)公司自2013年起,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元八步做網(wǎng)站,已為上家服務(wù),為八步各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
if (file.exists()){
file.delete();
}
使用File對(duì)象操作刪除,會(huì)判斷是否存在,如存在就刪了。
如果想找路徑,使用File類的getAbsolutePath()方/法就能得到/絕/對(duì)/路/徑/的字符串表示。
例如上面的對(duì)、象file,使用
String str = file.getAbsolutePath();
System.out.println(str);
你在/控/制/臺(tái)co/ns/ole/窗口就能看到了。
File
F=new
File(路徑);/通過(guò)將給定路徑名字符串轉(zhuǎn)換為抽象路徑名來(lái)創(chuàng)建一個(gè)新
File
實(shí)例。
F.delete();//刪除此抽象路徑名表示的文件或目錄。
文件的移動(dòng)的話,得通過(guò)輸入輸出流
FileInputStream
FI=new
FileInputStream(F);
FileOutputStream
FO=new
FileOutputStream(F);
wile(FI.read()!=EOF)
{
FO.write();
}
我覺(jué)得一樓說(shuō)的對(duì),我剛才試了下,就是這個(gè)原因,你可以參看我寫(xiě)的一個(gè)程序:
========================Code begin ==========================
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author limingatcn
* @time Jan 22, 2009 10:11:59 AM
* @version 1.0
*/
public class TestDelete {
public void readInfoFromFile() {
// 先聲明以備用
File file = new File("c:\\test.txt");
FileInputStream fis = null;
byte[] b = null;
int i = 0;
// 處理
try {
fis = new FileInputStream(file);
// byte數(shù)組不要越界
b = new byte[123456];
while (fis.available() 0) {
i += fis.read(b);
}
String str = new String(b, 0, i);
System.out.println(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 執(zhí)行刪除操作
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
file.delete();
System.out.println("The file was removed.");
}
}
/**
* @param args
*/
public static void main(String[] args) {
new TestDelete().readInfoFromFile();
}
}
========================Code end ============================
在finally中要執(zhí)行 關(guān)閉你所打開(kāi)讀流的對(duì)象 的操作,
在這個(gè)程序中如果沒(méi)有finally中的fis.close()
test.txt就出現(xiàn)你說(shuō)的刪除不掉的情況
結(jié)合這個(gè)程序你再試試。
1、創(chuàng)建File對(duì)象File(String pathname):pathname錄入時(shí)錄入的路徑字符串需要注意/和\的運(yùn)用,但是由于java開(kāi)發(fā)出的程序需要運(yùn)用到不同的系統(tǒng)上,因此,一般以File.separator來(lái)代替。
2、創(chuàng)建File對(duì)象File(File parent,String child):此為創(chuàng)建文件對(duì)象的另外一種方式,parent為已創(chuàng)建的對(duì)象,這種情況,parent一般為文件夾的路徑,child為文件的名稱。
3、判斷File對(duì)象是否是一個(gè)文件:isFile()返回值true為文件,false為非文件。如下程序中,由于file1對(duì)象為文件夾,非文件,返回為false;file2對(duì)象為文件,返回true。
4、文件的內(nèi)容長(zhǎng)度(占用字節(jié)):length()當(dāng)前File對(duì)象所表示的文件所占用的字節(jié)量。在testFile1.txt中事先錄入部分?jǐn)?shù)據(jù),file2指向的File對(duì)象,內(nèi)容長(zhǎng)度為8。
5、判斷文件是否存在,在硬盤(pán)創(chuàng)建文件前,一般需要判斷硬盤(pán)中文件是否存在,如果不存在才創(chuàng)建文件,這樣可以避免文件重復(fù)創(chuàng)建。
6、文件創(chuàng)建:createNewFile()當(dāng)File對(duì)象調(diào)用文件創(chuàng)建方法后,硬盤(pán)中文件才會(huì)被創(chuàng)建。
一個(gè)JAVA 實(shí)現(xiàn)FTP功能的代碼,包括了服務(wù)器的設(shè)置模塊,并包括有上傳文件至FTP的通用方法、下載文件的通用方法以及刪除文件、在ftp服務(wù)器上傳文件夾、檢測(cè)文件夾是否存在等,里面的有些代碼對(duì)編寫(xiě)JAVA文件上傳或許有參考價(jià)值,
(1):Java FTP主文件代碼:
package ftpDemo;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.;
public class ftpUtil {
// 上傳文件至FTP通用方法
public static void upLoadFileFtp(KmConfig kmConfig,InputStream is, String fileName){
try {
String ftpHost = kmConfig.getFtpHost();
int port = kmConfig.getFtpPort();
String userName = kmConfig.getFtpUser();
String passWord = kmConfig.getFtpPassword();
String path = kmConfig.getFtpPath();
FtpClient ftpClient = new FtpClient(ftpHost, port);// ftpHost為FTP服務(wù)器的IP地址,port為FTP服務(wù)器的登陸端口,ftpHost為String型,port為int型。
ftpClient.login(userName, passWord);// userName、passWord分別為FTP服務(wù)器的登陸用戶名和密碼
ftpClient.binary();
ftpClient.cd(path);// path為FTP服務(wù)器上保存上傳文件的路徑。
TelnetOutputStream telnetOut = ftpClient.put(fileName);// fileName為上傳的文件名
DataOutputStream dataOut = new DataOutputStream(telnetOut);
byte buffer[] = new byte[ * ];
int count = ;
while ((count = is.read(buffer)) != -) {
dataOut.write(buffer, , count);
}
telnetOut.close();
dataOut.close();
ftpClient.closeServer();
} catch (Exception e) {
System.out.println("上傳文件失敗!請(qǐng)檢查系統(tǒng)FTP設(shè)置,并確認(rèn)FTP服務(wù)啟動(dòng)");
}
}
// 刪除文件至FTP通用方法
public static void deleteFileFtp(KmConfig kmConfig,String fileName){
try {
String ftpHost = kmConfig.getFtpHost();
int port = kmConfig.getFtpPort();
String userName = kmConfig.getFtpUser();
String passWord = kmConfig.getFtpPassword();
String path = kmConfig.getFtpPath();
FtpClient ftpClient = new FtpClient(ftpHost, port);// ftpHost為FTP服務(wù)器的IP地址,port為FTP服務(wù)器的登陸端口,ftpHost為String型,port為int型。
ftpClient.login(userName, passWord);// userName、passWord分別為FTP服務(wù)器的登陸用戶名和密碼
ftpClient.binary();
ftpClient.cd(path);// path為FTP服務(wù)器上保存上傳文件的路徑。
try {
ftpClient.sendServer("dele " + fileName + "\r\n");
} catch (Exception e) {
System.out.println("刪除文件失敗!請(qǐng)檢查系統(tǒng)FTP設(shè)置,并確認(rèn)FTP服務(wù)啟動(dòng)");
}
ftpClient.closeServer();
} catch (Exception e) {
System.out.println("刪除文件失敗!");
}
}
// 下載ftp文件
public static void downloadFileFtp(KmConfig kmConfig,String fileName, String clientFileName, OutputStream outputStream){
try {
String ftpHost = kmConfig.getFtpHost();
int port = kmConfig.getFtpPort();
String userName = kmConfig.getFtpUser();
String passWord = kmConfig.getFtpPassword();
String path = kmConfig.getFtpPath();
FtpClient ftpClient = new FtpClient(ftpHost, port);// ftpHost為FTP服務(wù)器的IP地址,port為FTP服務(wù)器的登陸端口,ftpHost為String型,port為int型。
ftpClient.login(userName, passWord);// userName、passWord分別為FTP服務(wù)器的登陸用戶名和密碼
ftpClient.binary();
ftpClient.cd(path);// path為FTP服務(wù)器上保存上傳文件的路徑。
try {
TelnetInputStream in = ftpClient.get(fileName);
byte[] bytes = new byte[];
int cnt=;
while ((cnt=in.read(bytes,,bytes.length)) != -) {
outputStream.write(bytes, , cnt);
}
outputStream.close();
in.close();
} catch (Exception e) {
ftpClient.closeServer();
e.printStackTrace();
}
ftpClient.closeServer();
} catch (Exception e) {
System.out.println("下載文件失敗!請(qǐng)檢查系統(tǒng)FTP設(shè)置,并確認(rèn)FTP服務(wù)啟動(dòng)");
}
}
//在ftp服務(wù)器上傳件文件夾
public boolean createDir(String path,FtpClient ftpClient) throws Exception{
//進(jìn)入到home文件夾下
ftpClient.cd("/home");
//創(chuàng)建遠(yuǎn)程文件夾
//遠(yuǎn)程命令包括
//USER ?PORT ?RETR ?ALLO ?DELE ?SITE ?XMKD ?CDUP ?FEATbr
// ? ? PASS ?PASV ?STOR ?REST ?CWD ? STAT ?RMD ? XCUP ?OPTSbr
// ? ? ACCT ?TYPE ?APPE ?RNFR ?XCWD ?HELP ?XRMD ?STOU ?AUTHbr
// ? ? REIN ?STRU ?SMNT ?RNTO ?LIST ?NOOP ?PWD ? SIZE ?PBSZbr
// ? ? QUIT ?MODE ?SYST ?ABOR ?NLST ?MKD ? XPWD ?MDTM ?PROTbr
// ? ? ? ?在服務(wù)器上執(zhí)行命令,如果用sendServer來(lái)執(zhí)行遠(yuǎn)程命令(不能執(zhí)行本地FTP命令)的話,所有FTP命令都要加上/r/nbr
// ? ? ? ? ?ftpclient.sendServer("XMKD /test/bb/r/n"); //執(zhí)行服務(wù)器上的FTP命令br
// ? ? ? ? ?ftpclient.readServerResponse一定要在sendServer后調(diào)用br
// ? ? ? ? ?nameList("/test")獲取指目錄下的文件列表br
// ? ? ? ? ?XMKD建立目錄,當(dāng)目錄存在的情況下再次創(chuàng)建目錄時(shí)報(bào)錯(cuò)br
// ? ? ? ? ?XRMD刪除目錄br
// ? ? ? ? ?DELE刪除文件br
//通過(guò)遠(yuǎn)程命令 穿件一個(gè)files文件夾
ftpClient.sendServer("MKD "+ path + "\r\n");
//這個(gè)方法必須在 這兩個(gè)方法中間調(diào)用 否則 命令不管用
ftpClient.binary();
ftpClient.readServerResponse();
return false;
}
/**
* 檢查文件夾是否存在
* @param dir
* @param ftpClient
* @return
*/
public boolean isDirExist(String dir, FtpClient ftpClient) {
try {
ftpClient.cd(dir);
} catch (Exception e) {
return false;
}
return true;
}
}
(2):KmConfig.java代碼如下:定義FTP服務(wù)器參數(shù),包括登錄的用戶名密碼之類的。
package ftpDemo;
public class KmConfig {
//主機(jī)ip
private String FtpHost = "";
//端口號(hào)
private int FtpPort;
//ftp用戶名
private String FtpUser = "";
//ftp密碼
private String FtpPassword = "";
//ftp中的目錄
private String FtpPath = "";
public String getFtpHost() {
return FtpHost;
}
public void setFtpHost(String ftpHost) {
FtpHost = ftpHost;
}
public int getFtpPort() {
return FtpPort;
}
public void setFtpPort(int ftpPort) {
FtpPort = ftpPort;
}
public String getFtpUser() {
return FtpUser;
}
public void setFtpUser(String ftpUser) {
FtpUser = ftpUser;
}
public String getFtpPassword() {
return FtpPassword;
}
public void setFtpPassword(String ftpPassword) {
FtpPassword = ftpPassword;
}
public String getFtpPath() {
return FtpPath;
}
public void setFtpPath(String ftpPath) {
FtpPath = ftpPath;
}
}
(3):下面是測(cè)試代碼:
1、如果只是想要文件中的內(nèi)容,可以使用如下代碼:
FileOutputStream?fs?=?new?FileOutputStream(new?File("C:\\buyterms.txt"));
2、如果是想要文件夾中的內(nèi)容,可以使用如下代碼:
package?com.xx;??
import?java.io.File;??
public?class?Test?{??
public?static?void?main(String[]?args)?{??
String?fileRoot?=?"C:/Users/xx/Desktop/xx/xxx";??
delFolder(fileRoot);??
System.out.println("deleted");??
}??
//??//?刪除完文件后刪除文件夾??
//??//?param?folderPath?文件夾完整絕對(duì)路徑??
public?static?void?delFolder(String?folderPath)?{??
try?{??
delAllFile(folderPath);?//?刪除完里面所有內(nèi)容??
//不想刪除文佳夾隱藏下面??
//??????????String?filePath?=?folderPath;??
//??????????filePath?=?filePath.toString();??
//??????????java.io.File?myFilePath?=?new?java.io.File(filePath);??
//??????????myFilePath.delete();?//?刪除空文件夾??
}?catch?(Exception?e)?{??
e.printStackTrace();??
}??
}??
//?刪除指定文件夾下所有文件??
//?param?path?文件夾完整絕對(duì)路徑??
public?static?boolean?delAllFile(String?path)?{??
boolean?flag?=?false;??
File?file?=?new?File(path);??
if?(!file.exists())?{??
return?flag;??
}??
if?(!file.isDirectory())?{??
return?flag;??
}??
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]);//?再刪除空文件夾??
flag?=?true;??
}??
}??
return?flag;??
}??
}
網(wǎng)站欄目:java上傳刪除文件代碼,java代碼刪除文件夾
瀏覽路徑:http://chinadenli.net/article29/dseppch.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開(kāi)發(fā)、網(wǎng)站內(nèi)鏈、用戶體驗(yàn)、虛擬主機(jī)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)