I am trying to save a file (and then read it later) in java (android) using the following
FileInputStream fis = openFileInput(filename);
and then maybe use BufferedReader/writer. Anyways, I am trying to save String and numbers and I was wondering what would be the best method to write and read from I/O for such case?
I was about to do the following for reading
FileInputStream fis = openFileInput(filename);
InputStreamReader inputStreamReader = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
and for writing
FileOutputStream fos = openFileOutput(filename, 20);
OutputStreamWriter outStreamReader = new OutputStreamWriter(fos);
BufferedWriter bufferedWriter = new BufferedWriter(outStreamReader);
but I noticed that the readLine will always return string of the line. so I have to go throught the conversion of Strings to Integer for some lines. Is this an efficient way of doing it (or correct way)? I feel I am missing something
Thank you
Unless you have multiple-line strings or strings that can be mistaken for integers or a file with a very complex structure, just write the text file as proposed above. The inefficiency is not worth losing the benefit of portability and readability.
Other good options for more complex files are JSON or XML or Java properties files (for which good libraries exist that you can use).
Only go to a binary format if you have a really good reason (and having to call Integer.parseInt and saving 3 bytes is not a good reason).