找不到文件

发布于 2024-12-07 17:10:44 字数 768 浏览 0 评论 0原文

private void readRandomContacts() throws IOException 
{

BufferedReader bufRdr;
contacts = new ArrayList<Contacts>();

File randomContactsFile = new File("C:\randomContacts.csv");
try {
     bufRdr = new BufferedReader(new FileReader(randomContactsFile));
     String line = null;
     String[] a = new String[2];

        while ((line = bufRdr.readLine()) != null)
                {
                    a = line.split(",");
                    Contacts c = new Contacts(a[0], a[1], a[1], a[1], a[2]);
                    contacts.add(c);
                }

    } catch (FileNotFoundException e) {
        Log.d("file not found", "check");
        e.printStackTrace();
    }

我似乎无法让它找到该文件,并且 randomContacts.csv 确实存在于 C 目录中。有什么帮助吗?

private void readRandomContacts() throws IOException 
{

BufferedReader bufRdr;
contacts = new ArrayList<Contacts>();

File randomContactsFile = new File("C:\randomContacts.csv");
try {
     bufRdr = new BufferedReader(new FileReader(randomContactsFile));
     String line = null;
     String[] a = new String[2];

        while ((line = bufRdr.readLine()) != null)
                {
                    a = line.split(",");
                    Contacts c = new Contacts(a[0], a[1], a[1], a[1], a[2]);
                    contacts.add(c);
                }

    } catch (FileNotFoundException e) {
        Log.d("file not found", "check");
        e.printStackTrace();
    }

I cannot seem to make it find the file and randomContacts.csv does indeed exist in the C directory. Any help please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小梨窩很甜 2024-12-14 17:10:44

尝试使用:

File randomContactsFile = new File("C:\\randomContacts.csv");
                                      ^^  these two are important

有关字符和转义序列的一些信息,请参阅此处

Try with:

File randomContactsFile = new File("C:\\randomContacts.csv");
                                      ^^  these two are important

See here for some information about characters and escape sequences.

酒浓于脸红 2024-12-14 17:10:44
File randomContactsFile = new File("C:\randomContacts.csv"); 

应该是

File randomContactsFile = new File("C:\\randomContacts.csv"); 

你需要转义\,否则java会将\r读为回车符。

File randomContactsFile = new File("C:\randomContacts.csv"); 

should be

File randomContactsFile = new File("C:\\randomContacts.csv"); 

You need to escape \,otherwise, java would read \r as carriage return.

寂寞笑我太脆弱 2024-12-14 17:10:44

你也可以这样做(前斜杠):

new File("C:/randomContacts.csv")

you can also do this (frontslash):

new File("C:/randomContacts.csv")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文