高分求用java做一个读取文件属性程序! 1、要求:开启一个文件,一次读取并显示其内的文件属性,包括文件名、文件类型、是否只读、最后修改时间等属性;并实现对文件属性的修改功能。2、可以参考windows的文件属性功能: 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用java读取文件的属性,如文件的创建时间等 String path="xxxx.xxx"; File f = new File(path); f.xxxxx()//详见api帮助文档,索引里输入File就可以了。你参考一下API java中对于文件属性的一些操作1. 当Java.io中,如果文件的操作的时候,判断是否隐藏用File.ishiden()判断是否只读,可用File.canWrite().2. 当要设置是否是可读或者是隐藏时,在java中除了提供File.setReadOnly()外,就无其他方法了。 所以我们必须到Dos环境下去设置,在java中用Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " +R")该方法可以实现。因为路径file.getAbsolutePath()中可能会还有空格,所以必须用引号把它括起来,当作一个参数。这样就可以实现了(1) 设置只读Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " +R");(2) 设置可写Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " -R");(3) 设置隐藏Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " +H");(4) 设置非隐藏Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " -H"); 说的很详细了...其他还有些就要lz去查api了 public class FileAttrOperation { public static void main(String[] args) { FileAttrOperation a = new FileAttrOperation(); a.write(); } public void write() { File file = new File(this.getClass().getResource("/").getPath() + "/a.xml"); System.out.println("getPath:" + file.getPath()); System.out.println("isHidden:" + file.isHidden()); System.out.println("canRead:" + file.canRead()); System.out.println("canWrite:" + file.canWrite()); System.out.println("canExecute:" + file.canExecute()); System.out.println("exists:" + file.exists()); System.out.println("lastModified:" + file.lastModified()); System.out.println("getName:" + file.getName()); file = null; }}测试结果:Compiling 1 source file to D:\NetBean_work\WebApplication1\build\web\WEB-INF\classesgetPath:D:\NetBean_work\WebApplication1\build\web\WEB-INF\classes\a.xmlisHidden:falsecanRead:truecanWrite:falsecanExecute:trueexists:truelastModified:1227787638000(long型)getName:a.xml成功生成(总时间:0 秒) 时间格式转换: SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("lastModified:" + formatter.format(file.lastModified()));测试结果: lastModified:2008-11-27 20:07:18 java.io.File对象中定义了这一系列属性 关于使用伪静态后 找不到namespace的问题 jsp里面的标签问题。 struts学习建议? 刷新问题? 大虾几句话帮菜鸟解决难题 jsp实现线程安全访问 一个关于struts的问题!请各位朋友点解! 【新人求助】JAVA吊丝实习生求助 求spring 源码下载方法 怎样把对象序列化成xml格式.以及再还原回来? 编写网页问题!急急急急急急急急!!!!!!!!!!!!!!!!!!! hibernate的一些问题
String path="xxxx.xxx";
File f = new File(path);
f.xxxxx()//详见api帮助文档,索引里输入File就可以了。
你参考一下API
判断是否只读,可用File.canWrite().
2. 当要设置是否是可读或者是隐藏时,在java中除了提供File.setReadOnly()外,就无其他方法了。
所以我们必须到Dos环境下去设置,在java中用Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " +R")该方法可以实现。因为路径file.getAbsolutePath()中可能会还有空格,所以必须用引号把它括起来,当作一个参数。这样就可以实现了(1) 设置只读Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " +R");(2) 设置可写Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " -R");(3) 设置隐藏Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " +H");(4) 设置非隐藏Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " -H");
说的很详细了...其他还有些就要lz去查api了
FileAttrOperation a = new FileAttrOperation();
a.write(); } public void write() {
File file = new File(this.getClass().getResource("/").getPath() + "/a.xml");
System.out.println("getPath:" + file.getPath());
System.out.println("isHidden:" + file.isHidden());
System.out.println("canRead:" + file.canRead());
System.out.println("canWrite:" + file.canWrite());
System.out.println("canExecute:" + file.canExecute());
System.out.println("exists:" + file.exists());
System.out.println("lastModified:" + file.lastModified());
System.out.println("getName:" + file.getName());
file = null;
}
}测试结果:Compiling 1 source file to D:\NetBean_work\WebApplication1\build\web\WEB-INF\classes
getPath:D:\NetBean_work\WebApplication1\build\web\WEB-INF\classes\a.xml
isHidden:false
canRead:true
canWrite:false
canExecute:true
exists:true
lastModified:1227787638000(long型)
getName:a.xml
成功生成(总时间:0 秒)
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("lastModified:" + formatter.format(file.lastModified()));
测试结果:
lastModified:2008-11-27 20:07:18