四种读写方案IO流 (JAVA)
2016-06-06 16:24
393 查看
File类用于访问文件或目录的属性
流:指一连串流动的字符,是以先进先出的方式发送信息的通道。程序和数据源之间是通过流联系起来的。
第一套:字节流读取写入方案
FileInputStream :字节流方式读取文本文件
FileOutputStream:字节流写入硬盘
第二套:字符流读取写入方案
FileReader:字符流读取文本
FileWriter:字符流写入文本
第三套:<BufferedReader、BufferedWriter>一般和FileReader和FileWriter结合使用
BufferedReader:自定义缓存大小,读取文本 8192个char
BufferedWriter:写入文本
第四套:可以读取二进制(img图片等 )
DataInputStream:将本地的img加载到内存中
DataOutputStream:将内存中的二进制数据写入到硬盘上的某个文件中
注:在java中,byte数组和String字符串如何转换?
1、string 转 byte[]
2、byte[] 转 string
流:指一连串流动的字符,是以先进先出的方式发送信息的通道。程序和数据源之间是通过流联系起来的。
第一套:字节流读取写入方案
FileInputStream :字节流方式读取文本文件
FileInputStream fis=new FileInputStream("E:\\读取文件.txt"); byte[]bytes=new byte[1024]; int data; while((data=fis.read(bytes))!=-1) { String str=new String(bytes,0,data); System.out.println(str); } fis.close(); }
FileOutputStream:字节流写入硬盘
FileOutputStream fos=new FileOutputStream("E:\\1.txt"); String word="高考是人生的分水岭"; byte[] bytes = word.getBytes(); fos.write(bytes); fos.close(); System.out.println("写入成功!"); } }
第二套:字符流读取写入方案
FileReader:字符流读取文本
FileReader fr=new FileReader("E:\\读取文件.txt"); char[]chars=new char[1024]; int data; while((data=fr.read(chars))!=-1) { String str=new String(chars); System.out.println(str); } }
FileWriter:字符流写入文本
FileWriter fw=new FileWriter("E:\\2.txt"); fw.write("新的6月"); System.out.println("写入成功!"); fw.close(); }
第三套:<BufferedReader、BufferedWriter>一般和FileReader和FileWriter结合使用
BufferedReader:自定义缓存大小,读取文本 8192个char
FileReader fr=new FileReader("E:\\读取文件.txt"); BufferedReader br=new BufferedReader(fr); String line; while((line=br.readLine())!=null) { System.out.println(line); } br.close(); fr.close(); }
BufferedWriter:写入文本
FileWriter fw=new FileWriter("E:\\5.txt"); BufferedWriter bw=new BufferedWriter(fw); bw.write("OK!!"); bw.close(); fw.close(); System.out.println("写入成功!!"); }
第四套:可以读取二进制(img图片等 )
DataInputStream:将本地的img加载到内存中
FileInputStream fis=new FileInputStream("E:\\5.txt"); FileOutputStream fos=new FileOutputStream("D:\\55.txt"); DataInputStream dis=new DataInputStream(fis); DataOutputStream dos=null; byte[]bytes=new byte[1024]; int data; while((data=dis.read(bytes))!=-1) { dos=new DataOutputStream(fos); dos.write(bytes); } dos.close(); dis.close(); fos.close(); fis.close(); System.out.println("copy succes!!!"); }
DataOutputStream:将内存中的二进制数据写入到硬盘上的某个文件中
DataOutputStream out=null; DataInputStream dis=null; try { //创建输入流对象 FileInputStream fis=new FileInputStream("c:\\范宁.jpg"); dis=new DataInputStream(fis); //创建输出流对象 FileOutputStream outFile=new FileOutputStream("c:\\范宁小美女33.jpg"); out=new DataOutputStream(outFile); int temp=dis.read(); while (temp!=-1) { out.write(temp); temp=dis.read(); } System.out.println("复制成功"); fis.close(); outFile.close(); } catch (Exception e) { System.out.println("文件不存在"); }finally{ try { if (dis!=null) { dis.close(); } if (out!=null) { out.close(); } } catch (Exception e2) { e2.printStackTrace(); } }
注:在java中,byte数组和String字符串如何转换?
1、string 转 byte[]
String str = "Hello"; byte[] srtbyte = str.getBytes();
2、byte[] 转 string
byte[] srtbyte; String str = new String(srtbyte); System.out.println(str);
相关文章推荐
- java并发
- Java 调用WebService报错
- Javaweb学习路线
- Java 注解
- java实现 阿拉伯数字转换为汉字数字 算法
- Spring源码阅读 --BeanDefinition
- java EE 学习
- Spring 事务管理高级应用难点剖析: 第 3 部分
- beanUtils 使用
- java.lang.System的 System.exit(int code)解析
- 求斐波那契数列前20项
- Java反射
- Face Recognizer
- Spring 事务管理高级应用难点剖析: 第 2 部分
- Java的jdbc使用addBatch进行批处理操作的几种方式
- Eclipse中的快捷键
- Spring 事务管理高级应用难点剖析: 第 1 部分
- Runtime.getRuntime.exec()的用法
- org.w3c.dom(java dom)解析XML文档
- Java JDBC 批处理插入数据操作