码迷,mamicode.com
首页 > 其他好文 > 详细

2015_1_28_IO与常用类

时间:2015-01-28 22:38:28      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

/*日志名格式  年_月_日_内容 如2015_1_28_IO与常用类*/

**************************************************************************************************************************************************************

日期:2015年1月 28日

主题:IO 、常用类

相关文件夹:oracle/相关课件/第12章_Java常用类.pptx

                            oracle/相关课件/第11章_多线程.pptx

使用软件:Eclipse

**************************************************************************************************************************************************************

上午

**************************************************************************************************************************************************************

一、数据流

         数据流可用于对数据输入输出是加密用。

l     为了方便地操作Java语言的基本数据类型的数据,可以使用数据流。

l  数据流有两个类:(用于读取和写出基本数据类型的数据)

DataInputStream DataOutputStream

分别“套接”在 InputStream和OutputStream 节点流上

DataInputStream中的方法

    boolean readBoolean()           bytereadByte()

    char readChar()                        floatreadFloat()

    double readDouble()               shortreadShort()

    long readLong()                        intreadInt()

    String readUTF()                                 voidreadFully(byte[] b)

DataOutputStream中的方法

?  将上述的方法的read改为相应的write即可。

例:

             /**

     * Data Output Stream

     */

    @Test

    public void test() {

        DataOutputStream dos = null;

        try {

            dos = new DataOutputStream(new FileOutputStream(

                    new File("data.txt")));

            dos.writeDouble(12.2);

            dos.writeUTF("asdfasklfj");

            dos.flush();

        }catch(FileNotFoundException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }catch(IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }finally{

            try {

                dos.close();

            }catch(IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

    }

 

    @Test

    public void test1() {

        DataInputStreamdis= null;

        try {

            dis = new DataInputStream(new FileInputStream(new File("data.txt")));

            System.out.println(dis.readDouble());

            System.out.println(dis.readUTF());

        }catch(IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }finally{

            try {

                dis.close();

            }catch(IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

    }

二、 对象流

ObjectInputStream和OjbectOutputSteam

?  用于存储和读取对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。

序列化(Serialize):用ObjectOutputStream类将一个Java对象写入IO流中

反序列化(Deserialize):用ObjectInputStream类从IO流中恢复该Java对象

?  ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量

即注意:需要转换为流的对象需要先实例化Serialize接口将该类序列化,否则无法实现。

强调:如果某个类的字段不是基本数据类型或 String  类型,而是另一个引用类型,那么这个引用类型必须是可序列化的,否则拥有该类型的 Field 的类也不能序列化

三、RandomAccessFile类

         例题:通过RandomAccessFile类实现对文件的插入操作

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.RandomAccessFile;

 

public classInsertTest {

 

    public static void Insert(File f, long a, String val) {

        Filefile= f;

        RandomAccessFileraf= null;

        StringBuffersb = new StringBuffer();

        try {

            raf = new RandomAccessFile(file, "rw");

            byte[] by = new byte[1024];

            raf.seek(a);

            int len = 0;

            while ((len = raf.read(by)) != -1) {

                sb.append(new String(by));

            }

            raf.seek(a);

            raf.write(val.getBytes());

            raf.write(sb.toString().getBytes());

 

        }catch(IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }finally{

            try {

                raf.close();

            }catch(IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

    }

 

    public static void main(String[] args) {

        Filefile= newFile("hello.txt");

        Insert(file, 5, "asdfgg44432---");

    }

}

 

 

 

 

 

**************************************************************************************************************************************************************

下午

**************************************************************************************************************************************************************

一、String类

l     常量对象:字符串常量对象是用双引号括起的字符序列。      

l   例如:"你好"、"12.97"、"boy"等。

l  字符串的字符使用Unicode字符编码,一个字符占两个字节

l  String类较常用构造方法:

?  String  s1 = new String();

?  String  s2 = new String(String original);

?  String  s3 = new String(char[] a);

?  String  s4 = new String(char[] a,int startIndex,int count)

 

 

二、字符串与基本数据的相互转化

l     字符串转换为基本数据类型

?  Integer包装类的public static int parseInt(Strings):可以将由“数字”字符组成的字符串转换为整型。

?  类似地,使用java.lang包中的Byte、Short、Long、Float、Double类调相应的类方法可以将由“数字”字符组成的字符串,转化为相应的基本数据类型。

l  基本数据类型转换为字符串

?  调用String类的public String valueOf(int n)可将int型转换为字符串

?  相应的valueOf(byte b)、valueOf(long l)、valueOf(float f)、valueOf(double d)、valueOf(boolean b)可由参数的相应类到字符串的转换

 

三、StringBuffer类

l     java.lang.StringBuffer代表可变的字符序列,可以对字符串内容进行增删。

l  很多方法与String相同,但StingBuffer是可变长度的。

l  StringBuffer是一个容器。

 

四、StringBuilder类

l     StringBuilder和 StringBuffer非常类似,均代表可变的字符序列,而且方法也一样

?  String:不可变字符序列

?  StringBuffer:可变字符序列、效率低、线程安全

?  StringBuilder(JDK1.5):可变字符序列、效率高、线程不安全

l  String使用陷阱:

?   string s="a"; //创建了一个字符串
 s=s+"b"; //实际上原来的"a"字符串对象已经丢弃了,现在又产生了一个字符串s+"b"(也就是"ab")。如果多次执行这些改变串内容的操作,会导致大量副本字符串对象存留在内存中,降低效率。如果这样的操作放到循环中,会极大影响程序的性能。

 

五、日期类

         1.java.lang.System类

         System类提供的public static longcurrentTimeMillis()用来返回当前时间与1970年1月1     日0时0分0秒之间以毫秒为单位的时间差。

?  此方法适于计算时间差。

l  计算世界时间的主要标准有:

?  UTC(Universal Time Coordinated)

?  GMT(Greenwich Mean Time)

?  CST(Central Standard Time)

 

六、SimpleDateFormat()

         Date类的API不易于国际化,大部分被废弃了,java.text.Simp

         leDateFormat类是一个不与语言环境有关的方式来格式化和解析日期的具体类。

l  它允许进行格式化(日期à文本)、解析(文本à日期)

u  格式化:

?  SimpleDateFormat() :默认的模式和语言环境创建对象

?  public SimpleDateFormat(Stringpattern):该构造方法可以用参数pattern指定的格式创建一个对象,该对象调用:

?  public String format(Date date):方法格式化时间对象date

u  解析:

?  public Date parse(String source):从给定字符串的开始解析文本,以生成一个日期。

 

 

2015_1_28_IO与常用类

标签:

原文地址:http://blog.csdn.net/zydirtyfish/article/details/43242455

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!