读取文件时出现异常 [英] Getting exception on reading a file

查看:244
本文介绍了读取文件时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了从服务器下载文件并将其保存到项目的类路径的方法。下载后我可以看到项目文件夹中的文件。但是当我在文件上调用read方法时,我得到了异常。因此,当我终止执行之间(文件仍在文件夹中)并尝试再次运行程序时,它现在工作正常。我不确定是什么问题,因为我已经尝试在下载文件后等待并且它不起作用。不知何故,文件在下载后无法读取。谁能告诉我可能是什么问题?



I have method written for downloading files from server and saving it to the classpath of the project. I can see the files in project folder after its been downloaded. But when I call read method on the files I get exception. So when I terminate the execution in between (the files are still in the folder) and try to run the program again it works fine now. I am not sure what is the issue because I have tried putting wait after the files are downloaded and it does not work. Somehow the files are not readable just after download. Can anyone tell me what can be the issue?

public Map<String, String> readCsv(String csvName) {
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    String csvFileName = csvName;

    Map<String, String> abcCsv = new HashMap<>();
    InputStream inputStream = getClass().getClassLoader().getResourceAsStream(csvFileName);

    try {
        br = new BufferedReader(new InputStreamReader(inputStream));
        while ((line = br.readLine()) != null) {

            String[] abc = line.split(cvsSplitBy);

            abcCsv.put(
                    (abc[0]).trim().replaceAll(REP1, "").replaceAll(REP2, " ").replaceAll(REP6, ""),
                    (abc[1]).trim().replaceAll(REP1, "").replaceAll(REP2, " ").replaceAll(REP7, "")
                            .replaceAll(REP4, ""));
        }

    } catch (FileNotFoundException e) {
        LOGGER.error("File not found exception thrown ", e);
    } catch (IOException e) {
        LOGGER.error("I/O exception thrown ", e);
    } catch (ArrayIndexOutOfBoundsException e) {
        LOGGER.error("Array index out of bounds.");
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                LOGGER.error("I/O exception thrown ", e);
            }

        }
    }
    return abcCsv;

}





我的尝试:



替换了getResource或getResourceAsStream;使用了新的FileInputStream。但仍然得到相同的错误。



What I have tried:

replaced getResource or getResourceAsStream; used new FileInputStream. but still get the same error.

推荐答案

你的代码做了一些假设,比如它不知道返回了什么.getResourceAsStream( csvFileName) inputStream 可能最终为null,在代码崩溃之前你不会知道它。



你的代码没有在尝试打开文件之前检查文件是否存在。



您的代码还假设它读取的每一行都没问题且具有正确的字段数它。你根本不检查这些东西。
Your code makes some assumptions, such as it has no idea if anything was returned by .getResourceAsStream(csvFileName). inputStream could end up being null and you wouldn't know it until the code crashed.

Your code doesn't check to see if the file exists before trying to open it.

Your code also assumes that every line it reads is OK and has the correct number of fields in it. You don't check for these things at all.


这篇关于读取文件时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆