如何防止块被显示,最终,如果没有文件的路径。

huangapple go评论77阅读模式
英文:

How to prevent the block from being displayed, finally, if there is no path to the file

问题

以下是您要翻译的内容:

问题:如果在参数中未指定文件的路径,那么仍然会显示短语“The file was closed”。这在uploadToFile和read方法中都有效。我在参数中传递一个路径,并将第二个路径写在DownloadFile中。

public class Task implements AutoCloseable {

    public static void main(String[] args) throws IOException {
        String DownloadFile = "C:\\Users\\VGilenko\\IdeaProjects\\Task\\src\\main\\resources\\Out.txt";
        Map<String, Departament> departments = new HashMap<>();
        String path = args.length > 0 ? args[0] : null;

        read(path, departments);
        transferToDepartment(departments, DownloadFile);
    }

    private static void uploadToFile(List download, String path) {
        int i = 0;
        try (FileWriter writer = new FileWriter(path, false)) {
            // ...
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        } finally {
            System.out.println("The file was closed");
        }
    }

    public static void transferToDepartment(Map<String, Departament> departments, String downloadFile) {
        List<String> download = new ArrayList<>();
        // ...
        uploadToFile(download, downloadFile);
    }

    public static void read(String path, Map<String, Departament> departments) throws IOException {
        assert path != null;
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "CP1251")); br) {
            // ...
        } catch (FileNotFoundException e) {
            System.out.println("The file was not found, check the path");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Correct the file path, step out of the array");
        } catch (NullPointerException e) {
            System.out.println("You forgot to register the path to the file");
        } finally {
            System.out.println("The file was closed");
        }
    }

    @Override
    public void close() {
        System.out.println("The file was closed");
    }
}
英文:

Problem: if the path to the file was not specified in the arguments, then it still displays the phrase "The file was closed". This works 2 times. In uploadToFile and read method. I pass one path in the arguments, and the second is written in the DownloadFile

    public class Task implements AutoCloseable {
public static void main(String[] args) throws IOException {
String DownloadFile = &quot;C:\\Users\\VGilenko\\IdeaProjects\\Task\\src\\main\\resources\\Out.txt&quot;;
Map&lt;String, Departament&gt; departments = new HashMap&lt;&gt;();
String path = args.length &gt; 0 ? args[0] : null;
read(path, departments);
transferToDepartment(departments, DownloadFile);
}
private static void uploadToFile(List download, String path) {
int i = 0;
try (FileWriter writer = new FileWriter(path, false)) {
...
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
System.out.println(&quot;The file was closed&quot;);
}
}
public static void transferToDepartment(Map&lt;String, Departament&gt; departments, String downloadFile) {
List&lt;String&gt; download = new ArrayList&lt;&gt;();
...
}
uploadToFile(download, downloadFile);
}
public static void read(String path, Map&lt;String, Departament&gt; departments) throws IOException {
assert path != null;
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path), &quot;CP1251&quot;)); br) {
.....
}
}
} catch (FileNotFoundException e) {
System.out.println(&quot;The file was not found, check the path&quot;);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(&quot;Correct the file path, step out of the array&quot;);
} catch (NullPointerException e) {
System.out.println(&quot;You forgot to register the path to the file&quot;);
} finally {
System.out.println(&quot;The file was closed&quot;);
}
}
@Override
public void close() {
System.out.println(&quot;The file was closed&quot;);
}
}

答案1

得分: 1

你的打印输出位于 finally 语句中的 "文件已关闭"。如果你没有指定文件,你将捕获到一个异常,并且将执行你的 finally 块。

一个简单的修复方法是检查路径的存在(不为空,不为 null)。

英文:

You have your printout "The file was closed" in your finally statement. If you don't specify a file, you will catch an Exception, and your finally block will be executed.

An easy fix would be to check for the existence of the path (not being empty, not being null).

huangapple
  • 本文由 发表于 2020年8月11日 19:03:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63356846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定