I am trying to get all the data to save to a file "MovingAndMoreAppData.txt" when the save button is pressed then to load the data when the load button is pressed. I have spent hours looking up videos and forums on how to make this happen.
Both of the buttons work, except with the current load button code the app crashes when pressed.
I believe I have the save code correct and working but do not know how to test. I can not at all figure out how to get the load to work. I have tried multiple ways and nothing works and everything online is different ways then each other.
So can someone please let me know if the save code is correct and show me how to make the load work please?
else if (xID == R.id.saveJobButton) {
try {
//to save data to a file
FileOutputStream xFileOutput = openFileOutput("MovingAndMoreAppData.txt", MODE_PRIVATE);
OutputStreamWriter xOutputWriter = new OutputStreamWriter(xFileOutput);
xOutputWriter.write(
xStartMiles); //integer variable from a EditText view
//TODO after get xStartMiles working
/*xStartTime + "," +
xEndTime + "," +
xEndDriveCounter + "," +
xStartDriveCounter + "," +
xEndMiles + "," +
xTotalMiles + "," +
xDriveTime1 + "," +
xDriveTime2 + "," +
xDriveTime3 + "," +
xDriveTime4 + "," +
xDriveTime5 + "," +
xDriveTimeTotal + "," +
xEndDriveTime1 + "," +
xEndDriveTime2 + "," +
xEndDriveTime3 + "," +
xEndDriveTime4 + "," +
xEndDriveTime5 + "," +
xStartDriveTime1 + "," +
xStartDriveTime2 + "," +
xStartDriveTime3 + "," +
xStartDriveTime4 + "," +
xStartDriveTime5 + "," +
xTotalTime);
*/
xOutputWriter.flush();
xOutputWriter.close();
Toast.makeText(TimeLogActivity.this, "Data Saved", Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
Toast.makeText(TimeLogActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
//to load data from file
else if (xID == R.id.loadJobButton) {
Toast.makeText(TimeLogActivity.this, "button pressed", Toast.LENGTH_SHORT).show();
//need line below when loading data so it can display it
//xStartMilesInput.setText(String.valueOf(50));
File xFileToRead = getApplicationContext().getFileStreamPath("MovingAndMoreAppData.txt");
String lineFromFile;
if(xFileToRead.exists()) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(openFileInput("MovingAndMoreAppData.txt")));
while((lineFromFile = reader.readLine()) != null) {
StringTokenizer tokens = new StringTokenizer(lineFromFile, ",");
xStartMiles = Integer.parseInt(tokens.nextToken());
//set the EditText view to the integer value of the saved xStartMiles
xStartMilesInput.setText(String.valueOf(xStartMiles));
}
reader.close();
} catch (IOException e) {
Toast.makeText(TimeLogActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
Here is the LOGCAT:
2023-12-13 11:56:51.855 19740-19740 AndroidRuntime com...andmoving404.movingandmoreapp E FATAL EXCEPTION: main Process: com.moreandmoving404.movingandmoreapp, PID: 19740 java.lang.NumberFormatException: For input string: "??"
at java.lang.Integer.parseInt(Integer.java:604)
at java.lang.Integer.parseInt(Integer.java:650)
at com.moreandmoving404.movingandmoreapp.TimeLogActivity.onClick(TimeLogActivity.java:311)
at android.view.View.performClick(View.java:7441)
at android.view.View.performClickInternal(View.java:7418)
at android.view.View.access$3700(View.java:835)
at android.view.View$PerformClick.run(View.java:28676)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
2023-12-13 11:56:57.449 19740-19753 System com...andmoving404.movingandmoreapp W A resource failed to call close.