y****i 发帖数: 12114 | 1 步骤:
1、用户输入文件名和路径;
2、程序检查是否已有该文件存在,
2 .1、如果存在,要求用户输入新文件名和路径,回到2;
2.2 、如果新文件名不存在,则生成该文件,运行下一段程序。
我的代码如下。我知道按照我的代码,只要输入新文件名,那个新文件立刻就存在了,
然后就又得输入新文件名,然后文件又存在了,进入死循环了。
该怎么写才能实现上述步骤?
谢谢指点。
Scanner keyboard=new Scanner(System.in);
//get file name
System.out.println("Please enter file name:");
String fileName=keyboard.nextLine();
//get file path
System.out.println("Please enter path:");
String filePath=keyboard.nextLine();
//create file
File tester=new File(filePath+fileName);
// check if the file exists
if (tester.exists())
{
do
{System.out.println("File exists! Enter a different name:");
fileName=keyboard.nextLine();
} while(tester.exists());
}
else
{...} |
B*****g 发帖数: 34098 | 2 你这个tester没变过呀
【在 y****i 的大作中提到】 : 步骤: : 1、用户输入文件名和路径; : 2、程序检查是否已有该文件存在, : 2 .1、如果存在,要求用户输入新文件名和路径,回到2; : 2.2 、如果新文件名不存在,则生成该文件,运行下一段程序。 : 我的代码如下。我知道按照我的代码,只要输入新文件名,那个新文件立刻就存在了, : 然后就又得输入新文件名,然后文件又存在了,进入死循环了。 : 该怎么写才能实现上述步骤? : 谢谢指点。 : Scanner keyboard=new Scanner(System.in);
|
y****i 发帖数: 12114 | 3 我发现了tester没变,可是在哪里让程序改变tester的值,既能让程序跳出,又确保用
户不把现有文件覆盖呢?
我考虑过加一个tester,可是难道每次输入新文件名都要添加一个tester?这个思路好
像不对。
【在 B*****g 的大作中提到】 : 你这个tester没变过呀
|
a******n 发帖数: 5925 | 4 你运行过了吗
确定是死循环?
你的constructor只是建了一个 File instance
但是这个文件不一定存在吧?
所以if就过去了
根本进不了循环
【在 y****i 的大作中提到】 : 我发现了tester没变,可是在哪里让程序改变tester的值,既能让程序跳出,又确保用 : 户不把现有文件覆盖呢? : 我考虑过加一个tester,可是难道每次输入新文件名都要添加一个tester?这个思路好 : 像不对。
|
a****i 发帖数: 1182 | 5 程序有点搞笑
do {
System.out.println("File exists! Enter a different name:");
fileName=keyboard.nextLine();
--> tester = new File (path + fileName);
} while(tester.exists());
【在 y****i 的大作中提到】 : 步骤: : 1、用户输入文件名和路径; : 2、程序检查是否已有该文件存在, : 2 .1、如果存在,要求用户输入新文件名和路径,回到2; : 2.2 、如果新文件名不存在,则生成该文件,运行下一段程序。 : 我的代码如下。我知道按照我的代码,只要输入新文件名,那个新文件立刻就存在了, : 然后就又得输入新文件名,然后文件又存在了,进入死循环了。 : 该怎么写才能实现上述步骤? : 谢谢指点。 : Scanner keyboard=new Scanner(System.in);
|
u****s 发帖数: 2186 | 6 in addition to the change suggested above, you may want to add File.
separator into the full file path
tester = new File(filePath + File.separator + fileName); |
y****i 发帖数: 12114 | 7 高人一句话,胜读十年书!
测试过了,运行良好。
很感谢!
【在 a****i 的大作中提到】 : 程序有点搞笑 : do { : System.out.println("File exists! Enter a different name:"); : fileName=keyboard.nextLine(); : --> tester = new File (path + fileName); : } while(tester.exists());
|
a******n 发帖数: 5925 | 8 运行了一下
加不加那句都一样
直接从if跳过循环
加那句是思想是对的,改test的值
不过exit的结果 不是看你这个file instance建没建
是看这个file 存在不
java doc:
public boolean exists()
Tests whether the file or directory denoted by this abstract pathname exists.
你在循环里面加几个断点就知道了
两种情况下
循环都没有执行
原来不加的代码也没有死循环呀, 直接结束了
加了也是直接结束
【在 y****i 的大作中提到】 : 高人一句话,胜读十年书! : 测试过了,运行良好。 : 很感谢!
|
a****i 发帖数: 1182 | 9 你没注意他后面还有
else
{...}
?
如果文件存在,就进入循环,知道输入一个新的文件名
不存在,就在else里处理,那是另外一回事了
现在的问题是文件存在,do循环就出不来,因为那个File没有改
File.exist一直是true
exists.
【在 a******n 的大作中提到】 : 运行了一下 : 加不加那句都一样 : 直接从if跳过循环 : 加那句是思想是对的,改test的值 : 不过exit的结果 不是看你这个file instance建没建 : 是看这个file 存在不 : java doc: : public boolean exists() : Tests whether the file or directory denoted by this abstract pathname exists. : 你在循环里面加几个断点就知道了
|
a******n 发帖数: 5925 | 10 你的是对的
我测试的内容不充分
胡乱打了几个dir 和 file
都是不存在的
相当于没测存在时的情况
else又是空的,所以不管加不加那句话每次都是
if直接跳到程序结束
【在 a****i 的大作中提到】 : 你没注意他后面还有 : else : {...} : ? : 如果文件存在,就进入循环,知道输入一个新的文件名 : 不存在,就在else里处理,那是另外一回事了 : 现在的问题是文件存在,do循环就出不来,因为那个File没有改 : File.exist一直是true : : exists.
|