IO流--打印流

IO的两个规律:
1,名称定义规律,便于查找具体的对象。
2,IO流的操作规律,四个明确,可以解决使用哪个流对象的问题。


IO体系的组成。

File

InputStream
    |--FileInputStream:
    |--FilterInputStream
        |--BufferedInputStream:
        |--DataInputStream:
    |--ObjectInputStream:
    |--ByteArrayInputStream:
    |--SequenceInputStream:


OutputStream
    |--FileOutputStream:
    |--FilterOutputStream
        |--BufferedOutputStream:
        |--DataOutputStream:
        |--PrintStream:
    |--ObjectOutputStream:
    |--ByteArrayOutputStream:


Reader
    |--InputStreamReader:
        |--FileReader:
    |--BufferedReader:
    |--CharArrayReader:
    |--StringReader:
Writer
    |--InputStreamWriter:
        |--FileWriter:
    |--BufferedWriter:
    |--CharArrayWriter:
    |--StringWriter:
    |--PrintWriter:

重点对象:
File开头的四个。
Buffered开头的四个。
转换流两个。
打印流两个。
File对象。

后面的都是额外功能。
1,IO的基础流对象。直接调用底层资源,操作数据的对象。
File开头的流对象。
2,根据io流的学习规律,后面出现的流对象无非是增加额外功能。
继续提供一些功能。

需求:写一个数据(整数)到文件中。
可以通过将整数转成字符串,变成字节数组写入目的地。

简化方式,之前学习过输出语句发现要输出的内容都原样不变的体现出来。
输出语句对应的对象PrintStream。
对象提供了很多打印的方法,打印方法的好处在于保证数据值的表现形式。

PrintWriter:字符打印流。

-----------
另一个需求:保证数据值字节原样性不变。
例如:写一个整数,源是四个字节,希望是目的也是四个字节。
需要可以操作基本类型数值的对象。
DataOutputStream  DataInputStream
用于操作基本类型数据的流对象。


-----------
源和目的都是内存的流的。
字节流:
ByteArrayInputStream  ByteArrayOutputStream

字符流
CharArrayReader  CharArrayWriter
StringReader  StringWriter
原理其实通过流的read,write方法对数组以及字符串进行操作。
关闭这些流都是无效的。因为并未调用系统资源。不需要抛出IOException。

-----------------
需求:对文件进行读或者写的操作,想从哪里读就从哪里读,想从哪里写就从哪里写。
RandomAccessFile
public class PrintStreamDemo {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        //需求:希望写一个整数,到目的地整数的表现形式不变。可以将整数转成字符串在写入到目的地。

//      FileOutputStream fos = new FileOutputStream("tempfile\\int.txt");
//      fos.write(String.valueOf(97));//字节流的write方法只将一个整数的最低字节写入到目的地;//00000000 00000000 00000001 01100001
//      fos.close();


//      FileOutputStream fos = new FileOutputStream("tempfile\\int.txt");
//      //需要额外功能吗?保证数据值的表示形式。需要。
//      PrintStream ps = new PrintStream(fos);
        ps.write(97);// 只能写入最低字节。
//      ps.print(97);//将数据转成字符串在写入。保证数据值的表现形式。
//      ps.close();


        PrintStream ps = new PrintStream("tempfile\\int.txt");
        ps.print(98);
        ps.close();
    }

}
public class PrintWriterDemo {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        //读取键盘录入。将录入的数据转成大写保存到文件中。

        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

        PrintWriter out = new PrintWriter(System.out,true);//true自动刷新,对println有效。

        String line = null;
        while((line=bufr.readLine())!=null){
            if("over".equals(line)){
                break;
            }
            out.println(line.toUpperCase());
//          out.flush();
        }

        out.close();


        //想要将数据打印到文件中,并使用自动刷新。
        //PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true);
    }

}
public class DataStreamDemo {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {


//      writeData();

        readData();

    }

    public static void readData() throws IOException {

        FileInputStream fis = new FileInputStream("tempfile\\data.txt");

        //读取一个整数,需要额外功能。
        DataInputStream dis = new DataInputStream(fis);

        int num = dis.readInt();
        System.out.println("num="+num);
        dis.close();

    }

    public static void writeData() throws IOException {

        FileOutputStream fos = new FileOutputStream("tempfile\\data.txt");
        //需要额外功能吗?需要,可以写一个基本数值的原字节不变。
        DataOutputStream dos = new DataOutputStream(fos);

        dos.writeInt(97);//00000000 00000000 00000000 01100001 

        dos.close();

    }

}
public class ByteArrayStreamDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //源和目的都内存的读写过程。

        //源:内存。
        ByteArrayInputStream bis = new ByteArrayInputStream("abcde".getBytes());//用流的读写思想操作数组中的数据。

//      byte[] buf = "abcde".getBytes();
//      for(byte b : buf){
//          bos.write(b);
//      }

        //目的:内存。
        ByteArrayOutputStream bos = new ByteArrayOutputStream();//内部有一个可自动增长的数组。

        //不断的读写。
        int ch = 0;
        while((ch=bis.read())!=-1){
            bos.write(ch);
        }
        //因为没有调用底层资源,所以不要关闭,即使调用了close,也没有任何效果,不会抛出IOException.

        System.out.println(bos.toString());

    }

}
public class RandomAccessFileDemo {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        /*
         * RandomAccessFile:
         * 1,随机访问。
         * 2,操作文件。
         * 3,既可以读又可以写,
         * 4,内部维护了用于存储数据的byte数组。
         * 5,提供了一个对数组操作的文件指针。
         * 6, 文件指针可以通过getFilePointer 方法读取,并通过 seek 方法设置。 
         * 
         * 注意:随机读写,数据需要规律,用RandomAccessFile类需要明确要操作的数据的位置。
         */

//      writeFile();

        readFile();

    }

    public static void readFile() throws IOException {

        RandomAccessFile raf = new RandomAccessFile("tempfile\\random.txt", "r");
        //改变指针的位置,想读谁就读谁。
        raf.seek(8*3);


        byte[] buf = new byte[4];
        raf.read(buf);
        String name = new String(buf);
        System.out.println("name="+name);

        int age = raf.readInt();
        System.out.println("age="+age);

        raf.close();

    }

    public static void writeFile() throws IOException {



        RandomAccessFile raf = new RandomAccessFile("tempfile\\random.txt", "rw");

        //写一些字符信息,姓名 +年龄。
//      raf.write("张三".getBytes());
//      raf.writeInt(97);//保证字节的原样性。
//      raf.write("李四".getBytes());
//      raf.writeInt(99);//保证字节的原样性。

        raf.seek(4);

        raf.write("王武".getBytes());
        raf.writeInt(102);

        System.out.println(raf.getFilePointer());

        raf.close();

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值