My code is quite lengthy, but basically, each time a user clicks a button, it runs two methods - read an array of objects from a file. It then appends a new person's details to this array, then the second method writes to the file. First time, in logcat, I get the error "file not found" - expected, cos I haven't created the file yet. Second click, same error. Repeatedly, same error. therefore, I deduce, that something is going wrong, because otherwise it would not thrown an error "file not found" - file was created when writing last time. My activity code:
package com.example.partyorganiser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class AddPeople extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_people);
}
///Addpeople is the activity to add people to an arraylist, which is read and then written back to the file
///It will then write this to a file in the internal memory of the device, in CSV like format.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_list, menu);
return true;
}
public void addpeople_action_save(View view) {
EditText et_addpeople_name = (EditText) findViewById(R.id.addpeople_name);
EditText et_addpeople_number = (EditText) findViewById(R.id.addpeople_number);
String person_name = et_addpeople_name.getText().toString();///parses from the EditText to a string.
String person_number = et_addpeople_number.getText().toString();
///Next section is writing the two strings to a internal file, as a CSV format.
///eg: Bill, 0123567898
///this will actually append the string to an arraylist of strings, before writing it back to the file.
ArrayList<PeopleDetails> peopledetails_return = Readpeopledetails();///throws error here, presumably, when reading a file that doesn't exist.
///but doesn't stop normal program flow....
PeopleDetails person_details = new PeopleDetails("", "");
person_details.name = person_name;
person_details.number = person_number;
///need to write arraylists back to file. Will overwrite if exists (contains previous data in arraylist)
///or will write a new file if it doesn't exist.
///New method(PeopleNumber, PeopleName, Filename)
///will write to file.
}
///SORT METHOD:
///Could sort the names and numbers by alphabetical order,
public void addpeople_switchmenu(View view){
Intent switch_menu = new Intent(this, MenuList.class);
EditText et_addpeople_name = (EditText) findViewById(R.id.addpeople_name); et_addpeople_name.setText(null);
EditText et_addpeople_number = (EditText) findViewById(R.id.addpeople_number);et_addpeople_number.setText(null);
startActivity(switch_menu);
}
public void peopledetails_write(ArrayList<PeopleDetails> peopledetails_file) {
////numbers is the arraylist of numbers.
///names is the arraylist of names of people.
///Written to file like 01235 678 908, Daniel; 01245 645 123, Bob Marley; etc.
///Like a CSV file.
try{
FileOutputStream outputstream_people = openFileOutput("PeopleDetailsFile", MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(outputstream_people);
String filestring = ""; ///initializes filestring, which is written to the file.
for(PeopleDetails person : peopledetails_file){
String person_detail_string = "";
person_detail_string = person.name + "," + person.number;
filestring = filestring + person_detail_string + ";";
}
}
catch (IOException e) {
Log.e("ERROR", e.toString());
}
}
public ArrayList<PeopleDetails> Readpeopledetails(){
String filereadfrom = "";///declares a string to read file contents into. then split into peopledetails.
///Initialises two arraylists of string. one for names and one for numer.s will be used like paralell arrays.
try{
InputStream filestream = openFileInput("PeopleDetailsFile");
if (filestream != null){///first time file is not created. Later will be created, when writing back to the file.
InputStreamReader inputStreamReader = new InputStreamReader(filestream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
filereadfrom = stringBuilder.toString();
///using two delimiters: ";" and ","
///split based on ";" to get people as a string, put in array
///then split all strings in that array, based on "," and place each into the array peopledetails
String[] parsearray = filereadfrom.split(";");
ArrayList<PeopleDetails> peopledetails_array = new ArrayList<PeopleDetails>();
for(String personstring: parsearray){
String[] split = personstring.split(",");
PeopleDetails peopledetails_unit = new PeopleDetails("", "");
peopledetails_unit.name= split[0];
peopledetails_unit.number = split[1];
peopledetails_array.add(peopledetails_unit);
///neatly initializes and writes to the peopledetails_array in one section.
}
return peopledetails_array; }
}
catch(FileNotFoundException e) {
Log.e("File not found" , e.toString());
}
catch(IOException e){
Log.e("Can't read file" , e.toString());
}
return null;
}
public ArrayList<PeopleDetails> sortList(ArrayList<PeopleDetails> list){
PeopleDetails[] array = (PeopleDetails[]) list.toArray();
for (int i = 0; i< (array.length - 1) ; i++){
for (int j = 0; j < (array.length - 1); j++){
if(array[j].name.compareTo(array[j + 1].name) > 0){
PeopleDetails transfer = array[j];
array[j] = array[j + 1];
array[j + 1] = transfer;
}
}///based off the common bubble sort algorithm - easy to implement.
///if it was a much much bigger data system, this would be inappropriate, with Order(n**2),
///as it uses two for loops each n times.
}
ArrayList<PeopleDetails> returnlist = new ArrayList<PeopleDetails>();
for(PeopleDetails item : array){
returnlist.add(item);
}
return returnlist;
}
}
Rather lengthy....
Thanks for the help!! :)
onCreate
(only once) and declare them as instance variables. – Raghunandan