Java “错误”;“未找到文件”;即使在创建文件后也会抛出
我的代码相当长,但基本上,每次用户单击一个按钮时,它都会运行两种方法—从文件中读取对象数组。然后,它将一个新人的详细信息附加到此数组,然后第二个方法写入该文件。第一次,在logcat中,我得到了错误“未找到文件”-这是意料之中的,因为我还没有创建文件。第二次点击,同样的错误。反复出现同样的错误。所以,我推断,有些地方出了问题,因为否则它不会抛出错误“未找到文件”-文件是上次写入时创建的。 我的活动代码:Java “错误”;“未找到文件”;即使在创建文件后也会抛出,java,android,eclipse,file,filenotfoundexception,Java,Android,Eclipse,File,Filenotfoundexception,我的代码相当长,但基本上,每次用户单击一个按钮时,它都会运行两种方法—从文件中读取对象数组。然后,它将一个新人的详细信息附加到此数组,然后第二个方法写入该文件。第一次,在logcat中,我得到了错误“未找到文件”-这是意料之中的,因为我还没有创建文件。第二次点击,同样的错误。反复出现同样的错误。所以,我推断,有些地方出了问题,因为否则它不会抛出错误“未找到文件”-文件是上次写入时创建的。 我的活动代码: package com.example.partyorganiser; import j
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;
}
package com.example.partyorganiser;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.OutputStreamWriter;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.array;
导入android.os.Bundle;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.util.Log;
导入android.view.Menu;
导入android.view.view;
导入android.widget.EditText;
公共类AddPeople扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u add\u people);
}
///Addpeople是将人员添加到arraylist的活动,arraylist将被读取然后写回文件
///然后,它将以类似CSV的格式将其写入设备内部内存中的文件。
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\u列表,菜单);
返回true;
}
公共void addpeople\u action\u save(视图){
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();///将编辑文本解析为字符串。
字符串person_number=et_addpeople_number.getText().toString();
///下一节将把这两个字符串作为CSV格式写入一个内部文件。
///例:比尔,0123567898
///这实际上会在将字符串写回文件之前将其附加到字符串的arraylist中。
ArrayList peopledetails_return=Readpeopledetails();///在读取不存在的文件时,可能会在此处抛出错误。
///但不会停止正常的程序流。。。。
PeopleDetails person_details=新的PeopleDetails(“,”);
person\u details.name=person\u name;
person\u details.number=person\u number;
///需要将arraylist写回文件。如果存在,将覆盖(arraylist中包含以前的数据)
///或者,如果不存在,将写入新文件。
///新方法(PeopleNumber、PeopleName、Filename)
///将写入文件。
}
///排序方法:
///可以按字母顺序排列姓名和数字,
public void addpeople\u开关菜单(视图){
意向开关\菜单=新意向(此,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.settText(null);
启动触觉(开关菜单);
}
public void peopledetails\u write(ArrayList peopledetails\u文件){
////数字是数字的数组列表。
///姓名是人名的数组列表。
///写进文件中,比如01235678908,丹尼尔;01245645123,鲍勃·马利;等等。
///像一个CSV文件。
试一试{
FileOutputStream outputstream\u people=openFileOutput(“PeopleDetailsFile”,MODE\u PRIVATE);
OutputStreamWriter osw=新的OutputStreamWriter(outputstream_人);
String filestring=“”;///初始化写入文件的filestring。
for(PeopleDetails人员:PeopleDetails\u文件){
字符串person_detail_String=“”;
person\u detail\u string=person.name+“,”+person.number;
filestring=filestring+person_detail_string+“;”;
}
}
捕获(IOE异常){
Log.e(“ERROR”,e.toString());
}
}
公共ArrayList Readpeopledetails(){
String filereadfrom=“”;///声明要将文件内容读入的字符串。然后拆分为peopledetails。
///初始化字符串的两个ArrayList。一个用于名称,一个用于数字。s将与并行数组一样使用。
试一试{
InputStream filestream=openFileInput(“PeopleDetailsFile”);
如果(filestream!=null){///第一次未创建文件。稍后将在写回文件时创建。
InputStreamReader InputStreamReader=新的InputStreamReader(文件流);
BufferedReader BufferedReader=新的BufferedReader(inputStreamReader);
字符串receiveString=“”;
StringBuilder StringBuilder=新的StringBuilder();
而((receiveString=bufferedReader.readLine())!=null){
stringBuilder.append(receiveString);
}
filereadfrom=stringBuilder.toString();
///使用两个分隔符:;“和”
///基于“;”进行拆分以将人员作为字符串,放入数组中
///然后根据“,”拆分该数组中的所有字符串,并将每个字符串放入数组peopledetails中
字符串[]parsearray=filereadfrom.split(“;”);
ArrayList peopledetails_array=new ArrayList();
for(字符串personstring:parsearray){
String[]split=personstring.split(“,”);
PeopleDetails PeopleDetails\u unit=新的PeopleDetails(“,”);
peopledetails_unit.name=split[0];
peopledetails_unit.number=拆分[1];
人物细节_
ArrayList<PeopleDetails> peopledetails_return = Readpeopledetails()
InputStream filestream = openFileInput("PeopleDetailsFile");
551 public abstract FileOutputStream openFileOutput(String name, int mode)
552 throws FileNotFoundException;
FileOutputStream outputstream_people = openFileOutput("PeopleDetailsFile", MODE_PRIVATE);