java常用类解析一

来源:互联网 发布:贵州广电网络通讯录 编辑:程序博客网 时间:2024/06/04 18:17

System类、Object类、Arrays类、Cloneable接口

[java] view plaincopy
  1. <span style="font-size:16px;">package test;  
  2. public class SystemDemo {  
  3.     public static void main(String[] args) {  
  4.         String[] s = new String[] { "liu" };  
  5.         String[] s2 = new String[] { "hai" };  
  6.         String[][] a = { s, s2, s, s, s, s2, s2, s };  
  7.         String[][] b = new String[10][10];  
  8.         /* 
  9.          * System类包含一些有用的类字段和方法,是final类,无法被继承,构造方法是private,不能创建System对象。 
  10.          * 所有public属性和方法都是final static 
  11.          */  
  12.         // 1.数组复制,采用本地方法复制,实现了深拷贝  
  13.         System.arraycopy(a, 1, b, 05);  
  14.         System.out.println(b[0][0]);  
  15.         // 2.已经过去的毫米数,从1970-1-1开始  
  16.         System.currentTimeMillis();  
  17.         // 3.提示虚拟机进行垃圾回收,通过调用Runtime.getRuntime().gc();实现  
  18.         System.gc();  
  19.         // 4.返回系统环境  
  20.         System.getenv();  
  21.         // 5.返回当前的系统属性。  
  22.         System.getProperties();  
  23.         // 6.可用於計數已過去的時間  
  24.         System.nanoTime();  
  25.         // 7.0表示正常退出,調用Runtime.getRuntime().exit(0);  
  26.         System.exit(0);  
  27.         // 8.返回给定对象的哈希码,该代码与默认的方法 hashCode() 返回的代码一样,无论给定对象的类是否重写 hashCode()。  
  28.         System.identityHashCode(null);  
  29.     }  
  30. }  
  31. </span>  

[java] view plaincopy
  1. <span style="font-size:16px;">package test;  
  2.   
  3. public class ObjectDemo {  
  4.     /* 
  5.      * 1.Object没有public 的静态属性和方法  
  6.      * 2.public final native Class<?> getClass()返回运行时类信息, 
  7.      * 3.toString 返回类名+@+哈希码值 
  8.      * 4.其中wait和notify方法是final的,不可继承 
  9.      * 5.equals方法只比较对象的引用,hashCode方法返回哈希码值。 
  10.      * 6.重写equals方法要重写hashCode,因为相同的对象(通过equals比较返回true) 
  11.      *   必须返回相同的哈希码。 
  12.      * 7.finalize方法是一个protected空方法 
  13.      * 8.protected native Object clone()返回一个副本,对象必须实现Cloneable接口 
  14.      */  
  15. }  
  16. </span>  

[java] view plaincopy
  1. <span style="font-size:16px;">package test;  
  2.   
  3. import java.util.Arrays;  
  4.   
  5. public class ArraysDemo {  
  6.     /* 
  7.      * 1.数组类提供了排序功能,对基本数据类型length<7采用直接插入排序,否则采用快速排序 如果数组元素时对象,采用合并排序 
  8.      * 2.提供二分查找法实现,注意二分查找时先对数组进行排序,否则返回一个不确定值 
  9.      */  
  10.     public static void main(String[] args) {  
  11.         int[][] a = { { 12 } };  
  12.         int[][] b = { { 12 } };  
  13.         System.out.println(Arrays.toString(a));  
  14.         // 3. 多维数组的toString  
  15.         System.out.println(Arrays.deepToString(a));  
  16.         System.out.println(Arrays.hashCode(a));  
  17.         System.out.println(Arrays.deepHashCode(a));  
  18.         System.out.println(Arrays.equals(a, b));  
  19.         // 4. 多维数组的比较  
  20.         System.out.println(Arrays.deepEquals(a, b));  
  21.   
  22.         // 5. 并没有实现多维数组的复制  
  23.         int[][] c = Arrays.copyOf(a, 1);  
  24.         // 6.填充数组  
  25.         Arrays.fill(a[0], 5);// 在此改变a的值影响到了数组c的值  
  26.         System.out.println(Arrays.deepToString(c));  
  27.         System.out.println(Arrays.equals(a, c));  
  28.         System.out.println(Arrays.deepEquals(a, c));  
  29.   
  30.     }  
  31. }  
  32. </span>  

[java] view plaincopy
  1. <span style="font-size:16px;">package test;  
  2.   
  3. public class DeepCloneDemo {  
  4.     public static void main(String[] args) {  
  5.         B b = new B(2new A(1));  
  6.         B b1 = (B) b.clone();  
  7.         System.out.println(b == b1);  
  8.         System.out.println(b.equals(b1));  
  9.         System.out.println(b.getClass() == b.getClass());  
  10.         System.out.println("改变b的副本b1前:y=" + b.getY() + ",x=" + b.getA().getX());  
  11.         b1.setY(5);  
  12.         b1.getA().setX(100);  
  13.         System.out.println("改变b的副本b1后:y=" + b.getY() + ",x=" + b.getA().getX());  
  14.         System.out.println("深克隆成功!!!");  
  15.     }  
  16. }  
  17.   
  18. class A implements Cloneable {  
  19.     private int x;  
  20.   
  21.     // 为了实现深克隆  
  22.     public Object clone() {  
  23.         A a = null;  
  24.         try {  
  25.             a = (A) super.clone();  
  26.         } catch (CloneNotSupportedException e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.         return a;  
  30.     }  
  31.   
  32.     public A(int x) {  
  33.         this.x = x;  
  34.     }  
  35.   
  36.     public int getX() {  
  37.         return x;  
  38.     }  
  39.   
  40.     public void setX(int x) {  
  41.         this.x = x;  
  42.     }  
  43. }  
  44.   
  45. class B implements Cloneable {  
  46.     private int y;  
  47.     private A a;  
  48.   
  49.     // 覆盖Object中clone方法  
  50.     // protected native Object clone() throws CloneNotSupportedException;  
  51.     // 注意到protected,这里把权限改为了public  
  52.     public Object clone() {  
  53.         B b = null;  
  54.         try {  
  55.             b = (B) super.clone();  
  56.             // 实现深克隆,没有这条语句只是克隆了a的引用  
  57.             b.a = (A) a.clone();  
  58.         } catch (CloneNotSupportedException e) {  
  59.             e.printStackTrace();  
  60.         }  
  61.         return b;  
  62.     }  
  63.   
  64.     public B(int y, A a) {  
  65.         this.y = y;  
  66.         this.a = a;  
  67.     }  
  68.   
  69.     public int getY() {  
  70.         return y;  
  71.     }  
  72.   
  73.     public A getA() {  
  74.         return a;  
  75.     }  
  76.   
  77.     public void setY(int y) {  
  78.         this.y = y;  
  79.     }  
  80.   
  81.     public void setA(A a) {  
  82.         this.a = a;  
  83.     }  
  84. }  
  85. </span>  
IO系统输入输出类

InputStream的作用是用来表示那些从不同数据源产生输入的类。OutputStream决定了输出所要去往的目标

         数据源                                                                 对应的类(都继承自InputStream)

(1)字节数组                                                             ByteArrayInputStream  [ByteArrayOutputStream]

(2)String对象                                                           StringBufferInputStream(已弃用)

(3)文件                                                                    FileInputStream  [FileOutputStream]

(4)“管道”                                                                  PipedInputStream  [PipedOutputStream]

(5)由其它种类的流组成的序列                               SequenceInputStream

(6)其他数据源,如Internet

 

 

示例:

[java] view plaincopy
  1. <span style="font-size:16px;">package test;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.util.Arrays;  
  6. /* 
  7.  * ByteArrayInputStream(ByteArrayOutputStream)表示从字节数组产生输入(输出) 
  8.  * 这个类其实就是对一个字节数组进行操作,把这个字节数组看成一个缓冲区 
  9.  * 关闭方法是一个空方法,关闭后不影响其他方法 
  10.  * 可以将数组定位到指定位置开始读/写,可以将数组从头开始读/写,可以查看数组还有几个字节可用 
  11.  * 可以在某个位置做标记,可以返回到标记位置进行读/写 
  12.  */  
  13. public class ByteArrayInputStreamDemo {  
  14.     public static void main(String[] args) {  
  15.         // 输入流缓冲区(假设已经有若干字节)  
  16.         byte[] inputBuff = new byte[] { 123'a''b''c''d''e''f' };  
  17.         byte[] result = new byte[20];  
  18.         ByteArrayInputStream inputStream = new ByteArrayInputStream(inputBuff);  
  19.         // 将缓冲区的字节读入result数组并输出result  
  20.         inputStream.read(result, 020);  
  21.         System.out.println(Arrays.toString(result));  
  22.         // 将result数组写入输出流  
  23.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  24.         outStream.write(result, 020);  
  25.         System.out.println(Arrays.toString(outStream.toByteArray()));  
  26.     }  
  27.   
  28. }  
  29. </span>  


 

[java] view plaincopy
  1. <span style="font-size:16px;">package test;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8.   
  9. /* 
  10.  * FileInputStream从文件中产生输入流,FileOutputStream 
  11.  * 把输出流输出到文件。读/写、打开和关闭都是调用本地方法 
  12.  */  
  13. public class FileInputStreamDemo {  
  14.     public static void main(String[] args) throws IOException {  
  15.         FileInputStream inputStream = null;  
  16.         try {  
  17.             inputStream = new FileInputStream(new File("file/bb.dat"));  
  18.         } catch (FileNotFoundException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.         // 读到一个字节数组  
  22.         byte[] result = new byte[500];  
  23.         // int b;  
  24.         // while ((b = inputStream.read()) != -1)//读一个字节  
  25.         // System.out.print((char) b);  
  26.         inputStream.read(result);  
  27.         // System.out.println(Arrays.toString(result));  
  28.         inputStream.close();  
  29.         FileOutputStream outputStream = null;  
  30.         // true表示以追加的形式打开  
  31.         outputStream = new FileOutputStream("file/bb.dat"true);  
  32.         // 写入  
  33.         outputStream.write((int'A');  
  34.         outputStream.write(result);  
  35.         outputStream.close();  
  36.     }  
  37. }  
  38. </span>  
IO系统装饰类

java IO系统采用装饰器模式,用一些装饰类来装饰输入输出来,提供更强大的IO操作

FilterInputStream(FilterOutputStream)继承自InputStream(Outputstream)

常用装饰类(都继承自FilterInputStream)               功能

DataInputStream(DataOutputStream)                 读写基本类型即UTF

BufferedInputStream(BufferedOutputStream)      使用缓冲区

PrintStream继承自Outputstream,用于格式化输出到文本或控制台等

 

示例:

[java] view plaincopy
  1. <span style="font-size:16px;">package test;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.DataInputStream;  
  6. import java.io.DataOutputStream;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileNotFoundException;  
  9. import java.io.FileOutputStream;  
  10. import java.io.IOException;  
  11.   
  12. /* 
  13.  * DataInputStream继承自FilterInputStream(FilterInputStream继承自InputStream) 
  14.  * 用来装饰InputStream,提供可移植方式从流读取基本数据类型 
  15.  * DataOutputStream继承自FilterOutputStream(FilterOutputStream继承自OutputStream) 
  16.  * 用来装饰OutputStream,提供可移植方式向流写入基本数据类型 
  17.  * DataInputStream/DataOutputStream可以实现数据的存储与恢复 
  18.  */  
  19. public class DataInputStreamDemo {  
  20.     public static void main(String[] args) {  
  21.         DataOutputStream dataOutStream = null;  
  22.         try {  
  23.             dataOutStream = new DataOutputStream(new BufferedOutputStream(  
  24.                     new FileOutputStream("file/aa.data")));  
  25.         } catch (FileNotFoundException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.         }  
  29.         try {// 写入文件  
  30.             dataOutStream.writeChar('a');  
  31.             dataOutStream.writeInt(3);  
  32.             dataOutStream.writeDouble(5.5);  
  33.             dataOutStream.writeFloat(3.2f);  
  34.             dataOutStream.writeUTF("nihaoma");  
  35.             dataOutStream.close();  
  36.         } catch (IOException e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.           
  40.         DataInputStream dataInputStream = null;  
  41.         try {  
  42.             dataInputStream = new DataInputStream(new BufferedInputStream(  
  43.                     new FileInputStream("file/aa.data")));  
  44.         } catch (FileNotFoundException e) {  
  45.             e.printStackTrace();  
  46.         }  
  47.         try {// 读取文件  
  48.             System.out.println(dataInputStream.readChar());  
  49.             System.out.println(dataInputStream.readInt());  
  50.             System.out.println(dataInputStream.readDouble());  
  51.             System.out.println(dataInputStream.readFloat());  
  52.             System.out.println(dataInputStream.readUTF());  
  53.             dataInputStream.close();  
  54.         } catch (IOException e) {  
  55.             e.printStackTrace();  
  56.         }  
  57.     }  
  58. }  
  59. </span>  


 

[java] view plaincopy
  1. <span style="font-size:16px;">package test;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import java.io.PrintStream;  
  5.   
  6. /* 
  7.  * 继承自FilterOutputStream,其中DataOutputStream处理数据的存储,PrintStream处理显示 
  8.  * 用于格式化打印 
  9.  */  
  10. public class PrintStreamDemo {  
  11.     public static void main(String[] args) throws FileNotFoundException {  
  12.         // 把数据可视化格式显示到文本文件中  
  13.         PrintStream printStream = new PrintStream("file/test2.txt");  
  14.         printStream.println('a');  
  15.         printStream.println(2);  
  16.         printStream.println(3.2);  
  17.         printStream.println("liuhaifang");  
  18.         printStream.println("刘海房");  
  19.         // 可视化显示到控制台  
  20.         printStream = new PrintStream(System.out);  
  21.         printStream.println("hello  java");  
  22.     }  
  23. }  
  24. </span>  
I/O流典型使用方式
[java] view plaincopy
  1. package http;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.FileReader;  
  6. import java.io.FileWriter;  
  7. import java.io.IOException;  
  8.   
  9. /* 
  10.  * Read和Write分别对应InputStream和OutputStream 
  11.  * 前者用于字符流,后者用于字节流 
  12.  * FileReader和FileWrite分别对应与FileInputStream和FileOutputStream 
  13.  * BufferedReader和BufferedWrite分别对应与BufferedInputStream和 
  14.  * BufferedOutputStream 
  15.  * 此示例实现文本文件的字符读写 
  16.  */  
  17. public class FileReaderAndBufferedReaderDemo {  
  18.     public static String read(String fileName) throws IOException {  
  19.         StringBuilder str = new StringBuilder();  
  20.         BufferedReader in = new BufferedReader(new FileReader(fileName));  
  21.         String s;  
  22.         while ((s = in.readLine()) != null)  
  23.             str.append(s + '\n');  
  24.         in.close();  
  25.         return str.toString();  
  26.     }  
  27.   
  28.     public static void write(String fileName, boolean append)  
  29.             throws IOException {  
  30.         BufferedWriter out = new BufferedWriter(  
  31.                 new FileWriter(fileName, append));  
  32.         out.write("我是dahai!java hello!");  
  33.         out.close();  
  34.     }  
  35.   
  36.     public static void main(String[] args) {  
  37.         try {  
  38.             write("file/test3.txt"false);  
  39.             System.out.println(read("file/test3.txt"));  
  40.         } catch (IOException e) {  
  41.             // TODO Auto-generated catch block  
  42.             e.printStackTrace();  
  43.         }  
  44.     }  
  45. }  


 

[java] view plaincopy
  1. package http;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.DataInputStream;  
  5. import java.io.IOException;  
  6.   
  7.   
  8. /* 
  9.  * DataInputStream用于读取格式化的数据 
  10.  */  
  11. public class DataInputStreamAndByteArrayInputStreamDemo {  
  12.     public static void main(String[] args) throws IOException {  
  13.         DataInputStream in = new DataInputStream(new ByteArrayInputStream(  
  14.                 FileReaderAndBufferedReaderDemo.read("file/test3.txt")  
  15.                         .getBytes()));  
  16.         while (in.available() != 0)  
  17.             System.out.print((char) in.readByte());  
  18.   
  19.     }  
  20. }  


 

[java] view plaincopy
  1. package test;  
  2.   
  3. import http.FileReaderAndBufferedReaderDemo;  
  4.   
  5. import java.io.IOException;  
  6. import java.io.StringReader;  
  7. /* 
  8.  * StringReader操作的是字符串 
  9.  */  
  10. public class StringReaderDemo {  
  11.     public static void main(String[] args) throws IOException {  
  12.         StringReader in = new StringReader(FileReaderAndBufferedReaderDemo  
  13.                 .read("file/test3.txt"));  
  14.         int c;  
  15.         while ((c = in.read()) != -1)  
  16.             System.out.print((char) c);  
  17.     }  
  18. }  


 

[java] view plaincopy
  1. package test;  
  2.   
  3. import http.FileReaderAndBufferedReaderDemo;  
  4.   
  5. import java.io.IOException;  
  6. import java.io.PrintWriter;  
  7.   
  8. /* 
  9.  * 对应于PrintStream 
  10.  * 用于格式化输出到文件 
  11.  */  
  12. public class PrintWriterDemo {  
  13.     public static void main(String[] args) throws IOException {  
  14.         // 简化的创建方式  
  15.         PrintWriter out = new PrintWriter("file/test4.txt");  
  16.         // 也可以这样创建: out=new Printer(new BufferedWriter(new  
  17.         // FileWriter("file/test4.txt")));  
  18.         // 格式化输出到文本  
  19.         out.println('a');  
  20.         out.println(3);  
  21.         out.println(3.5);  
  22.         out.print("我爱你!i love you");  
  23.         out.close();  
  24.         // 从文本读取刚才的写入  
  25.         System.out.println(FileReaderAndBufferedReaderDemo  
  26.                 .read("file/test4.txt"));  
  27.     }  
  28.   
  29. }  


 

[java] view plaincopy
  1. package test;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import java.io.IOException;  
  5. import java.io.RandomAccessFile;  
  6.   
  7. /* 
  8.  * RandomAccessFile直接继承Object,可以进行随机输入和输出,类似于c语言的文件操作 
  9.  * 要指明以什么方式打开文件,用这个类时要知道文件的排版,该类有读写基本类型和UTF-8字符串 
  10.  * 的各种方法,可以定位到文件的某一位置进行读写 
  11.  */  
  12. public class RandomAccessFileDemo {  
  13.     public static void main(String[] args) throws FileNotFoundException {  
  14.         RandomAccessFile out = new RandomAccessFile("file/test5""rw");  
  15.         try {  
  16.             out.writeInt(1);  
  17.             out.writeDouble(3.3);  
  18.             out.writeChar('a');  
  19.             out.writeUTF("hello,java!");  
  20.             out.close();  
  21.         } catch (IOException e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.         RandomAccessFile in = new RandomAccessFile("file/test5""r");  
  25.         try {  
  26.             in.seek(4);  
  27.             System.out.println(in.readDouble());  
  28.             in.seek(4+8+2);  
  29.             System.out.println(in.readUTF());  
  30.             in.close();  
  31.         } catch (IOException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.     }  
  35. }  


 

[java] view plaincopy
  1. package test;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.ObjectInputStream;  
  8. import java.io.ObjectOutputStream;  
  9. import java.io.Serializable;  
  10.   
  11. /* 
  12.  * ObjectInputStream(ObjectOutputStream)用于对象的序列化 
  13.  * 直接对一个对象进行读写,该对象须实现Serializable 
  14.  */  
  15. public class ObjectInputStreamDemo {  
  16.     public static void writeObject(String fileName, Object o, boolean isAppend)  
  17.             throws FileNotFoundException, IOException {  
  18.         ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(  
  19.                 fileName, true));  
  20.         out.writeObject(o);  
  21.         out.close();  
  22.     }  
  23.   
  24.     public static Object readObject(String fileName)  
  25.             throws FileNotFoundException, IOException, ClassNotFoundException {  
  26.         ObjectInputStream in = new ObjectInputStream(new FileInputStream(  
  27.                 fileName));  
  28.         Object o = in.readObject();  
  29.         in.close();  
  30.         return o;  
  31.     }  
  32.   
  33.     public static void main(String[] args) {  
  34.         try {  
  35.             ObjectInputStreamDemo.writeObject("file/object"new Person(),  
  36.                     false);  
  37.             ((Person) ObjectInputStreamDemo.readObject("file/object"))  
  38.                     .display();  
  39.         } catch (IOException e) {  
  40.             e.printStackTrace();  
  41.         } catch (Exception e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.     }  
  45.   
  46. }  
  47.   
  48. class Person implements Serializable {  
  49.     private String name = "刘海房liuhaifang";  
  50.     private int sex = 0;  
  51.   
  52.     Person(String name, int sex) {  
  53.         this.name = name;  
  54.         this.sex = sex;  
  55.     }  
  56.   
  57.     Person() {  
  58.     }  
  59.   
  60.     void display() {  
  61.         System.out.println("my name is :" + name);  
  62.         String s = (sex == 0) ? "男" : "女";  
  63.         System.out.println(s);  
  64.     }  
  65. }  

原创粉丝点击