Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重命名Android中存在的文件_Android - Fatal编程技术网

重命名Android中存在的文件

重命名Android中存在的文件,android,Android,我一直在看论坛,发现了一些技巧,但没有一个能给我带来最终的解决方案。如果可能的话,我需要密码 每次关闭我的应用程序时,我都会创建一个txt文件,我的目标是重命名该文件,以防该文件已存在,格式如下: file.txt-file(1.txt-file(2.txt) 到目前为止,我得到的信息如下: int num = 0; public void createFile(String name) { try { String filename = name;

我一直在看论坛,发现了一些技巧,但没有一个能给我带来最终的解决方案。如果可能的话,我需要密码

每次关闭我的应用程序时,我都会创建一个txt文件,我的目标是重命名该文件,以防该文件已存在,格式如下:

file.txt-file(1.txt-file(2.txt)

到目前为止,我得到的信息如下:

int num = 0;
    public void createFile(String name) {  
    try {
        String filename = name;
        File myFile = new File(Environment.getExternalStorageDirectory(), filename);
        if (!myFile.exists()) {
            myFile.createNewFile();
        } else {
            num++;
            createFile(filename + (num));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
file.txt-file.txt1-file.txt12

我拥有的代码如下:

int num = 0;
    public void createFile(String name) {  
    try {
        String filename = name;
        File myFile = new File(Environment.getExternalStorageDirectory(), filename);
        if (!myFile.exists()) {
            myFile.createNewFile();
        } else {
            num++;
            createFile(filename + (num));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

提前谢谢大家

您的
filename
变量包含文件的全名(即file.txt)。所以当你这样做的时候:

createFile(filename + (num));
它只是将数字添加到文件名的末尾

你应该这样做:

int num = 0;

public void createFile(String prefix) {
    try {
        String filename = prefix + "(" + num + ").txt";  //create the correct filename
        File myFile = new File(Environment.getExternalStorageDirectory(), filename);

        if (!myFile.exists()) {
            myFile.createNewFile();
        } else {
            num++;               //increase the file index
            createFile(prefix);  //simply call this method again with the same prefix
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
createFile("file");
那么就这样称呼它:

int num = 0;

public void createFile(String prefix) {
    try {
        String filename = prefix + "(" + num + ").txt";  //create the correct filename
        File myFile = new File(Environment.getExternalStorageDirectory(), filename);

        if (!myFile.exists()) {
            myFile.createNewFile();
        } else {
            num++;               //increase the file index
            createFile(prefix);  //simply call this method again with the same prefix
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
createFile("file");

如何调用
createFile
方法?使用哪个参数?createFile(“FloodingFile.txt”);不管是谁在这个问题上给了我负面的名声,谢谢(讽刺的是)。我不明白原因。如果是因为代码没有正确缩进,我尝试过多次正确缩进,但总是收到错误消息。我认为你给我负面评价是不公平的。多亏了这个人,我无法对那些很好地回答我问题的人投赞成票。再次感谢那个曾经的人,你们知道我如何为这样的文件分配“只读”权限吗?此时,当我打开文件时,用户可以编辑文件。我想让用户只阅读它,不能修改文件的内容谢谢,但已经看过这样的帖子并尝试了答案,但我仍然可以进行更改并保存它们…嗨!我刚刚意识到这个重命名问题有效,但是已经存在的文件的内容被修改了。我的意思是,如果已经存在FloodingFile(0).txt,并且包含例如“abcd”,并且正在使用“efgh”创建一个新文件,则新创建的文件的名称将是FloodingFile(1).txt,这是正确的,但FloodingFile(0)的内容修改为“efgh”。这里出了什么问题?如果我对自己的问题写了一个答案,是因为之前我在评论中也写了同样的答案,但我没有收到答案。我想也许通过回答而不是评论能让我更清楚。如果我们不删除帖子,而是回答评论和问题,这可能会更好