문제

I've been working on a little application to allow my friends and I to create our own virtual flashcards. These cards are arranged in text files mapped to be accessed via Spinner -s. The user of the application defines their own spinners and thus the categories of study.

When the activity is first started, I use a Scanner to read in from a text file called SpinnersTXT.txt, I run through it line by line and, accepting the rigid syntax of Name-Type-Elements, instantiate the Spinner defined in the file. But that isn't exactly important. I actually just read in from the file using a Scanner

If one adds a new Spinner later its information is added to the text file. When I run my code I experience no errors relating to anything not covered by my exception handling, yet I know that either the new Spinner-info is not being added to the TXT file, and/or my Scanner is not accessing the correct file as the Spinner object is not being added (and I did remember the addView call to the parent ScrollView).

Here is my file path setup, with the folder Saves containing my initially empty txt file

--> So firstly I must ask if this is how internal storage is used, I want to access and edit this file from within my app, with this placement, may I?

Beyond that here is the code by which I attempt to access the file, and create it if it doesn't exist. I derived this from another stackoverflow question (I have done research) but would appreciate some validation.

try{
        fileProcessorScanner = new Scanner("Saves/SpinnersTXT.txt");

    }catch(Exception e){
        e.printStackTrace();
        File path = new File(getFilesDir(),"Saves");
        path.mkdirs();
        File SpinnersTXT = new File(path,"SpinnersTXT.txt");
    }

--> Will this code successfully get a file? And will the catch create the file if it doesn't exist? I apologize I am new to IO but even after reading the chapters of my beginners' books I still see so many forms of Input and Output I am easily boggled... But I feel that I must use a Scanner for its nextLine() method, despite that most examples don't even touch the Scanner.

Finally here is the code by which I try to write to that very file (created by my catch if not in existence):

outGoingSpinners = openFileOutput("SpinnersTXT.txt",MODE_WORLD_READABLE);
        outGoingSpinners.write(NEW_SPIN_OPEN.getBytes());
        outGoingSpinners.write(tempNewSpinnerName.getBytes());
        outGoingSpinners.write(tempNewSpinnerType.getBytes());
        outGoingSpinners.write(tempNewSpinnerInitializer.getBytes());
        outGoingSpinners.write(NEW_SPIN_CLOSE.getBytes());
        outGoingSpinners.close();

--> Will this save my information to the same text file as above? Will my escape sequences be respected and preserved?

I sincerely appreciate any and all help. All of you are so insightful and this community has made many of my dreams a reality. I just get lost and in over my head sometimes especially with the various forms of IO and the nuances of the language. Thank you all.

도움이 되었습니까?

해결책

As explained at your similar question, you cannot place files in arbitrary locations under your source project and expect them to turn up on the device. You can store data using a mechanism such as assets, but it will be read-only at runtime. To have modifiable initialized data, you must package it in a read only form and then copy it out to the filesystem to create a modifiable version. Or you can download it at runtime using various methods.

This is covered in detail in many questions here, especially concerning databases, but the solutions illustrated there are basically generic to any type of file.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top