java 字节流字符流读写
来源:互联网 发布:2016年双11淘宝交易额 编辑:程序博客网 时间:2024/05/17 02:01
字节流和字符流的区别:
字节流就是可以读取图片,文本,视频等二进制数据;字符流只能读取纯文本文件。
字节流读写示例:
public static void main(String[] args) {String path = "D:/study_project/1.jpg";String path1 = "D:/study_project/2.jpg";String path2 = "D:/study_project/java-io-test.txt";String path3 = "D:/study_project/java-io-test-to.txt";//readTxt(path2);writeFile(path2,path3);}/** * 文件的写入: * @param fromPath 源 * @param toPath 目标 */public static void writeFile(String fromPath,String toPath) {BufferedInputStream fis = null;BufferedOutputStream fos = null;int len = 2;byte[] buffer = new byte[len];try {fis = new BufferedInputStream(new FileInputStream(fromPath));fos = new BufferedOutputStream(new FileOutputStream(toPath));while ((fis.read(buffer, 0, len)!=-1) && buffer.length > 0) {fos.write(buffer, 0, buffer.length);}} catch (FileNotFoundException e) {e.printStackTrace();System.out.println("文件没有找到");} catch (IOException e) {e.printStackTrace();System.out.println("文件读取异常");} finally {try {fis.close();fos.close();} catch (Exception e2) {System.out.println(e2);}}}
public static void main(String[] args) {String path = "D:/study_project/1.jpg";String path1 = "D:/study_project/2.jpg";String path2 = "D:/study_project/java-io-test.txt";String path3 = "D:/study_project/java-io-test-to.txt";readChar(path2,path3);}public static void readChar(String fromPath,String toPath){BufferedReader read = null;BufferedWriter write = null;int len = 20;char[] buffer = new char[len];try {read = new BufferedReader(new InputStreamReader(new FileInputStream(fromPath), "utf8"));write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toPath),"utf8"));while ((read.read(buffer, 0, len)!=-1) && buffer.length > 0) {write.write(buffer, 0, len);}write.flush();} catch (FileNotFoundException e) {e.printStackTrace();System.out.println("文件没有找到");} catch (IOException e) {e.printStackTrace();System.out.println("文件读取异常");} finally {try {read.close();write.close();} catch (Exception e2) {System.out.println(e2);}}}
0 0
- java 字节流字符流读写
- Java中的文件读写字节流,字符流
- java中字节流与字符流的读写方式
- JAVA 字节流和字符流度读写的区别
- JAVA 字节流和字符流度读写的区别
- JAVA字节字符流
- 字符流字节流读写用例
- 使用字节流读写中文字符
- Java 字节流读写文件
- Java字节流读写文件
- java4Android(15)字节流大文件读写,字符流读写
- java 字符流 读写
- Java字节、二进制、字节流、字符
- java字符流字节流
- java字节流、字符流
- java字节流字符流
- Java 字节流,字符流
- java 字节流 字符流
- 我不是产品经理-005
- SSM框架 +MYSQL数据库 配置事务控制的方法和注意点
- 文字 jquery文字动画插件LetterFX 7种不同变换效果
- node 模块 fs-extra
- iptables 使用详解
- java 字节流字符流读写
- 硬件设计规范
- 【转】设置本地wamp环境挂载多站点同时运行
- DNS 原理入门
- 试说明进程在三个基本状态之间转换的典型原因。
- Java中HashMap遍历的两种方式
- 栈与队列
- IIOP.net 编译多仓库IDL 失败
- UVA10112Myacm Triangles