文件有关操作,重命名失败

Max7Chou 2012-12-07 09:46:08
现在main类中调用test(){ modif();addFile();}不能成功。 但是变成main中直接用addFile();就可以成功。
test和addFile是在main之外的另一个类中写的,addFile中主要是对已存在的一个文件log.txt进行重命名,然后再新建log.txt, modify中是对log.txt进行写操作。 并且在addFile()函数体中开始处对读写流进行了close操作。 求:如何才能使得调用test执行成功,并作解释,谢谢。。。
...全文
197 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nmyangym 2012-12-11
  • 打赏
  • 举报
回复
楼上那创建流多余,因为File有个方法直接返回文件大小。

	private boolean isTooLarge() throws IOException
	{
		long space;
		space = file.length()/1024;
		System.out.println("Log.isTooLarge() "+space);
		return (space > MAX_FILE_SIZE); 
	}
Max7Chou 2012-12-10
  • 打赏
  • 举报
回复
引用 5 楼 nmyangym 的回复:
楼主直接在modify()方法里,关闭写入流试试。 我测试这样可以。

算了 我还是直接上代码吧,目的是只需要调用test类中的saveLogMsg方法就行了,但是现在这样不行,在save里面关闭流也还是不能成功,只是单独调用addFile是可以增加文件。。。
public class test {


private static test instance;
private File file;
private FileOutputStream out;
private String filePath;

static final int MAX_FILE_SIZE = 1; //KB
static final int MAX_FILE_COUNT = 10;

public static void saveLogMsg(String msg){
try {
test log = getInstance();
log.save(msg);
} catch (Exception e) {
e.printStackTrace();
}
}

public static test getInstance() throws IOException{
if(instance == null){
instance = new test();
instance.init();
}
return instance;
}

private void init() throws IOException{
filePath = "./log/";
File dirFile = new File(filePath);
if(!dirFile.exists()){
dirFile.mkdir();
}
file = new File(filePath + "log.txt");
if(!file.exists()){
file.createNewFile();
}
}

public void save(String string) throws IOException{
if(out == null){
out = new FileOutputStream(file,true);
}

String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date());
string = currentTime + string + "\r\n";
out.write(string.getBytes());
out.flush();

if(isTooLarge()){System.out.println("Log.save()"+isTooLarge());
addFile();
}
}

private boolean isTooLarge() throws IOException{
long space;
space = new FileInputStream(file).available()/1024;
System.out.println("Log.isTooLarge()"+space);
return (space > MAX_FILE_SIZE);
}

public void addFile() throws IOException{
if(out != null){
out.flush();
out.close();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
out = null;
}
for(int i = MAX_FILE_COUNT; i > 0 ; i--){
File checkFile = new File( filePath, "log" + i + ".txt");
if(checkFile.exists() && i == MAX_FILE_COUNT){
checkFile.delete();
}

int index = i - 1;
File lastFile = new File( filePath, "log" + index + ".txt");
if(index == 0){
lastFile = new File( filePath ,"log.txt");
}

boolean ss = lastFile.renameTo(checkFile);
System.out.println("from" + index + " to " + i + " " +ss);
}
if(!file.exists()){
file.createNewFile();System.out.println("dfdgfuh");
}
}

public static void main(String args[]){
test.saveLogMsg("ddddfdhgfhgfjhrtj");
}

}
nmyangym 2012-12-10
  • 打赏
  • 举报
回复
下面这段代码,打开了文件输入流,而没有关闭,导致改名不成功.
private boolean isTooLarge() throws IOException{
long space;
space = new FileInputStream(file).available()/1024;//打开输入流而没关闭。
System.out.println("Log.isTooLarge()"+space);
return (space > MAX_FILE_SIZE); 
}
如此改一下,就ok了。

	private boolean isTooLarge() throws IOException
	{
		long space;
		FileInputStream fis=new FileInputStream(file);
		space = fis.available()/1024;
		fis.close();    //关闭流!!!
		System.out.println("Log.isTooLarge()"+space);
		return (space > MAX_FILE_SIZE); 
	}
Max7Chou 2012-12-07
  • 打赏
  • 举报
回复
为什么 没有人回答...
Max7Chou 2012-12-07
  • 打赏
  • 举报
回复
引用 2 楼 zxl513029 的回复:
引用 1 楼 abcmsnet 的回复:modify和addFile都对同个文件读写操作没有file.close就会有权限冲突了。你把异常贴出来看看 他没有报异常,只是在对log.txt重命名执行rename()时返回的false
只是modify进行写操作,addFile里面没有读写,只是重命名
Max7Chou 2012-12-07
  • 打赏
  • 举报
回复
引用 1 楼 abcmsnet 的回复:
modify和addFile都对同个文件读写操作没有file.close就会有权限冲突了。你把异常贴出来看看
他没有报异常,只是在对log.txt重命名执行rename()时返回的false
  • 打赏
  • 举报
回复
modify和addFile都对同个文件读写操作没有file.close就会有权限冲突了。你把异常贴出来看看
nmyangym 2012-12-07
  • 打赏
  • 举报
回复
楼主直接在modify()方法里,关闭写入流试试。 我测试这样可以。

50,543

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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