zoukankan      html  css  js  c++  java
  • IO学习二

    l  管道流  线程的通信 PipedInputStream  PipedoutputStream

    l  System.out.println

    l  System.in输入 使用事为inputstream实例化后存在两个问题

    输入的长度受限制,

    如果输入中文超过了容量则可能出现乱码

    实际上在输入数据的时候更希望将全部数据一次性读取近来,如果一次性读取肯定不会出现乱码问题

    要想实现上述的功能,则需要将输入数据放入到一个缓存之中

    BufferedReader  BufferedReader(Reader in) 此类在实例化时需要

    l  PrintStream

     

     

    字符集 文件保存使用的编码

     

    SequenceInputStream合并两个文件的SequenceInputStream(InputStream s1, InputStream s2)

                     序列化问题:

     

     

                                对象序列化就是把对象写入到流(0101的比特流)中,对象的反序列化是指从流中恢复对象。

    对象序列化的优点在于即使程序运行结束,对象仍然可以被保存下来

    对象所在的类必须实现java.io.serializable接口,才能被序列化

    一个类的对象如果想被实例化 必须实现serializable接口

    class Person implements Serializable

    {

        private String name ;

        private transient int age ;

        public Person(String name,int age)

        {

           this.name = name ;

           this.age = age ;

        }

        public String toString()

        {

           return "姓名:"+this.name+",年龄:"+this.age ;

        }

    };

    public class IODemo26

    {

        public static void main(String args[]) throws Exception

        {

           Person per = new Person("李兴华",30) ;

           ser(per) ;

           System.out.println(dser()) ;

        }

        // 建立一个方法用于完成对象的序列化

        public static void ser(Person per) throws Exception

        {

           ObjectOutputStream oos = null ;

           oos = new ObjectOutputStream(new FileOutputStream(new File("f:\\lxh.txt"))) ;

           oos.writeObject(per) ;

           oos.close() ;

        }

        // 反序列化

        public static Person dser() throws Exception

        {

           ObjectInputStream ois = null ;

           ois = new ObjectInputStream(new FileInputStream(new File("f:\\lxh.txt"))) ;

           Object obj = ois.readObject() ;

           ois.close() ;

           return (Person)obj ;

        }

    };

    如果需要将对象变为byte 则需要将对象进行转化 ObjectOutputStream实现

    ObjectOutputStream(OutputStream out)
              
    创建写入指定 OutputStream ObjectOutputStream

    此种方法类似与printwriter/printStream 谁为其实例化就向哪里输出

    class Person implements Serializable

    {

        private String name ;

        private transient int age ;

        public Person(String name,int age)

        {

           this.name = name ;

           this.age = age ;

        }

        public String toString()

        {

           return "姓名:"+this.name+",年龄:"+this.age ;

        }

    };

    public class IODemo26

    {

        public static void main(String args[]) throws Exception

        {

           Person per = new Person("李兴华",30) ;

           ser(per) ;

           System.out.println(dser()) ;

        }

        // 建立一个方法用于完成对象的序列化

        public static void ser(Person per) throws Exception

        {

           ObjectOutputStream oos = null ;

            oos = new ObjectOutputStream(new FileOutputStream(new File("f:\\lxh.txt"))) ;

           oos.writeObject(per) ;

           oos.close() ;

        }

        // 反序列化

        public static Person dser() throws Exception

        {

           ObjectInputStream ois = null ;

           ois = new ObjectInputStream(new FileInputStream(new File("f:\\lxh.txt"))) ;

           Object obj = ois.readObject() ;

           ois.close() ;

           return (Person)obj ;

        }

    };

    这是保存的对象  但是乱码看不懂

     

    反序列化 objectInputStream

     

    一个序列化的核心部分是Serializable接口,其中没有任何方法

    此接口只是一个声明性接口 表示实现此接口的类可以被序列化。

    问题:

    现在程序中姓名和年龄都序列化了 我要是不想把年龄序列化时 transient来标示

     

    变量声明的完整定义

    Public protected private final static transient 数据类型 变量名称 =初始化内容

     

    完整的方法定义

    Public protected private final static synchronized 方法的返回类型 void 方法名称(参数列表)

     

     

     

    常用的API 应用程序接口 指的是系统提供的大量的类及方法

     

    StringBuffer

    回顾String类的对象一旦声明则不可改变

    String对象不适合以下的情况

    public class APIDemo01

    {

        public static void main(String args[])

        {

           String str = "" ;

           for(int i=0;i<20;i++)

           {

               str += i ;

           }

           System.out.println(str) ;

        }

    };

    此时我们可以用StringBuffer,其也是一个字符串对象,但是此对象中的内容可以改变,只是代码的形式少有不同 StringBuffer必须先实例化 

    StringBuffer可以改变但是String 不可改变

    StringBuffer必须先实例化

    public class APIDemo02

    {

        public static void main(String args[])

        {

           // String str = "" ;

           // +表示字符串连接

           // str = "A" + "B" + "c" ;

           StringBuffer sb = new StringBuffer() ;

           sb.append("A").append("B") ;

           sb.append("C").append(1) ;

           fun(sb) ;

           System.out.println(sb) ;

        }

        // StringBuffer传递的是引用

        public static void fun(StringBuffer s)

        {

           s.append("LXH --> MLDN") ;

        }

    };

     

     

     

    包装类:

     IO操作时字符串变为整型 Integer

     java中提倡一种思想:一切极为对象,java中有八中数据类型 基本类型

    用法 常见在与转型上: 字符串响应的数据类型上

    public class APIDemo03

    {

        public static void main(String args[])

        {

           String str = "123" ;

           // int i = Integer.parseInt(str) ;

           // System.out.println(i * 2) ;

           float f = Float.parseFloat(str) ;

           System.out.println(f * 2) ;

        }

    };

     

    Runtime类:

           运行时类使用此类可以调用本机上的程序

           每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。可以通过 getRuntime 方法获取当前运行时。

    应用程序不能创建自己的 Runtime 类实例。

    即外部无法实例化

    因为在类中将构造方法私有化了

    public class APIDemo04

    {

        public static void main(String args[]) throws Exception

        {

           Runtime r = Runtime.getRuntime() ;

           // 运行程序

           Process p = r.exec("notepad.exe") ;

           Thread.sleep(3000) ;

           // 让进程销毁 --> 关闭

           p.destroy() ;

        }

    };

    调用了本机的程序

    要求:能不能让记事本打开之后三秒后自动关闭

    运行的程序是一个进程,可以通过进程来操作

     

    Random
          
    表示一个随机数类 例如 要产生一个不大于10010个随即整数

    java.util包下

    public class APIDemo05

    {

        public static void main(String args[]) throws Exception

        {

           Random r = new Random() ;

           for(int i=0;i<10;i++)

           {

               System.out.println(r.nextInt(100)) ;

           }

        }

    };

     

    取的时间的类

           Date Calendar(可以将时间精确到秒)类:

    public class APIDemo06

    {

        public static void main(String args[]) throws Exception

        {

           System.out.println(new Date()) ;

        }

    };

    但是date不符合中国人的习惯

    Calendar是个抽象类 要想实例化此类 则要需要子类

    public class APIDemo07

    {

        public static void main(String args[]) throws Exception

        {

           Calendar calendar = new GregorianCalendar() ;

           System.out.println("YEAR: " + calendar.get(Calendar.YEAR));

           System.out.println("MONTH: " + (calendar.get(Calendar.MONTH)+1));

           System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));

           System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));

           System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));

           System.out.println("SECOND: " + calendar.get(Calendar.SECOND));

           System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));

        }

        // 2007517 xx:xx:xx:xxx

    };

     

    有时候可以对格式进行转换

           我们可以用 从原格式中丢弃格式 取出具体的时间数字

                      准备新格式

                   将原格式中的时间数子放入新格式中

     SimpleDateForm

    java.text

    public class APIDemo08

    {

        public static void main(String args[]) throws Exception

        {

           /*

               2007-5-17 16:19:20

               -- ::

     

           */

           String str = "2007-5-17 16:19:20" ;

           // 1、准备原格式

           SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;

     

           // 2、准备新格式

           SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd HHmmss") ;

     

           // 3、按sdf1模板摘出时间数

           /*

           public Date parse(String source)

               throws ParseException

           */

           Date d = sdf1.parse(str) ;

           // 4、将时间数插入到新模板之中

           // public final String format(Date date)

           String newStr = sdf2.format(d) ;

           System.out.println(newStr) ;

        }

    };

  • 相关阅读:
    【转载】比较c++中的值传递,引用传递,指针传递
    【转载】在ARX中通过COM在ACAD中添加菜单和工具条
    【转载】预编译头文件phc
    jsp 连 sql server
    今天上传点关于asp的好东东
    转: [软件人生]给一个刚毕业学生朋友的建议
    世界首富比尔盖茨花钱全过程
    wap开发工具
    一名25岁的董事长给大学生的18条忠告
    今天再来点好东东,
  • 原文地址:https://www.cnblogs.com/zhxiaomiao/p/1412245.html
Copyright © 2011-2022 走看看