Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android应用程序中找不到文件_Android_File_File Not Found - Fatal编程技术网

在Android应用程序中找不到文件

在Android应用程序中找不到文件,android,file,file-not-found,Android,File,File Not Found,我正在开发一个android应用程序,它可以作为一个滚动手册,我创建了StudentStore类,它可以将自定义列表加载并保存到文件中。我的问题是,文件不是在SD卡中创建的;我试过一些方法,但似乎都不管用 public class StudentStore { public StudentStore(){ Log.d("TAG", "student store constructor"); } public SortedDoublyList<Student>

我正在开发一个android应用程序,它可以作为一个滚动手册,我创建了StudentStore类,它可以将自定义列表加载并保存到文件中。我的问题是,文件不是在SD卡中创建的;我试过一些方法,但似乎都不管用

public class StudentStore {
public StudentStore(){ 
    Log.d("TAG", "student store constructor"); 
}
        public SortedDoublyList<Student> loadStudents() throws FileNotFoundException { 
        // sdCard trajectory
        File sdCard = Environment.getExternalStorageDirectory(); 
        File directory = new File (sdCard.getAbsolutePath() + "/dir1/dir2"); 
        // Creates directory from path held by the variable named directory
        directory.mkdirs();

        File rollBookFile = new File(directory.getAbsolutePath()+"StudentsInfo.txt"); 
        SortedDoublyList<Student> studentList = null; 
        FileInputStream fileInfoIn = null; 
        ObjectInputStream studentInfoIn = null;

        try { 

            fileInfoIn = new FileInputStream(rollBookFile);
            studentInfoIn = new ObjectInputStream(fileInfoIn); 
            studentList = (SortedDoublyList<Student>) studentInfoIn.readObject();

            studentInfoIn.close();
        } 
        catch (IOException ex) { 
            FileOutputStream fileInfoOut = new FileOutputStream(rollBookFile);
        } catch (ClassNotFoundException ex) {

        } 

        // Empty file
        if(studentList == null){ 
            studentList = new SortedDoublyList<Student>();
            saveStudents(studentList);
        } 
        return studentList;
    } 

        public void saveStudents(SortedDoublyList<Student> studentList) { 
        // sdCard trajectory
        File sdCard = Environment.getExternalStorageDirectory(); 
        File directory = new File (sdCard.getAbsolutePath() + "/dir1/dir2");

        // Creates directory from path held by the variable named directory
        directory.mkdirs();

        // Constructs file from newly obtained directory pointing towards StudentsInfo.txt
        File rollBookFile = new File(directory.getAbsolutePath()+"StudentsInfo.txt"); 

        FileOutputStream fileInfoOut = null; 
        ObjectOutputStream studentInfoOut = null; 
        try { 
            fileInfoOut = new FileOutputStream(rollBookFile); 
            studentInfoOut = new ObjectOutputStream(fileInfoOut);
            studentInfoOut.writeObject(studentList); 
            studentInfoOut.close(); 
            System.out.println("Object Persisted"); 
        } 
        catch (IOException ex) { 
            ex.printStackTrace(); 
        } 
    } 
公共类学生商店{
公共学生商店(){
Log.d(“标签”、“学生商店构造函数”);
}
public SortedDoublyList loadStudents()引发FileNotFoundException{
//SD卡轨迹
文件sdCard=Environment.getExternalStorageDirectory();
文件目录=新文件(sdCard.getAbsolutePath()+“/dir1/dir2”);
//从名为directory的变量所持有的路径创建目录
mkdirs()目录;
File rollBookFile=新文件(directory.getAbsolutePath()+“StudentsInfo.txt”);
SortedDoublyList studentList=null;
FileInputStream fileInfoIn=null;
ObjectInputStream StudentInfo=null;
试试{
fileInfoIn=newfileinputstream(rollBookFile);
StudentInfo=新对象输入流(fileInfoIn);
studentList=(SortedDoublyList)StudentInfo.readObject();
studentInfo.close();
} 
捕获(IOEX){
FileOutputStream fileInfoOut=新的FileOutputStream(rollBookFile);
}捕获(ClassNotFoundException ex){
} 
//空文件
如果(studentList==null){
studentList=new SortedDoublyList();
保存学生(学生名单);
} 
返回学生名单;
} 
public void saveStudents(SortedDoublyList studentList){
//SD卡轨迹
文件sdCard=Environment.getExternalStorageDirectory();
文件目录=新文件(sdCard.getAbsolutePath()+“/dir1/dir2”);
//从名为directory的变量所持有的路径创建目录
mkdirs()目录;
//从新获得的指向StudentsInfo.txt的目录构造文件
File rollBookFile=新文件(directory.getAbsolutePath()+“StudentsInfo.txt”);
FileOutputStream fileInfoOut=null;
ObjectOutputStream StudentInfo=null;
试试{
fileInfoOut=newfileoutputstream(rollBookFile);
studentInfoOut=新对象输出流(fileInfoOut);
StudentInfo.writeObject(studentList);
studentfout.close();
System.out.println(“对象持久化”);
} 
捕获(IOEX){
例如printStackTrace();
} 
} 

在写入数据之前,尝试添加
createNewFile()
方法来创建文件:

try { 
    rollBookFile.createNewFile();

    fileInfoOut = new FileOutputStream(rollBookFile); 
    studentInfoOut = new ObjectOutputStream(fileInfoOut);
    studentInfoOut.writeObject(studentList); 
    studentInfoOut.close(); 
    System.out.println("Object Persisted"); 
} 

你添加了谢谢!这正是我所缺少的!我没有意识到我犯了这么简单的错误!谢谢!我缺少了那行代码和android权限