① java 怎麼遍歷ftp目錄下的所有目錄以及目錄下的文件名稱,取出文件的相對路徑
package com.hmilyld.exp;
import java.io.File;
public class ListFile {
private long[] count = new long[] { 0, 0 };
private File file;
private long[] listFile(String path) {
file = new File(path);
File[] f = file.listFiles();
for (int i = 0; i < f.length; i++) {
if (f[i].isDirectory()) {
count[0]++;
this.listFile(f[i].getPath());
} else {
count[1]++;
}
}
return count;
}
/**
* 得到指定路徑下的文件和文件夾數量
*
* @param path
* 要查看的路徑
* @return object[0]耗時(毫秒)<br>
* object[1]文件夾數量<br>
* object[2]文件數量
*/
public Object[] getFileCount(String path) {
long t = System.currentTimeMillis();
long[] count = this.listFile(path);
t = System.currentTimeMillis() - t;
Object[] o = new Object[] { Long.valueOf(t), Long.valueOf(count[0]),
Long.valueOf(count[1])};
return o;
}
public static void main(String[] args) {
ListFile l = new ListFile();
Object[] count = l.getFileCount("d:\\");
System.out.println(count[0]);
System.out.println(count[1]);
System.out.println(count[2]);
}
}
以前寫的一個獲取目錄下有多少文件和多少文件夾的代碼,
可以參考下.:)
② java寫的ftp程序~~怎麼遍歷文件
//這是一個計算指定目錄所包含文件的總大小的函數
//當然涉及了遍歷文件及文件夾
void getLength(File file)
{
if(file.isDirectory())
{
File fileArray[]=file.listFiles();
for(int i=0;i<fileArray.length;i++){
getLength(fileArray[i]); //System.out.println(fileArray[i].getPath());
}
}
else if(file.isFile()){
try
{
RandomAccessFile raf=new RandomAccessFile(file,"r");
fileLength=fileLength+raf.length();
raf.close();
}catch(IOException ioe){ioe.printStackTrace();}
}
}
③ 跪求:C#.NET遍歷FTP伺服器裡面的文件,以及文件夾
/// <summary>
/// 獲取當前目錄下明細(包含文件和文件夾)
/// </summary>
/// <returns></returns>
public string[] GetFilesDetailList()
{
string ftpURI = "ftp://10.86.86.86";
string ftpUserID = "sa";
string ftpPassword = "sa";
string[] downloadFiles;
try
{
StringBuilder result = new StringBuilder();
FtpWebRequest ftp;
ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));
ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponse response = ftp.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf("\n"), 1);
reader.Close();
response.Close();
return result.ToString().Split('\n');
}
catch (Exception ex)
{
downloadFiles = null;
return downloadFiles;
}
}
④ asp.net(c#)遍歷FTP下的文件夾、子文件夾、文件、子文件夾文件
遞歸//所有文件信息 string fileInfo = string.Empty; private void GetAllFiles() { if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { DirectoryInfo dInfo = new DirectoryInfo(folderBrowserDialog.SelectedPath); //遍歷該文件夾 GetFolder(dInfo); } using (System.IO.StreamWriter sw = new StreamWriter (Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\\fileInfo.txt",false, Encoding.GetEncoding("gb2312"))) { sw.Write(fileInfo); sw.Flush(); } } private void GetFolder(DirectoryInfo dInfo) { //顯示其中文件 GetFile(dInfo); //遍歷文件夾中的文件夾 foreach (DirectoryInfo dir in dInfo.GetDirectories()) { //遞歸遍歷該文件夾 GetFolder(dir); } } private void GetFile(DirectoryInfo dInfo) { //遍歷文件夾中的文件 foreach (FileInfo file in dInfo.GetFiles()) { if(file.Extension.Equal(".jpg")||file.Extension.Equal(".gif")||file.Extension.Equal(".bmp")) { //這里就獲取到了 fileInfo+=file.Name+"\r\n"; } Application.DoEvents(); } }