Continue. Today we are talking about reading and writing record files. Because the sudoku implemented on the Brew platform is a binary file, it is used directly on Android.
We plan to implement two functions: loadtilist (), load the question library, first load the question library file in the resource, and then load it from the resource as a datainputstream. There are few lines of code, as shown below: public static Boolean loadtilist (mainactivity me)
...{
Datainputstream in = NULL;
Try
...{
In = new datainputstream (Me. getresources (). openrawresource (R. Raw. Ti ));
Byte [] bufc4 = new byte [4];
Byte [] bufc81 = new byte [81];
// Total number
In. Read (bufc4, 0, 4 );
Int Len = (INT) bufc4 [3] <24) + (INT) bufc4 [2] <16) + (INT) bufc4 [1] <8) + (INT) bufc4 [0];
For (INT I = 0; I <Len; I ++)
...{
Question Ti = new question ();
// Code
In. Read (bufc4, 0, 4 );
Ti. code = (long) bufc4 [3] <24) + (long) bufc4 [2] <16) + (long) bufc4 [1] <8) + (long) bufc4 [0]);
// Time
In. Read (bufc4, 0, 4 );
Sharedpreferences sp = me. getpreferences (context. mode_world_readable );
Ti. Time = sp. getlong (long. tostring (TI. Code), 0 );
// Data
In. Read (bufc81, 0, 81 );
For (Int J = 0; j <81; j ++) Ti. Data [J] = bufc81 [J];
Me. tilist. Add (Ti );
}
In. Close ();
}
Catch (exception ex )...{
Return false;
}
Finally ...{
Try... {in. Close ();} catch (exception e )...{}
}
Return true;
}
The most troublesome reason here is that Java does not have the unsigned type, so it will overflow and be depressing. This is not a solution. You can only generate the question library file and pay attention to it, the question library file cannot be shared with the Brew platform.
The second is to save the record. On the Brew platform, I directly use a file to read and write the record, but Android cannot, because ti. dat is loaded from resources, so it can only be static and cannot be modified. The record can only be placed in preferences. The Code is as follows: public static Boolean savetilist (mainactivity me)
...{
Try
...{
Sharedpreferences sp = me. getpreferences (context. mode_world_writeable );
Question Ti = me. gridview. Ti;
Sp. Edit (). putlong (long. tostring (TI. Code), Ti. time );
Sp. Edit (). Commit ();
}
Catch (exception ex )...{
Return false;
}
Return true;
}
Sharepreferences can be saved by key-value. Therefore, if the key uses the code of the question, the value is the time it takes to solve the problem.
Android cannot directly access files in the app directory, so it cannot store data files in the program directory for reading and writing as brew does.
The two functions openfileoutput and openfileinput provided by activity are not convenient, although they can be used to read and write files.
In addition, it is not convenient to use SQLite because it is of little use to get these things on mobile phones.