權限的開放,在AndroidManifest.xml加入

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

SDcard的寫入

File filename = new File(Environment.getExternalStorageDirectory(), "myfile.txt");
    try {
        FileOutputStream fout = new FileOutputStream(filename);
        fout.write("test".getBytes());
        fout.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
           e.printStackTrace();
    }

SDcard的讀取

try {
        FileInputStream fin = new FileInputStream(filename);
        BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
        String content = reader.readLine();
        reader.close();
        fin.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
arrow
arrow

    痞客興 發表在 痞客邦 留言(0) 人氣()