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);
}
}
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);
}
}