La meme
La meme

Reputation: 1

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

Even though I've given the program the exact path to the csv file, it still doesn't exist according to the program. I've got no idea how to fix it.

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();
        }
    }

The relevant code.

I've already enabled reading and writing from external storage from the manifest.

Upvotes: 0

Views: 336

Answers (1)

computercarguy
computercarguy

Reputation: 2453

The location C:\users\blah\...... means absolutely nothing to the Android app, which is part of why it's not working. The other part is because that file doesn't reside on the device you are testing the app on.

Whether you are running this app on a mobile device or through an emulator (virtual device), you don't have direct access to your computer's drives. At best, you'll have to map a network drive for the app to access. At worst, you'll have to set up an API on your machine for the app to hit and have that access the file.

At this point, you might as well use a database for the password info, since it's likely you're going to need to save other information. Besides, CSV files have some serious issues, and beyond just the security issue of having usernames and passwords in plain text (even if they are encoded before they get to your saving methods).

Upvotes: 3

Related Questions