PDA

View Full Version : [JAVA] Dir Listing - Assurdo!!!!


fbcyborg
21-07-2005, 17:54
Questo piccolo programma cerca di simulare un ls -l unix, con un solo piccolo grande problema: funziona solo se i path è System.getProperty("user.dir")
PERCHE'?????? è assurdo!
Se creo un file con un path diverso, non c'è nulla da fare, le date e le dimensioni dei files sono rispettivamente 01-01-1970 e 0, e neanche gli attributi drw vengono visualizzati. :mc: :mc: :mc:

import java.io.*;
import java.util.Date;
import java.text.DateFormat;

public class Ls {
public static void main(String[] args) throws IOException{
System.out.println(System.getProperty("user.dir"));
File f = new File(System.getProperty("user.dir"));
//File f = new File("D:\\");
//File f = new File("D:\\Temp");

String[] fList = f.list();

String[] d = new String[fList.length];
String[] r = new String[fList.length];
String[] w = new String[fList.length];

for(int i=0; i<fList.length; ++i){
if(new File(fList[i]).isDirectory())
d[i] = "d";
else
d[i] = "-";

if(new File(fList[i]).canRead())
r[i] = "r";
else
r[i] = "-";

if(new File(fList[i]).canWrite())
w[i] = "w";
else
w[i] = "-";
}

for(int j=0;j<fList.length;j++){
System.out.println(d[j] + r[j] + w[j] + "xr-xr-x root root " +
formatta12((new File(fList[j])).length()) +" " +
dataFormatter(new Date((new File(fList[j])).lastModified()))+
" " + fList[j]);
}

System.gc();
}

private static String dataFormatter(Date d){
DateFormat f = DateFormat.getDateInstance(DateFormat.MEDIUM);
return formattaData11(f.format(d));
}

private static String formattaData11(String data){
if(data.length()==10)
return " " + data;
else
return data;
}

private static String formatta12(long number){
char[] c = new Long(number).toString().toCharArray();
char[] ret = new char[12];
int i = 0;
for (;i<(12-c.length);i++)
ret[i] =' ';
// arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
System.arraycopy(c,0,ret,i,c.length);
return new String(ret);
}
}

fbcyborg
21-07-2005, 17:58
Ah, forse ho capito perchè:
in fList ci sono solo i nomi dei files, e non dell'intero percorso...
ma come risolvere questo problema??

fbcyborg
21-07-2005, 18:05
OK, scusate ma ho risolto utilizzando il metodo
listFiles() della classe Files!

Ciaooo

mjordan
22-07-2005, 07:13
OK, scusate ma ho risolto utilizzando il metodo
listFiles() della classe Files!

Ciaooo

Arrivo sempre tardi, questa la sapevo :cry: :muro: :D

fbcyborg
22-07-2005, 09:15
e vabbè dai... tranquillo!!!! :D grazie cmq

mjordan
22-07-2005, 09:28
e vabbè dai... tranquillo!!!! :D grazie cmq

E di che? Te lo sei risolto da solo il problema...