zoukankan      html  css  js  c++  java
  • Android文件操作IO技术

        /**
         * 读取输入流数据
         * @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();
        }
  • 相关阅读:
    angular2^ typescript 将 文件和Json数据 合并发送到服务器(1.客户端处理)
    错误的尝试:回射程序改进2
    XML Schema笔记
    回射程序改进1
    DTD笔记
    XML语法笔记
    判断IPv6地址合法性
    线程相关函数(POSIX线程):
    使用string实现一个用于储存那些太大而无法使用 long long 的数
    基本SCTP套接字编程常用函数
  • 原文地址:https://www.cnblogs.com/xiaoyao095/p/4072796.html
Copyright © 2011-2022 走看看