Java学习日记13——File*putStream的方法
2014-11-29 23:22
411 查看
FileInputStream继承自InputStream,对应的动作是“读”(站在程序的角度)。
FileOutputStream继承自OutputStream,站在程序的角度,对于的动词为“写”
package com.sxu.cs.io; import java.io.*; public class FileInputStreamMethod { static FileInputStream in = null; public static void main(String[] args) { // TODO Auto-generated method stub try { in = new FileInputStream("G:/input.txt"); // testRead(); // testRead1(); // testRead2(); testSkip(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(in!=null) try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private static void testRead(){ byte b = 0; try { b = (byte)in.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println((char)b); } private static void testRead1(){ byte[] bArr = new byte[1024]; int counter = 0; try { counter = in.read(bArr); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.print ("the file:G:/input.txt has "+counter +" 字节,文件内容为:"); for(int i = 0;i<bArr.length;i++) { System.out.print((char)bArr[i]); } } private static void testRead2(){ byte[] bArr = new byte[1024]; int offset = 5;//偏移量 int length = 5;//长度 try { in.read(bArr,offset,length); for(int i = 0;i<bArr.length;i++) { System.out.print((char)bArr[i]); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void testSkip(){ try { long l = in.skip(5+3); System.out.println("跳过 "+l+" 字节"); testRead1(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
FileOutputStream继承自OutputStream,站在程序的角度,对于的动词为“写”
package com.sxu.cs.io; import java.io.*; public class FileOutputStreanMethod { public static void main(String[] args) { // TODO Auto-generated method stub File file = new File("G:/output.txt"); FileOutputStream out = null; byte[] b1 ={12,34,56,78,90}; String s = "i love java"; byte[] b2 = s.getBytes(); try { out = new FileOutputStream(file); out.write(123); out.write(b1); out.write(b2); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(out != null) try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
相关文章推荐
- 黑马程序员_Java学习日记13_IO流2
- android学习日记13--数据存储之File存储
- java学习日记_22:面向对象之构造方法。07.11—12;
- java学习日记_55:Scanner的概述和构造方法原理
- JAVA基础 day22 File类的方法和应用 利用递归获取目录下的所有文件 Properties类的应用(和IO技术结合,存取配置文件) 打印流和合并流等功能类的学习应用 切割文件练习
- 黑马程序员_Java学习日记15_File
- java学习之File类方法
- java学习日记_24:面向对象之成员方法的分类和运用。07.13
- JMeter学习-027-JMeter参数文件(脚本分发)路径问题:jmeter.threads.JMeterThread: Test failed! java.lang.IllegalArgumentException: File distributed.csv must exist and be readable解决方法
- java学习日记_54:Object类的finalize()方法和clone()方法
- [bxd学习java基本点]13泛型类,泛型方法,当定义二种类型的泛型方法时,用迭代器的方法
- Java学习日记之方法
- 黑马程序员--内省和JavaBean--java学习日记13(高新技术)
- java学习日记_27:面向对象之main方法格式详细解释
- 18天Java学习---13.面向对象编程08-Java中继承的概念,子类实例化过程,super()及方法复写
- [bxd学习java基本点]13泛型类,泛型方法,当定义二种类型的泛型方法时,用迭代器的方法
- Java学习笔记之 File构造方法
- java学习之旅40--面向对象_13_继承_Object类用法_toString和equals方法_重写toString
- java学习日记_51:Object类的hashCode()方法
- java学习日记_37:方法重写的应用和注意事项