56.IO流

作者: SlideException | 来源:发表于2020-08-17 20:36 被阅读0次
/**
 * 每天一个知识点day56 TODO java基础 IO流
 *
 * 流:代表任何有能力产出数据的数据源对象或者是有能力接受数据的接收端对象
 * 流的本质:数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
 * IO
 * 字符流的由来: Java中字符是采用Unicode标准,一个字符是16位,
 * 即一个字符使用两个字节来表示。为此,JAVA中引入了处理字符的流。
 * 因为数据编码的不同,而有了对字符进行高效操作的流对象。
 * 本质其实就是基于字节流读取时,去查了指定的码表。
 * -字节流 每次读取(写出)一个字节,当传输的资源文件有中文时,就会出现乱码
 *  -InputStream字节输入流
 *      -FileInputStream
 *      -BufferInputStream
 *      -ByteArrayInputStream
 *      -DataInputStream 数据输入流,它是用来装饰其它输入流,
 *      它“允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型”
 *      -PipedInputStream 管道字节输入流,它和PipedOutputStream一起使用,
 *      能实现多线程间的管道通信
 *      -ObjectInputStream 用来提供对“基本数据或对象”的持久存储
 *  -OutputStream字节输出流
 *      -FileOutputStream
 *      -BufferedOutputStream
 *      -ByteArrayOutputStream
 *      -DataOutputStream
 *      -PipedOutputStream
 *      -ObjectOutputStream
 * -字符流 每次读取(写出)两个字节,有中文时,使用该流就可以正确传输显示中文。
 *  -Reader字符输入流
 *      -FileReader
 *      -BufferedReader
 *      -InputStreamReader
 *  -Writer字符输出流
 *      -FileWriter
 *      -BufferedWriter
 *      -OutputStreamWriter
 * <p>
 * 字节流和字符流使用情况:
 * 字符流和字节流的使用范围:字节流一般用来处理图像,视频,以及PPT,Word类型的文件。
 * 字符流一般用于处理纯文本类型的文件,如TXT文件等,
 * 字节流可以用来处理纯文本文件,但是字符流不能用于处理图像视频等非文本类型的文件。
 * <p>
 * 字节流和字符流的区别
 * 字节流没有缓冲区,是直接输出的,而字符流是输出到缓冲区的。因此在输出时,
 * 字节流不调用close()方法时,信息已经输出了,而字符流只有在调用close()方法关闭缓冲区时,
 * 信息才输出。要想字符流在未关闭时输出信息,则需要手动调用flush()方法
 * 读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。
 * 处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。
 */

private static final int      REQUEST_EXTERNAL_STORAGE = 1;
private static       String[] PERMISSIONS_STORAGE      = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE};

public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE);
    }
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    verifyStoragePermissions(this);
}

public void read(View view) {
    String s = getFileFromSdcard("a1.txt");
    Log.d("dww", s + "");
}

public final String content = "明日复明日" + "\r\n" + "明日何其多=====";

public void write(View view) {
    boolean a1 = saveContentToSdcard("a1.txt", content);
    Log.d("dww", a1 + "写");
}

public boolean saveContentToSdcard(String fileName, String content) {
    boolean flag = false;
    FileOutputStream fileOutputStream = null;
    // 获得sdcard卡所在的路径
    File file = new File(Environment.getExternalStorageDirectory(), fileName);
    // 判断sdcard卡是否可用
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        try {
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(content.getBytes());
            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return flag;
}

public String getFileFromSdcard(String fileName) {
    FileInputStream inputStream = null;
    // 缓存的流,和磁盘无关,不需要关闭
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    File file = new File(Environment.getExternalStorageDirectory(),
            fileName);
    if (Environment.MEDIA_MOUNTED.equals(Environment
            .getExternalStorageState())) {
        try {
            inputStream = new FileInputStream(file);
            int len = 0;
            byte[] data = new byte[1024];
            while ((len = inputStream.read(data)) != -1) {
                outputStream.write(data, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return new String(outputStream.toByteArray());
}

相关文章

  • 56.IO流

  • 电商的四流体系

    四流:即商品流、信息流、资金流、物流 Ps: 三流:信息流、现金流、物流 四流:信息流、现金流、物流、商品...

  • JAVA API-day07

    A 基本的IO操作 文件流 缓冲流 节点流和处理流 对象流

  • 十五、Stream 流操作

    流的简单使用 流的获取 流的转换 将流做一些处理并返回一个流 抽取子流和连接流 流的转换 2 简单约简 ,终结流的...

  • Java学习——day 17

    主要内容 缓冲流 转换流 字节数组流 数据流 对象流 笔记详情 1. 缓冲流 Java中的流可以分为节点流和处理流...

  • IO流之节点流(文件流)

    IO流的分类 按操作的数据单位:字节流(8bit),字符流(16bit)---->对于文本文件使用字符流处理,对于...

  • Append 流、 Retract 流、 Upsert 流、动态

    Append-only 流: 仅通过 INSERT 操作修改的动态表可以通过输出插入的行转换为流。 Retract...

  • 心流

    心流心流心流

  • IO流 打印流

    一共可以分成3类1:纯字节流2:字符流3:混合流(混合流最好) 混合流 字符流 字节流

  • 流与文件-流

    写在书上 保存下来防止丢失

网友评论

      本文标题:56.IO流

      本文链接:https://www.haomeiwen.com/subject/cegglktx.html