java 中如何设置建立的txt文件为隐藏

Python046

java 中如何设置建立的txt文件为隐藏,第1张

从根本上说,就是用Java调用CMD,然后用CMD的隐藏文件命令

import java.io.File

import java.io.IOException

public class DoFile {

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

File file = new File("d:/test.txt")

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

file.delete()

file.createNewFile()

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

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

// 输出命令串

System.out.println(sets)

// 运行命令串

Runtime.getRuntime().exec(sets)

}

}

一看就知道要干坏事,不过方法确实有,但不知道JAVA能不能干,因为利用的是Windows的文件系统漏洞的。

当然如果想简单点,只是设置文件属性为隐藏,这个可能比较简单。

import java.io.IOException

public class JavaCmd {

public static void main(String[] args){

//设置路径名

String path="c:/debug/*.*"

//拼接命令

String cmd = "attrib +h +s " + path + " /S /D "

Runtime run = Runtime.getRuntime()

try {

run.exec(cmd)  //执行命令

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

}

}

java 调用windows文件属性设置命令。示例代码:D盘下创建hello文件夹设置属性为隐藏

import java.io.File

import java.io.IOException

public class Test {

public static void main(String[] args) {

File file=new File("D:/hello")

try {

if(!file.exists())

file.mkdir()

String string=" attrib +H "+file.getAbsolutePath()

Process p = Runtime.getRuntime().exec(string)

} catch (IOException e) {

e.printStackTrace()

}

}

}

文件相关属性设置:显示或更改文件属性。

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

/D]]

+ 设置属性。

-清除属性。

R 只读文件属性。

A 存档文件属性。

S 系统文件属性。

H 隐藏文件属性。

[drive:][path][filename]

指定要处理的文件属性。

/S 处理当前文件夹及其子文件夹中的匹配文件。

/D 也处理文件夹。

希望帮助到你