怎样才能取消文件的只读属性?在线等待中。。

SoftHt 2004-10-18 02:21:40
请问,在Java里,用哪个函数来判断某文件是否为只读属性,如果是则要取消只读属性,当编辑完该文件后,在把文件设回只读状态,该功能如何实现?谢谢!
...全文
768 点赞 收藏 10
写回复
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangnewton 2004-11-19
楼上说的正解

我刚刚测试过可以,3x
回复
zuohaosh 2004-10-18
Runtime.getRuntime().exec("attrib -r " + 文件名)
回复
cold_blooded 2004-10-18
JDK1.5也不提供setWrite()这样的方法。
如果要纯Java实现,推荐crete一个新文件。
在把原来的烤进去。删除源文件,给新文件更名。
回复
SoftHt 2004-10-18
to zuohaosh(湖南伢子):

这个方法老是出错,是不是哪里写的不对呀,请指正。谢谢!
<%@ page import="java.lang.*" %>
<%
Runtime.exec("attrib -r " + "aa.txt");
%>

错误信息:
An error occurred at line: 2 in the jsp file: /test.jsp
Generated servlet error:
C:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\test_jsp.java:46: non-static method exec(java.lang.String) cannot be referenced from a static context
Runtime.exec("attrib -r " + "aa.txt");
^
1 error

回复
flyazdream 2004-10-18
仔细看看API中的File类,里面有相应的方法。
canRead
public boolean canRead()
Tests whether the application can read the file denoted by this abstract pathname.

Returns:
true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise
public boolean canWrite()
Tests whether the application can modify to the file denoted by this abstract pathname.

Returns:
true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise.
setReadOnly
public boolean setReadOnly()
Marks the file or directory named by this abstract pathname so that only read operations are allowed. After invoking this method the file or directory is guaranteed not to change until it is either deleted or marked to allow write access. Whether or not a read-only file or directory may be deleted depends upon the underlying system.

Returns:
true if and only if the operation succeeded; false otherwise

其他我也不是很明白了!至于如何取消文件的只读属性我也不是很明白


回复
zuohaosh 2004-10-18
Runtime.exec("attrib +r " + 文件名) //加上只读属性
回复
zuohaosh 2004-10-18
Runtime.exec("attrib -r " + 文件名)
回复
SoftHt 2004-10-18
高手们都帮帮忙呀!谢谢了。
回复
SoftHt 2004-10-18
to GJA106(中文字符):

我用的是j2sdk1.4.2_05,那还有别的办法吗?
回复
GJA106 2004-10-18
纯java没法实现。除非jdk5.0有。
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告