IO操作

IO操作

字符流:
Reader--FileReader(InputStreamReader)--BufferedReader--LineNumberReader
Writer--FileWriter(OutputStreamWriter)--BufferedWriter

字节流:
InputStream--FileInputStream--BufferedInputStream
OutputStream--FileOutputStream--BufferedOutputStream

----------------------------------------

转换流:
InputStreamReader
    字节流-->字符流
    用于將字节流转成字符流,是Reader的子类

OutputStreamWriter
    字符流-->字节流
    是字符流通向字节流的桥梁,是Writer的子类

需要加入指定编码表utf-8。而指定的编码表只有转换流可以指定。
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d.txt"),"UTF-8");

手机文本文件的复制

//------------------------------------
// 文件的创建
//------------------------------------
File phoneDir;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
    phoneDir = Environment.getExternalStorageDirectory();
} else {
    phoneDir = this.getFilesDir();
}
File dir = new File(phoneDir + File.separator + "IODemo");
dir.mkdirs();

File file1 = new File(dir, "demo001.txt");
File file2 = new File(dir, "demo002.txt");
try {
    file1.createNewFile();
} catch (IOException e) {
    e.printStackTrace();
}

//------------------------------------
// 文件的复制
//------------------------------------
FileReader fr = null;
FileWriter fw = null;
try {
    fr = new FileReader(file1.toString());// 字符输入流
    fw = new FileWriter(file2.toString());// 字符输出流

    char[] buf = new char[1024];
    int len = 0;

    while ((len = fr.read(buf)) != -1) {
        fw.write(buf, 0, len);
    }
} catch (Exception e) {
    e.printStackTrace();
}finally{
    if(fr!=null){
        try {
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if(fw!=null){
        try {
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

把Assets目录下的文件拷贝到手机内存中

/**
 * 把Assets目录下的文件拷贝到手机内存中
 */
public void copyAssetFile(String copyFileName) {
    // ------------------------------------
    // 获取手机存储目录
    // ------------------------------------
    File phoneDir;
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        phoneDir = Environment.getExternalStorageDirectory();
    } else {
        phoneDir = this.getFilesDir();
    }

    // ------------------------------------
    // 判断文件是否存在
    // ------------------------------------
    File outFile = new File(phoneDir, copyFileName);
    if (outFile.exists()) {
        return;
    }

    // ------------------------------------
    // 文件的复制
    // ------------------------------------
    FileOutputStream out = null;
    InputStream in = null;
    try {
        out = new FileOutputStream(outFile);// 字节输出流
        in = this.getAssets().open(copyFileName);// 字节输入流

        int len = 0;
        byte[] buffer = new byte[1024];

        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
} 

把Assets目录下mp3复制到SD卡

public class MainActivity extends Activity {

    private File outFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //------------------------------------
        // 准备文件
        //------------------------------------
        File phoneDir;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            phoneDir = Environment.getExternalStorageDirectory();
        } else {
            phoneDir = this.getFilesDir();
        }
        outFile = new File(phoneDir, "do.mp3"); 


        //------------------------------------
        // 开始复制
        //------------------------------------
        long start = System.currentTimeMillis();
        copyFile(outFile);
        long end = System.currentTimeMillis();
        Log.d("ziru", "end-start=" + (end - start));

    }

    public void copyFile(File outFile) {
        try {
            BufferedOutputStream bufos = new BufferedOutputStream(
                    new FileOutputStream(outFile));// 字节输出流
            BufferedInputStream bufis = new BufferedInputStream(getResources()
                    .getAssets().open("do.mp3"));// 字节输入流

            int buffer = 0;
            while ((buffer = bufis.read()) != -1) {
                bufos.write(buffer);
            }

            bufos.close();
            bufis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值