日期:2014-05-20  浏览次数:20876 次

帮我看看怎么用properties读不出来文件
package com.yeliguo.LoanRepaymentSystem.service.impl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

import com.yeliguo.LoanRepaymentSystem.bean.LoanMessage;
import com.yeliguo.LoanRepaymentSystem.service.Service;

public class AddLoanService implements Service{

public String getId() throws IOException{
String id="";
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String today = sdf.format(date);
String path = System.getProperty("user.dir") + "/com.yeliguo.loanrepaymentsystem/id.properties";
Properties p = new Properties();
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(path);
fos = new FileOutputStream(path);
p.load(fis);
String pid =(String)p.get("loan.id");
String pdate = p.getProperty("loan.date");
if(today.equals(pdate)){
id = "" +Integer.parseInt(pid) + 1;
p.setProperty("loan.id", id);
p.store(fos, null);
}else{
p.setProperty("loan.id", "1");
p.setProperty("loan.date", today);
p.store(fos, null);
}

fos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try{
if(fis != null){
fis.close();
}
if(fos != null){
fos.close();
}
}catch(IOException e){
e.getStackTrace();
}
}

return id;
}

@Override
public void service(Map<String, String> account, Socket s) {
LoanMessage lm = new LoanMessage();
}

public static void main(String[] args) throws IOException {

System.out.println(new AddLoanService().getId());
}

}


------解决方案--------------------
user.dir不是一个固定的目录。

在eclipse里运行,是项目的根目录。
在命令行运行,在那个目录下运行就是那个目录。