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

SoftHt 2004-10-18 02:21:40
请问,在Java里,用哪个函数来判断某文件是否为只读属性,如果是则要取消只读属性,当编辑完该文件后,在把文件设回只读状态,该功能如何实现?谢谢!
...全文
854 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
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有。

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧