gpt4 book ai didi

java - 是否需要 try catch block

转载 作者:行者123 更新时间:2023-12-02 00:05:14 24 4
gpt4 key购买 nike

我从不同的 Windows 服务器获取目录和文件的总字节数,但不确定是否需要在这两种方法中使用 try catch block ?请帮忙

private void retrieveTotalBytes(File sourceFile)
{
File[] files = sourceFile.listFiles();
for(File file : files)
{
if(file.isDirectory())
retrieveTotalBytes(file);
else totalBytes += file.length();
}
}

private void copyFiles(File sourceFile, File targetFile) throws IOException
{
if(sourceFile.isDirectory())
{
if(!targetFile.exists()) targetFile.mkdirs();

String[] filePaths = sourceFile.list();
for(String filePath : filePaths)
{
File srcFile = new File(sourceFile, filePath);
File destFile = new File(targetFile, filePath);

copyFiles(srcFile, destFile);
}
}
else
{ }
}

最佳答案

我认为您不需要进入这些功能,因为它们的级别较低(假设客户足够聪明,可以做出这些负责任的决定)。但您可能会考虑在调用这些函数的更高级别上执行此操作。

例如。 。 。

File data, destination;

try { copyFiles(data, destination); }
catch (IOException e) { . . . }


// and. . .

try { retrieveTotalBytes(data); }
catch (Exception e) { . . . };

。 。 。但这取决于你

关于java - 是否需要 try catch block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14026912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com