Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2341422
  • 博文数量: 2110
  • 博客积分: 18861
  • 博客等级: 上将
  • 技术积分: 24420
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-05 18:23
文章分类

全部博文(2110)

文章存档

2011年(139)

2010年(1971)

我的朋友

分类: Java

2010-10-19 11:55:57

 

 Java代码

  public class 设置文件属性 {

  // 执行以下代码你将看到一个属性为隐藏的文件(D:\ddd.ddd)

  // 请到D盘下查看

  public static void main(String[] args) throws IOException {

  // 创建新文件

  File file = new File("D:\\ddd.ddd");

  // 删除文件并创建新文件

  file.delete();

  file.createNewFile();

  // 拼dos命令

  // attrib的祥细功能介绍请在DOS内输入 " attrib /? " 查看

  String sets = "attrib +H \"" + file.getAbsolutePath() + "\"";

  // 输出命令串

  System.out.println(sets);

  // 运行命令串

  Runtime.getRuntime().exec(sets);

  }

  }

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

阅读(401) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~