Reading int value from txt file on SD card android

So in this app I made, The user makes a project and when they save, the number of frames is saved to numberFrames.txt on the SD card. Then I retrieve the file in another class. Only thing is that nFrames = 50 when i show a toast of nFrames to the screen after I run this code. The only initializing of nFrames I do is to zero right above this code, which is located in the onCreate().

File sdcardLocal =  Environment.getExternalStorageDirectory();
                 File dir = new File (sdcardLocal.getAbsolutePath() + "/Flipbook/"+customizeDialog.getTitle()+"/");
                 dir.mkdirs();
                 File fileNum = new File(dir, "numberFrames.txt");
                 FileWriter myFileWriter = null;
        try {
         myFileWriter = new FileWriter(fileNum);
        } catch (IOException e) {
         e.printStackTrace();
        }
                 BufferedWriter out = new BufferedWriter(myFileWriter); 
                          try {
                           String text = bitmaps.size()+"";
                           out.write(text);
         out.close();
        } catch (IOException e) {
         e.printStackTrace();
        }

I retrieve the file like this. I have no idea where this "50" value for nFrames came from as there are no loops around this and I know for sure that the particular project saved has only 3 frames. Why is this?

FileInputStream is = null;
             BufferedInputStream bis = null;
             try {
                 is = new FileInputStream(new File(mFolderDialog.getPath()+"/numberFrames.txt"));
                 bis = new BufferedInputStream(is);
                 nFrames = bis.read();
             }catch (IOException e) {
     e.printStackTrace();
    }

Answers


You are writing out a string, and then reading the first byte as an integer. 50 is the ascii code for the '2' character.

You can use BufferedReader.readLine to read the entire first line of the file as a String, and then Integer.parseInt to convert that to an integer.

Also, I would take a closer look at your application's workflow. You don't give much information, but saving a file with a single integer value to the sdcard has a certain "smell" to it :). Have you looked at using a database, or maybe store the text file in your application's directory instead?


Need Your Help

Position child div relative to grandparent div

css position relative absolute

I got a CSS Problem, and I don't know if it's actually possible to achieve what I want.

How Java compares two wrapper variables?

java autoboxing

I have two variables that should be compared:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.