android IO流操作文件(存储和读取)

存储文件:

public class FileOperate extends Activity {
    private static final String FILENAME = "mydata.txt" ;// 设置文件名称
    private static final String DIR = "ljpdata" ;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.main);                // 调用布局文件
        if(Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)){                // 如果sdcard存在
            File file = new File(Environment
                    .getExternalStorageDirectory().toString()
                    + File.separator
                    + DIR + File.separator + FILENAME) ;    // 定义File类对象
            if (! file.getParentFile().exists()) {            // 父文件夹不存在
                file.getParentFile().mkdirs() ;             // 创建文件夹 
            }
            PrintStream out = null ;                        // 打印流对象用于输出
            try {
                out = new PrintStream(new FileOutputStream(file, true));    // 追加文件
                out.println("山东大学软件学院(SDU,www.sdu.edu.cn),讲师:学生");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (out != null) {
                    out.close() ;                            // 关闭打印流
                }
            }
        } else {    // SDCard不存在,使用Toast提示用户
            Toast.makeText(this, "保存失败,SD卡不存在!",Toast.LENGTH_LONG).show();
        }
    }
}

读取文件:

public class FileOperate extends Activity {
    private static final String FILENAME = "mydata.txt" ;    // 设置文件名称
    private static final String DIR = "ljpdata" ;            // 设置保存文件夹
    private TextView msg = null ;                            // 文本显示
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.main);                // 调用布局文件
        this.msg = (TextView) super.findViewById(R.id.msg) ;
        if(Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)){                // 如果sdcard存在
            File file = new File(Environment
                    .getExternalStorageDirectory().toString()
                    + File.separator
                    + DIR + File.separator + FILENAME) ;    // 定义File类对象
            if (! file.getParentFile().exists()) {            // 父文件夹不存在
                file.getParentFile().mkdirs() ;             // 创建文件夹 
            }
            Scanner scan = null ;                            // 扫描输入
            try {
                scan = new Scanner(new FileInputStream(file)) ;    // 实例化Scanner
                while(scan.hasNext()){                            // 循环读取
                    this.msg.append(scan.next() + "
") ;        // 设置文本
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (scan != null) {
                    scan.close() ;                            // 关闭打印流
                }
            }
        } else {    // SDCard不存在,使用Toast提示用户
            Toast.makeText(this, "读取失败,SD卡不存在!",Toast.LENGTH_LONG).show();
        }
    }
}

Android将图像转换成流存储与将流转换成图像

//take the image to byte[]
byte[] byteIcon = userIcon;

//将字节数组即头像从二进制流转换成drawable
if(byteIcon != null){
  ByteArrayInputStream bais = new ByteArrayInputStream(byteIcon);
   userIconDrawable = Drawable.createFromStream(bais, "image");
}
BitMap userIcon;

//将bitmap转换成drawable
ByteArrayOutputStream os = new ByteArrayOutputStream();
//参数1转换类型,参数2压缩质量,参数3字节流资源        
userIcon.compress(CompressFormat.PNG, 100, os);
//将Drawable 转成bitmap
BitmapDrawable tempDrawable = (BitmapDrawable) DrawableImage;
tempDrawable.getBitmap();

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值