zoukankan      html  css  js  c++  java
  • java流和文件 保存字节级数据(写)

    重要的知识点:

    流的概念:  从数据源到I/O类的输入流(in)

            从I/O类数据接收器的输出流(out)

    I/O包含子类较多的有四大家族:InputStream,OutputStream,Reader和Write类

                  InputStream,OutputStream类均为抽象类,也就是说不能用他们创建实例对象,必须子类化之后才能建立对象

    printStream是一个格式化的输出流,它含有如下形式的write()方法

    public void write(int b)

    public void wirte(byte b[], int off, int len);

    除了write()方法外,printStream还有两个主要方法,print()和println(),举例如下:

    System.out.println(“hello java");

    System.out.print("x=",x);

    主要差别是:print()方法是先把字符保存到缓冲区,然后当遇到换行符" "时再显示到屏幕上;而则是直接显示字符

    package com.swust;
    import java.io.*;
    /*
     * 功能:按照双精度浮点型、整型、布尔型、字符型、和字符串型的顺序存储数据到一个名为sample.dat文件
     * 分析:用FileOutputStream类创建一个输出流的实例作为一个“流”
     *       将该实例作为DataOutputStream对象的输入
     *       该对象调用各种方法实现写入“流”的功能
     * 图形:
     *  sample.dat<----fs_out(FileOutputStream)<-------out(DataOutputStream)
     *  FileOutputStream类似一个底层函数,DataOutputStream为顶层函数,使用它作为一个桥梁输出到流
     */
    public class flowTest {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            double pi=3.1415;
            int i=10;
            boolean ok=true;
            char c='w';
            String str="我是二逼";
            try{
                FileOutputStream fs_out=new FileOutputStream("sample.dat");
                DataOutputStream out =new DataOutputStream(fs_out);
                out.writeDouble(pi);
                out.writeInt(i);
                out.writeBoolean(ok);
                out.writeChar(c);
                out.writeUTF(str);
                out.close();
            
            }catch(FileNotFoundException fe){
                System.err.println(fe);
            }catch(IOException ioe){
                System.err.println(ioe);
            }
        }
    
    }
  • 相关阅读:
    合适的适配器的最佳模式
    poj3414--Pots(bfs,记录路径)
    HTML与XHTML差额
    获得球——采访拼图
    3-08. 栈模拟队列(25)(ZJU_PAT 模拟)
    学习内核驱动程序的错误及其解决方案的出现,
    Objective-C中的Block
    Objective-C语法之代码块(block)的使用
    ObjectiveC中的block用法解析
    使用Swift的代理,闭包来封装一个公用协议减少垃圾代码
  • 原文地址:https://www.cnblogs.com/shuqingstudy/p/4728555.html
Copyright © 2011-2022 走看看