2009-08-18 104 views

回答

19

对于Java 6及以下,

您将需要使用本地通话,这里是Windows

Runtime.getRuntime().exec("attrib +H myHiddenFile.java"); 

你应该学习一些关于Win32的API或Java本机的一种方式。

+4

“原生” 是指你正在运行的平台特定的代码。 'exec()'启动一个DOS/Windows shell来执行一个DOS/Windows程序。 – 2010-01-04 13:40:18

+0

你是一个救星! – 2014-01-08 01:03:30

+0

在Linux中执行此代码时会发生什么?或者我如何防止它? – Xerus 2018-01-21 12:20:44

20

,你的愿望的功能是在即将到来的Java 7

一个NIO.2的特性下面是描述它如何将用于您所需要的一篇文章:Managing Metadata (File and File Store Attributes)。这里有一个例子与DOS File Attributes

Path file = ...; 
try { 
    DosFileAttributes attr = Attributes.readDosFileAttributes(file); 
    System.out.println("isReadOnly is " + attr.isReadOnly()); 
    System.out.println("isHidden is " + attr.isHidden()); 
    System.out.println("isArchive is " + attr.isArchive()); 
    System.out.println("isSystem is " + attr.isSystem()); 
} catch (IOException x) { 
    System.err.println("DOS file attributes not supported:" + x); 
} 

设置属性可以使用DosFileAttributeView

考虑到这些事实来完成,我怀疑有一个标准和优雅的方式来完成,在Java中6或Java 5

3

这是我使用:

void hide(File src) throws InterruptedException, IOException { 
    // win32 command line variant 
    Process p = Runtime.getRuntime().exec("attrib +h " + src.getPath()); 
    p.waitFor(); // p.waitFor() important, so that the file really appears as hidden immediately after function exit. 
} 
13

Java 7中可以隐藏一个DOS文件是这样的:

Path path = ...; 
Boolean hidden = path.getAttribute("dos:hidden", LinkOption.NOFOLLOW_LINKS); 
if (hidden != null && !hidden) { 
    path.setAttribute("dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS); 
} 

早期的Java-s不能。

上述代码不会在非DOS文件系统上引发异常。如果文件的名称以句点开头,则它也将隐藏在UNIX文件系统中。

+0

对于类型java.nio.file.Path(JDK 7u13) – Antonio 2013-02-04 14:33:46

+1

Antonio,未定义方法getAttribute(String,LinkOption),在我使用的Java 7草稿版本中,它一定是这样。我发现类似的功能现在在java.nio.file.Files中。 – 2013-02-04 19:41:27

+5

您可以使用'Files.setAttribute'来接受'Path'来设置属性。 – 2014-07-16 09:35:02

0
String cmd1[] = {"attrib","+h",file/folder path}; 
Runtime.getRuntime().exec(cmd1); 

使用此代码,它可能会解决你的问题

2

在Windows上,使用Java NIO,文件

Path path = Paths.get(..); //< input target path 
Files.write(path, data_byte, StandardOpenOption.CREATE_NEW); //< if file not exist, create 
Files.setAttribute(path, "dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS); //< set hidden attribute 
+2

请添加关于您发布的代码如何解决用户问题的说明 – Suever 2016-04-07 02:25:59

1

这里是一个隐藏在Windows上的任意文件的完全可编译Java 7的代码示例。

import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.nio.file.attribute.DosFileAttributes; 


class A { 
    public static void main(String[] args) throws Exception 
    { 
     //locate the full path to the file e.g. c:\a\b\Log.txt 
     Path p = Paths.get("c:\\a\\b\\Log.txt"); 

     //link file to DosFileAttributes 
     DosFileAttributes dos = Files.readAttributes(p, DosFileAttributes.class); 

     //hide the Log file 
     Files.setAttribute(p, "dos:hidden", true); 

     System.out.println(dos.isHidden()); 

    } 
} 

检查文件是隐藏的。右键单击有问题的文件,在法庭执行后您会看到相关文件是真正隐藏的。

enter image description here