java struts 读取文件,Struts中读写文件的路径问题

问题:JavaBean的一个写文件方法,独立调试正常。但移到Struts下,通过Action调用时,

抛出异常。

原因:文件路径问题解决方法:1.修改原来JavaBean里带前缀路径的文件---"resources/users.properties"为"users.properties"2.将struts框架下的源文件users.properties,直接移到src下3.重新编译,部署4.运行这个注册组件成功后,可以到$服务器主目录$/bin下,查看这个已经写过的users.properties文件以上问题,曾尝试将resources/user.properite改为绝对路径"d:/users.properties",或改为相对路径"/resources/properties",或直接向JavaBean中传入路径参数path,path=request.getRealPath("")(或request.getContextPath)等,均没有调试成功。故记录下来,希望其它网友遇到时,不必再做这样的重复劳动。附:1.Action中调用方法:UserDirectory.getInstance().setUser(userName,password1);2.JavaBean的缩略代码:UserDirectory.javaimport java.io.IOException;

import java.io.FileOutputStream;

import java.util.Enumeration;

import java.util.Properties;public class UserDirectory {

private static final String UserDirectoryFile = "users.properties";

private static final String UserDirectoryHeader = "${user}=${password}";public static UserDirectory getInstance() throws UserDirectoryException {

if (null == userDirectory) {

userDirectory = new UserDirectory();

}

return userDirectory;

}

public void setUser(String userId, String password) throws

UserDirectoryException {

if ( (null == userId) || (null == password)) {

throw new UserDirectoryException();

}try {

p.put(fixId(userId), password);

p.store(new FileOutputStream(UserDirectoryFile),UserDirectoryHeader);

}catch (IOException e) {

throw new UserDirectoryException();

}

}

}

posted on 2005-09-07 13:39 my java 阅读(1418) 评论(1)  编辑  收藏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值