What path does “getApplicationContext().getFilesDir()” return?

后端 未结 5 1703
余生分开走
余生分开走 2020-12-17 23:07

I\'m doing a simple app in Android and in a certain part of the app I would like to create an Excel file and write in it. I\'ve already prepared everything to use jexcel lib

5条回答
  •  半阙折子戏
    2020-12-17 23:33

    This worked:

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            context = getApplicationContext();
            b = (Brain)load("brain.txt");
            if (b == null) {
                b = new Brain();
            }
            vocabulary = (ArrayList ) load("vocabulary.txt");
            if (vocabulary == null) {
                vocabulary = new ArrayList  ();
                vocabulary.add("I love you.");
                vocabulary.add("Hi!");
            }
            b.setRunning(true);
        }
    
    public Object load(String fileName) {
        File file = new File("/storage/emulated/0/Android/data/com.cobalttechnology.myfirstapplication/files/" + fileName);
        if (!file.exists()) {
            return null;
        }
        try {
            Object o;
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            o = ois.readObject();
            if (o == null) {
                System.out.println(fileName + " = null");
            }
            ois.close();
            fis.close();
            System.out.println("Loaded: " + fileName);
            return o;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }
        return null;
    }
    public void save(Object o, String fileName) {
        File file = new File("/storage/emulated/0/Android/data/com.cobalttechnology.myfirstapplication/files/" + fileName);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(o);
            oos.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

提交回复
热议问题