小编aga*_*aga的帖子

Files.move和Files.copy抛出了java.nio.file.FileAlreadyExistsException

我想删除一个文件并使用旧文件重命名另一个文件,但我无法移动此文件,因为java正在抛出java.nio.file.FileAlreadyExistsException以下是我正在使用的代码片段

static void swapData(String origFilePath, String tempFilePath) throws IOException{

        Path tempPath = FileSystems.getDefault().getPath(tempFilePath);
        Path origPath = FileSystems.getDefault().getPath(origFilePath);
        try{
            String origFileName = null;
            File origFileRef = new File(origFilePath);
            if(Files.exists(origPath)){
                origFileName = origFileRef.getName();
                Files.delete(origPath);
                if(Files.exists(origPath))
                    throw new IOException("cannot able to delete original file");
            }
            if(origFileName != null)
                Files.move(tempPath, tempPath.resolveSibling(origFileName), StandardCopyOption.REPLACE_EXISTING);
        }catch(IOException e){
            throw e;
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我收到的例外 在此输入图像描述Files.move(tempPath, tempPath.resolveSibling(origFileName), StandardCopyOption.REPLACE_EXISTING);

此外,当我在Windows资源管理器中看到此文件时,其缩略图存在但无法打开它.我无法理解它为什么会发生,如果我使用REPLACE_EXISTING,为什么它会抛出FileAlreadyExistsException异常.

我也编辑了上一个问题,因为它没有明确说明.

请帮忙.

Anuj

java java.nio.file

7
推荐指数
1
解决办法
1万
查看次数

提供Function参数的最佳方法

我有一个Api,它将两个struct作为参数

struct bundle{
int a;
int b;
int c;
};

void func(const bundle& startBundle, const bundle& endBundle);
Run Code Online (Sandbox Code Playgroud)

此外,我必须编写另一个具有相同要求的API,而不是在bundle结构中的int,它应该是double.

我可以编写2个结构(1个用于int,1个用于double)但似乎不太好,如果我使用结构,则函数有太多的参数(3个开头的agruments和3个结尾的agruments).请提出一些正确的方法来解决这个问题.

另外如果我必须使用endBundle的默认参数,我该如何使用它?

c++

4
推荐指数
1
解决办法
102
查看次数

了解在共享库中重载operator new的行为

我正在使用Centos平台上的共享库和应用程序[clang ++,llvm3.9.0和libc ++],并且库和应用程序都会重载它们自己的operator new和operator delete.

除1例外,一切正常.在调用std :: string的copy构造函数时总是调用应用程序端的operator new:

这是senario:

std::string str1 ( "A very strange issue on CentOS using clang and libc++" ); //operator new of library side called.

std::string str2(str1); //operator new of application side called. WHY??
Run Code Online (Sandbox Code Playgroud)

对于库侧调用两种情况下的operator delete.

以下是运行以下代码时的日志:

====================================================
operator new in shared library
operator new called Application side
operator delete in shared library
operator delete in shared library
====================================================
Run Code Online (Sandbox Code Playgroud)

共享库侧操作员new和delete:

    void * operator new ( size_t len ) throw ( std::bad_alloc …
Run Code Online (Sandbox Code Playgroud)

c++ stdstring new-operator clang++ libc++

3
推荐指数
1
解决办法
425
查看次数

如何在iPhone/var/mobile/Container/Data/Application/Document中复制文件

我为iPhone开发了一个需要读取一些文件的测试应用程序.在模拟器中,我复制了NSSearchPathForDirectoriesInDomains显示的那些文件.现在我尝试在iPhone中运行相同的应用程序,它显示/var/mobile/Containers/Data/Application/ED49734D-0E61-4BB4-B3CC-D462F3BF9215/Documents/ 位置,但我不知道如何将我的文件放在Documents文件夹中,以便我的应用程序可以读取它.

请帮忙

iphone

1
推荐指数
1
解决办法
4936
查看次数

使用String.format的十进制后的位数

我知道要将double值转换为string,我可以这样做:

String value = String.format("%f", 10.0000);
Run Code Online (Sandbox Code Playgroud)

我也可以使用这个来控制小数点后的位数:

String value = String.format("%.3f", 10.000000);
Run Code Online (Sandbox Code Playgroud)

但我的问题是,我通过变量接收小数点后的位数.如何使用String.format打印用户提供的小数后的位数.

此致,Anuj

java string format

1
推荐指数
1
解决办法
76
查看次数

标签 统计

c++ ×2

java ×2

clang++ ×1

format ×1

iphone ×1

java.nio.file ×1

libc++ ×1

new-operator ×1

stdstring ×1

string ×1