我的代码抛出"FileNotFoundException"
错误。读取和写入文件的正确方法是什么?是不是因为文件尚未创建而引发此错误,还是因为对文件读取和文件写入的误解?谢谢
我的代码:
public class EditPeople_ListFragment extends ListFragment {
public Pair<List<String>, List<String>> ReadPeopleFile(FileReader peopleReader){
///Soon parsed into a string list, by delimiting and separating into two string arrays,
char characterList[] = new char[10000]; // I'm assuming that 1000 is enough here
int numChars = 0;
try {
numChars = peopleReader.read(characterList);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String parseString = new String(characterList, 0, numChars);///Creates a string parseString.
List<String> parseString_array = new ArrayList<String>();///Initialises a list of strings
parseString_array = Arrays.asList(parseString.split(";"));///Splits the parseString into the parseString_array
List <String> mobileNumber = new ArrayList<String>();///Initialises a list of strings, meant to be mobile numbers.
List <String> name = new ArrayList<String>();///Initialises a list of string, meant to be names
String[] parts = new String[2];
for (String peopleDetails : parseString_array){///sets up a loop for each of the strings in parseString_array, to be split into mobile numbers, and name.
///delimiter: ";" is for each peopleDetail string. Delimiter: "," separates mobile number and names in each peopleDetails string.
parts = peopleDetails.split(",");
mobileNumber.add(parts[0]);
name.add(parts[1]);
}
try {
peopleReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new Pair(mobileNumber, name);
///Then use on return value nameList = ------.getname();
///and use --------.getmobileNumber();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FileReader peopleFile_Reader = new FileReader(new File(getFilesDir(), "PartyApp_PeopleFile"))
Pair<List<String>, List<String>> returnedpair = ReadPeopleFile(peopleFile_Reader);
List<String> mobileList = returnedpair.first;
List<String> nameList = returnedpair.second;
List<String> people_List = null;
if(nameList.size() == mobileList.size()){///Checks the list are the same length; should be, but need to add a check to AddPeople, to check both fields are filled and correct
for (int i = 0; i < nameList.size(); ++i) {///
people_List.add(i, nameList.get(i) + "," + mobileList.get(i));
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), R.id.ListView_EditPeopleFragment, people_List);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}