我需要在Windows和Linux上隐藏文件和文件夹。我知道附加'。'到一个文件或文件夹的前面将使它隐藏在Linux上。如何在Windows上隐藏文件或文件夹?使用Java在Windows上隐藏文件/文件夹
回答
对于Java 6及以下,
您将需要使用本地通话,这里是Windows
Runtime.getRuntime().exec("attrib +H myHiddenFile.java");
你应该学习一些关于Win32的API或Java本机的一种方式。
,你的愿望的功能是在即将到来的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
这是我使用:
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.
}
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文件系统中。
对于类型java.nio.file.Path(JDK 7u13) – Antonio 2013-02-04 14:33:46
Antonio,未定义方法getAttribute(String,LinkOption),在我使用的Java 7草稿版本中,它一定是这样。我发现类似的功能现在在java.nio.file.Files中。 – 2013-02-04 19:41:27
您可以使用'Files.setAttribute'来接受'Path'来设置属性。 – 2014-07-16 09:35:02
String cmd1[] = {"attrib","+h",file/folder path};
Runtime.getRuntime().exec(cmd1);
使用此代码,它可能会解决你的问题
在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
请添加关于您发布的代码如何解决用户问题的说明 – Suever 2016-04-07 02:25:59
这里是一个隐藏在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());
}
}
检查文件是隐藏的。右键单击有问题的文件,在法庭执行后您会看到相关文件是真正隐藏的。
- 1. 如何使用java隐藏文件夹
- 2. Java FileSystemView隐藏文件夹
- 3. 在Plone 4.0.5上隐藏文件夹
- 4. 在Windows中隐藏Linux隐藏文件
- 5. 在Windows中隐藏文件
- 6. 如何在Windows上隐藏文件?
- 7. 使用Silverlight隐藏文件夹4
- 8. XCode4中的隐藏文件夹/文件
- 9. NSFileManager:隐藏文件夹?
- 10. 创建隐藏文件夹
- 11. 隐藏文件夹C#
- 12. 隐藏文件夹.htaccess
- 13. applicationhost.config隐藏.vs文件夹
- 14. 如何使用.NET更改Windows文件夹的隐藏属性?
- 15. 如何使用java检索隐藏的文件夹?
- 16. 无法使用java的Runtime.getRuntime()隐藏文件夹。exec()
- 17. 如何在Sharepoint中隐藏文件夹
- 18. 在android中的隐藏文件夹
- 19. 在Delphi中隐藏文件夹中搜索文件夹和特定文件
- 20. 隐藏符号链接文件夹到硬链接文件夹
- 21. 用随机名隐藏公用文件夹中的文件paperclip
- 22. 隐藏的管理文件夹,使用不同的文件夹名称?
- 23. Samba文件服务器隐藏某些用户的文件夹
- 24. 应用程序文件系统中的隐藏文件夹
- 25. C Stat()在隐藏文件夹中的文件失败
- 26. 在Premake中隐藏项目文件的常见父文件夹
- 27. 隐藏反复出现的文件夹
- 28. 隐藏文件夹重定向
- 29. C++ winapi SHBrowseForFolder浏览隐藏文件夹
- 30. 龟SVN隐藏_svn文件夹
“原生” 是指你正在运行的平台特定的代码。 'exec()'启动一个DOS/Windows shell来执行一个DOS/Windows程序。 – 2010-01-04 13:40:18
你是一个救星! – 2014-01-08 01:03:30
在Linux中执行此代码时会发生什么?或者我如何防止它? – Xerus 2018-01-21 12:20:44