Androidファイル操作IOテクノロジー

3647 ワード

    /**

     *        

     * @param inStream

     * @return

     */

    public static byte[] read(InputStream inStream) throws Exception{

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];

        int len = 0;

        while( (len = inStream.read(buffer)) != -1 ){

            outStream.write(buffer, 0, len);

        }

        inStream.close();

        return outStream.toByteArray();

    }
    /**

     * 

     * @param     

     * @param     

     * @throws     

     */

    public void save(String fileName, String fileContext) throws Exception {

        //       :               ,           ,               ,                 

        FileOutputStream outStream = context.openFileOutput(fileName,

                Context.MODE_PRIVATE);

        outStream.write(fileContext.getBytes());

        outStream.close();

    }



    /**

     * 

     * @param     

     * @param     

     * @throws     

     */

    public void saveToSDCard(String fileName, String context) throws Exception {

        //       :               ,           ,               ,                 

        File file = new File(Environment.getExternalStorageDirectory(),

                fileName);

        FileOutputStream outStream = new FileOutputStream(file);

        outStream.write(context.getBytes());

        outStream.close();

    }