Java哪个类可以修改文件夹属性

快乐的2 2009-08-27 03:56:24
U盘出去打印的时候中病毒了,拿回来一看U盘里的文件夹都给改成系统属性隐藏了,并在盘上建了个跟原来文件夹同名的可执行文件,幸好电脑的扩展名都是显示出来的,把病毒删除之后想把文件夹改回正常的,结果手工好像改不了,又想到用Java改下文件属性,结果查看了File的API后发现没有更改属性的方法,又查了几个类也没有发现这样的功能,请问高手们Java里面哪个类可以修改文件夹或者文件的属性?能给个Demo最好不过了。
...全文
497 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm6478362 2010-12-21
  • 打赏
  • 举报
回复
学习了!“倡议问题解决后发出解决方法,Java学的不好给出自己解决后的代码共享”!
快乐的2 2009-10-13
  • 打赏
  • 举报
回复
倡议问题解决后发出解决方法,Java学的不好给出自己解决后的代码共享
文件属性类

package cn.sist.file;

public class FileProperty {
private int sysProperty = 0;
private int hiddenProperty = 0;
private int readProperty = 0;
private int arriveProperty = 0;
private boolean changeAll = false;


public FileProperty setChangeAll(boolean changeAll){
this.changeAll = changeAll;
return this;
}

public boolean getChangeAll(){
return this.changeAll;
}
protected int getArriveProperty() {
return arriveProperty;
}
public FileProperty addArriveProperty() {
this.arriveProperty = 1;
return this;
}
public FileProperty removeArriveProperty(){
this.arriveProperty = 2;
return this;
}
protected int getHiddenProperty() {
return hiddenProperty;
}
public FileProperty addHiddenProperty() {
this.hiddenProperty = 1;
return this;
}
public FileProperty removeHiddenProperty(){
this.hiddenProperty = 2;
return this;
}
protected int getReadProperty() {
return readProperty;
}
public FileProperty addReadProperty() {
this.readProperty = 1;
return this;
}
public FileProperty removeReadProperty(){
this.readProperty = 2;
return this;
}
protected int getSysProperty() {
return sysProperty;
}
public FileProperty addSysProperty() {
this.sysProperty = 1;
return this;
}
public FileProperty removeSysProperty(){
this.sysProperty = 2;
return this;
}
}

文件操作类

package cn.sist.file;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class FilePropertyChange {
private static final String xpCommand = "cmd /c attrib";
private static final String[] xpSysProperty = {"","+s","-s"};
private static final String[] xpHiddenProperty = {"","+h","-h"};
private static final String[] xpArriveProperty = {"","+a","-a"};
private static final String[] xpReadOnlyProperty = {"","+r","-r"};
/*
* defaultFileProperty only have arriveProperty.
* it has removed all property except arriveProperty
*/
public static final FileProperty defaultFileProperty =
new FileProperty().addArriveProperty().removeSysProperty()
.removeHiddenProperty().removeReadProperty();

/*
* XPFileProertyChange is a method which execute under System of windows XP.
* It use dos command to change the file's Property
*/
public static boolean XPFilePropertyChange(File file,FileProperty fp){
// if(!file.exists())
// return false;
final String space = " ";
StringBuffer sb = new StringBuffer().append(FilePropertyChange.xpCommand).append(space);
sb.append(FilePropertyChange.xpArriveProperty[fp.getArriveProperty()]).append(space);
sb.append(FilePropertyChange.xpHiddenProperty[fp.getHiddenProperty()]).append(space);
sb.append(FilePropertyChange.xpReadOnlyProperty[fp.getReadProperty()]).append(space);
sb.append(FilePropertyChange.xpSysProperty[fp.getSysProperty()]).append(space);
sb.append("\"").append(file.getAbsolutePath()).append("\" ");
sb.append("/s /d");
StringBuffer sb2 = null;
try{
Runtime.getRuntime().exec(sb.toString());
}catch(IOException ioe){
System.out.println("Error");
ioe.printStackTrace();
return false;
}
if(fp.getChangeAll())
return XPFilePropertyChange(new File(file.getAbsoluteFile()+"\\*"),fp.setChangeAll(false));
return true;
}

/*
* @paramter file
* @return List<File>
*/
public static List<File> listFile(File file){
if(!file.exists())
return null;
List<File> list = new ArrayList<File>();
list.add(file);
File[] files = null;
if(file.isDirectory()){
files = file.listFiles();
for(File tmpFile:files){
if(tmpFile.isDirectory()){
list.addAll(listFile(tmpFile));
}else{
list.add(tmpFile);
}
}
}
return list;
}
public static void main(String[] args) {
FileProperty fp = new FileProperty();
fp.removeSysProperty().removeHiddenProperty()
.addArriveProperty().addReadProperty().setChangeAll(false);
File file = new File("H:\\hidden");
System.out.println(file.getAbsolutePath());
boolean sucess = FilePropertyChange.XPFilePropertyChange(file, fp);
System.out.println(sucess);
}
}
woshayawo 2009-08-27
  • 打赏
  • 举报
回复
学习了!
xiaocaogf 2009-08-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zhuyx808 的回复:]
这个问题我也遇到过,不是U盘,是照相机,照好的照片都在SD卡里面,结果病毒感染了,而且杀毒软件直接把病毒给杀了,我打开那个盘看里面空空的,怎么办?我的照片啊……


幸好我有winrar,用winrar打开SD卡,文件夹出来了,把照片拷出来,然后——>格式化SD卡
[/Quote]

呵呵,问题想的太复杂了!
快乐的2 2009-08-27
  • 打赏
  • 举报
回复
怎么会不能结贴呢。。。
快乐的2 2009-08-27
  • 打赏
  • 举报
回复
好的,谢谢各位了,先试下,如果好使就结贴了。
快乐的2 2009-08-27
  • 打赏
  • 举报
回复
还是这么运行
Runtime.getRuntime().exec("[command|cmd] \"attrib -s +r " + myFilePath+myFileName+" /s /d \"");
lovecj6185 2009-08-27
  • 打赏
  • 举报
回复
只要
attrib -s +r " + myFilePath+myFileName+" /s /d
能在ms-dos窗口中执行

上面的代码就可以执行
hxbot 2009-08-27
  • 打赏
  • 举报
回复
JAVA应该本身不会有这种方法,因为你这种改权限实际上是跟操作系统有关,不同的系统,设置权限的方式都不一样,JAVA是跨平台的怎么会有这种方法呢.

既然跟操作系统平台有关,那么我给你条思路。


建议过程:写一个文件域。获得绝对路径,然后调用
Runtime.getRuntime().exec(
"cmd /c attrib d:\\SUPER_BAR_CHART.jpg -r -a -s -h");
此方法仅限Windows平台


try {
Runtime.getRuntime().exec(
"cmd /c attrib d:\\SUPER_BAR_CHART.jpg -r -a -s -h");
}catch(IOException e) {
e.printStackTrace();
}
快乐的2 2009-08-27
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lovecj6185 的回复:]
用File类里的isHidden() ,只能检测文件是不是隐藏文件
但是没有提供设置的方法

如果楼主真想用java改的话
就执行
Runtime.getRuntime().exec(command)
把dos命令放到command里
[/Quote]
是这么执行吗?
Runtime.getRuntime().exec("attrib -s +r " + myFilePath+myFileName+" /s /d");

这么执行对吗?
好吃的松子 2009-08-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zhuyx808 的回复:]
这个问题我也遇到过,不是U盘,是照相机,照好的照片都在SD卡里面,结果病毒感染了,而且杀毒软件直接把病毒给杀了,我打开那个盘看里面空空的,怎么办?我的照片啊……


幸好我有winrar,用winrar打开SD卡,文件夹出来了,把照片拷出来,然后——>格式化SD卡
[/Quote]

哦,Winrar有这功能,回去试试的我那个micro sd卡还能不能读得出来。
lovecj6185 2009-08-27
  • 打赏
  • 举报
回复
用File类里的isHidden() ,只能检测文件是不是隐藏文件
但是没有提供设置的方法

如果楼主真想用java改的话
就执行
Runtime.getRuntime().exec(command)
把dos命令放到command里
快乐的2 2009-08-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bigbug9002 的回复:]
在安全模式下,或纯Dos下用attrib命令试试吧:

ATTRIB [+R | -R] [+A | -A ] [+S | -S] [+H | -H] [[drive:] [path] filename] [/S [/D]]

  +  设置属性。
  -    清除属性。
  R  只读文件属性。
  A  存档文件属性。
  S  系统文件属性。
  H  隐藏文件属性。
  [drive:][path][filename]
      指定要处理的文件属性。
  /S  处理当前文件夹及其子文件夹中的匹配文件。
  /D  也处理文件夹。
[/Quote]
虽然知道这个方法,但还是谢谢,我就是想知道Java能实现这样的功能不。
快乐的2 2009-08-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bigbug9002 的回复:]
你修改时,系统的提示是什么?
[/Quote]
文件夹的属性为系统,并且是灰色的。
快乐的2 2009-08-27
  • 打赏
  • 举报
回复
只是想多学点知识,正在迷茫中。
justinavril 2009-08-27
  • 打赏
  • 举报
回复
楼主实在太可爱了,中病毒首先想到的是Java,呵呵。
zhuyx808 2009-08-27
  • 打赏
  • 举报
回复
这个问题我也遇到过,不是U盘,是照相机,照好的照片都在SD卡里面,结果病毒感染了,而且杀毒软件直接把病毒给杀了,我打开那个盘看里面空空的,怎么办?我的照片啊……


幸好我有winrar,用winrar打开SD卡,文件夹出来了,把照片拷出来,然后——>格式化SD卡
why_java 2009-08-27
  • 打赏
  • 举报
回复
应该不可以吧!!!!
bigbug9002 2009-08-27
  • 打赏
  • 举报
回复
在安全模式下,或纯Dos下用attrib命令试试吧:

ATTRIB [+R | -R] [+A | -A ] [+S | -S] [+H | -H] [[drive:] [path] filename] [/S [/D]]

+ 设置属性。
- 清除属性。
R 只读文件属性。
A 存档文件属性。
S 系统文件属性。
H 隐藏文件属性。
[drive:][path][filename]
指定要处理的文件属性。
/S 处理当前文件夹及其子文件夹中的匹配文件。
/D 也处理文件夹。
bea_java 2009-08-27
  • 打赏
  • 举报
回复
可以修改吗? 如果有的话我顶 。顶。强力的顶。
加载更多回复(2)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧