繁体   English   中英

试图让 android 应用程序访问 CSV 文件进行编辑和写入,但它似乎无法找到该文件

[英]Trying to get an android app to access a CSV file to edit and write to, but it can't seem to find the file

尽管我已经为程序提供了 csv 文件的确切路径,但根据程序,它仍然不存在。 我不知道如何解决它。

public boolean SignUpFunc(String  username, String password){
    try {
        File file = new File("C:\\Users\\altaf\\OneDrive\\Desktop\\Java\\login.csv");
        BufferedReader reader = new BufferedReader(new FileReader(file.getPath()));
        String line;
        while ((line= reader.readLine())!= null){
            //split by ,
            String[] tokens = line.split(",");

            //Read the data
            if (username.equals(tokens[0])){
                reader.close();
                return false;
            }
        }
        save(file,username,password);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

    public static void save(File file, String username, String password){
        try{
            BufferedWriter Bwriter = new BufferedWriter(new FileWriter(file.getPath(),true));
            PrintWriter writer = new PrintWriter(Bwriter);
            writer.println(username+","+password);
            writer.flush();
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("FilenotFound");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

相关代码。

我已经从清单中启用了从外部存储读取和写入。

位置C:\users\blah\......对 Android 应用程序绝对没有任何意义,这也是它无法正常工作的部分原因。 另一部分是因为该文件不驻留在您正在测试应用程序的设备上。

无论您是在移动设备上还是通过模拟器(虚拟设备)运行此应用程序,您都无法直接访问计算机的驱动器。 充其量,您必须使用 map 网络驱动器才能访问该应用程序。 在最坏的情况下,您必须在您的计算机上设置一个 API 才能让应用程序点击并访问该文件。

此时,您不妨使用数据库来存储密码信息,因为您可能需要保存其他信息。 此外,CSV 文件有一些严重的问题,不仅仅是用户名和密码为纯文本的安全问题(即使它们在进入您的保存方法之前已经过编码)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM