我的代码非常冗长,但基本上,每次用户单击按钮时,它都会运行两种方法 - 从文件中读取一组对象。然后它将新人的详细信息附加到此数组,然后第二个方法写入该文件。第一次,在logcat中,我收到错误“找不到文件” - 期望,因为我尚未创建该文件。第二次点击,同样的错误。反复,同样的错误。因此,我推断,出现了问题,因为否则它不会抛出错误“找不到文件” - 文件是在上次写入时创建的。 我的活动代码:
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;
}
}
相当冗长....
感谢您的帮助!! :)
答案 0 :(得分:0)
点击按钮
你有
ArrayList<PeopleDetails> peopledetails_return = Readpeopledetails()
在Readpeopledetails
InputStream filestream = openFileInput("PeopleDetailsFile");
551 public abstract FileOutputStream openFileOutput(String name, int mode)
552 throws FileNotFoundException;
尚未创建文件,或者您的内部手机存储中没有名称为PeopleDetailsFile
的文件。所以你得到FileNotFoundException
。
我看不到你在哪里调用peopledetails_write
FileOutputStream outputstream_people = openFileOutput("PeopleDetailsFile", MODE_PRIVATE);
您还需要写入数据并关闭FileOutputStream
如果再次点击它仍然没有创建。
在尝试打开文件之前,您需要FileOutputStream outputstream_people = openFileOutput("PeopleDetailsFile", MODE_PRIVATE);
创建一个文件。
此外,无需再次初始化视图。您可以在onCreate
初始化一次。
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal