使用名称中的变量创建新文件名 [英] Create new file name using variables in the name

查看:84
本文介绍了使用名称中的变量创建新文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在名称中使用变量的特定目录中创建文件。但是,我在以下行保持get和IOException。

Trying to create a file in a specific directory that uses a variable in its name. However, I keep get and IOException at the following lines.

File dir = new File("logs/" + s);
dir.mkdirs();
File permfile = new File(dir, stamp + ".txt");
permfile.createNewFile();
boolean exist = permfile.createNewFile();

感谢您的指导。我今天已经12个小时了,一旦我能写出我可以回家的文件了! :)

I appreciate your guidance. Ive' been at this 12 hours today, once I can write the files I can go home! :)

推荐答案

我会使用 NIO API

Path dir = Paths.get("logs", s);
Files.createDirectories(dir);
Path permfile = dir.resolve(stamp + ".txt");
boolean exist = true;
if(!permfile.toFile().exists()) {
    Files.createFile(permfile);
    exist = false;
}

这篇关于使用名称中的变量创建新文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆