免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3843 | 回复: 1
打印 上一主题 下一主题

java如何控制linux的文件权限? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-02 14:12 |只看该作者 |倒序浏览
C里面可以用系统调用,比如chdir(),chmod(),Java怎么实现呢?可以实现吗?
PS:不是用Runtime.exec(),而是用api

论坛徽章:
0
2 [报告]
发表于 2009-06-03 16:43 |只看该作者
chdir():
Java Applications and the "Current Directory"
A recurring question in the Sun Java Forum groups is, "How do I change the current directory of my Java application?" The correct answer is that you don't! Java applications don't use the metaphor of 'current directory' in the same way native Unix and Windows applications do.
A native Unix or Windows application typically by default points at the current directory of the command shell where the program is started, and if you do an fopen("x.txt","w") your file will end up in that directory. You can then call chdir(..) to change the current directory of the application. The metaphor is that the application is like a user at the command line, cd'ing around and accessing files.

On the other hand, when you start a Java application with 'java appname', it is system dependent what the current directory is and you can't change the 'current directory' from a Java application, and the application as user metaphor does not apply.

To understand the Java solution, it is helpful to identify is the real requirement. The requirement is not to change to a new current directory just because you want to. The real requirement is to allow a program to get at files in the file system relative to the current directory. In other words, make it easy to access files in a given location without an absolute path prefix.

In Java, you use File objects to construct a relative view of the file system. Two of the constructors for the File object take a 'parent' argument that specifies a parent path that is prefixed to the path of the file itself to create the full abstract path to the file. What you do is, create a File object with the path that represents your current directory and then create all your file objects using that File object as the parent. Voila, a current directory. If you want to change the current directory, create a new File object with the desired path and use it as the parent. See the SDK documentation, File object for the specifics of the constructors.

The main catch here is that you have to specify the parent somehow, either by knowing it outright, specifying it thru some user input or figuring it out from the application environment. If you know it outright, you can hardcode it or store it as an application property or something similar. The user input could be a command line argument or some run-time input method, such as a JFileChooser object.

On some systems you can read a system property called 'user.dir' to get the path that the application was started in. This property is system dependent as to what it means or whether it exists at all, but in Unix and Windows it returns the directory the command shell was in when the java application was started. On Windows and Unix, "user.dir" is a READ ONLY property, and you can't change the 'current directory' by trying to set the "user.dir" property. You read system properties with the System.getProperty(String) method.


chmod():
http://java.sun.com/j2se/1.5.0/d ... FilePermission.html
需要说明的是Java是跨平台的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP