安卓文件读写 InputStream/InputStreamReader/BufferedReader OutputStream/OutputStreamWriter 字节数组和字符数组互转

第三方安卓程序都安装在"/data/data/包名"目录下该目录下有files文件夹,和chche文件夹
files文件夹用来盛装应用的数据,在系统中叫做应用的数据,”/data/data/包名“是应用自己的空间,
不需要权限就可以操作此文件夹的内容,
cache文件夹是应用的缓存,一些清理软件可以清除掉软件的cache文件夹的内容

 

1、InputStream、OutputStream

处理字节流的抽象类

InputStream 是字节输入流的所有类的超类,一般我们使用它的子类,如FileInputStream等.

OutputStream是字节输出流的所有类的超类,一般我们使用它的子类,如FileOutputStream等.

  或者 InputStream in = new FileInputStream(String fileName);//读取文件中的数据。可以看出 FileInputStream 为InputStream的子类。 
主要方法 :int read();//读取单个字符。 
                 int read(char []cbuf);//将读取到的字符存到数组中。返回读取的字符数。

   //1读取文件的数据到字节流inputStream
    InputStream inputStream = new FileInputStream("D:\\demo.txt");//读取文件的数据到字节流inputStream。

InputStream.class
    
public int read() throws IOException {
        throw new RuntimeException("Stub!");
    }

    public int read(byte[] b) throws IOException {
        throw new RuntimeException("Stub!");
    }

    public int read(byte[] b, int off, int len) throws IOException {
        throw new RuntimeException("Stub!");
    }

2、InputStreamReader  OutputStreamWriter

处理字符流的抽象类

InputStreamReader 是字节流通向字符流的桥梁,它将字节流转换为字符流.

OutputStreamWriter是字符流通向字节流的桥梁,它将字符流转换为字节流.

   //2将字节流inputStream转换成字符流inputStreamReader
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);//将字节流inputStream转换成字符流inputStreamReader。

InputStreamReader.class
 
public int read() throws IOException {
        throw new RuntimeException("Stub!");
    }

    public int read(char[] cbuf, int offset, int length) throws IOException {
        throw new RuntimeException("Stub!");
    }


  
    //3读取字符流inputStreamReader的数据到字符数组ch中,并返回字符数组ch的真实的字符数
    char []ch = new char[1024];
    int len = isr.read(ch);

 

 

3、BufferedReader BufferedWriter

BufferedReader 由Reader类扩展而来,提供通用的缓冲方式文本读取,readLine读取一个文本行,

从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。

BufferedWriter  由Writer 类扩展而来,提供通用的缓冲方式文本写入, newLine使用平台自己的行分隔符,

将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。

                    File file = new File ("/sdcard/ClientConversation_dispatch_handleRemoteASRResult.txt");
                    FileInputStream fin = new FileInputStream (file);
                    // 下面对获取到的输入流进行读取
                    reader = new BufferedReader (new InputStreamReader (fin));
                    StringBuilder response = new StringBuilder ();
                    String line;
                    Log.d ("MainActivity", "line : ");
                    while ((line = reader.readLine ()) != null) {
                        response.append (line);
                        Log.d ("MainActivity", "line : " + line);
                    }

1、字节数组转换为字符串

byte[] byBuffer = new byte[20];
... ...
String strRead = new String(byBuffer);
strRead=String.copyValueOf(strRead.toCharArray(), 0, byBuffer.length]);

String.class

    public String(byte[] bytes) {
        throw new RuntimeException("Stub!");
    }


2、字符串转换成字节数组
 

	byte[] byBuffer = new byte[200];
	String strInput="abcdefg";
	byBuffer= strInput.getBytes();

String.class
					
	public byte[] getBytes(String charsetName) throws UnsupportedEncodingException {
        throw new RuntimeException("Stub!");
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值