- Java code
public void saveAsDay(){ SimpleDateFormat dataFormat=new SimpleDateFormat("yyyy-MM-dd'.txt'"); String sToday=dataFormat.format(new Date()).toString(); boolean hasToday=false; try{ File dir=new File("./"); String filename[]=dir.list(); for(int i=1;i<filename.length;i++){ if(filename[i].equals(sToday)) hasToday=true; } File acc=new File("./","account.txt"); acc.createNewFile(); if(hasToday){ String s,tmp=""; { FileReader dataReader=new FileReader(acc); BufferedReader readData=new BufferedReader(dataReader); while((s=readData.readLine())!=null) tmp+=(s+"\n"); readData.close(); dataReader.close(); acc.delete(); } { File today=new File("./",sToday); FileWriter dataWriter=new FileWriter(today,true); BufferedWriter writeData=new BufferedWriter(dataWriter); writeData.write(tmp); writeData.close(); dataWriter.close(); } } else{ acc.renameTo(new File("./",sToday)); } }
这段代码在eclipse中运行没问题,打包jar后,if内代码无效,else部分有效,是什么问题??程序中还有几个用到FileReader、FileWriter的,都没有问题,不得其解啊
------解决方案--------------------
File dir=new File("./");
String filename[]=dir.list();
for(int i=1;i<filename.length;i++){
if(filename[i].equals(sToday))
hasToday=true;
}
File acc=new File("./","account.txt");
acc.createNewFile();
那就说明这段代码有问题呗,你的boolean没有改变
------解决方案--------------------
有没可能是编码的问题,eclipse 默认编码有时候和系统编码不一样,如果你的文件名有中文字符就会有问题