黑马程序员0907_API部分+IO输入输出部分

来源:互联网 发布:淘宝如何判定做工瑕疵 编辑:程序博客网 时间:2024/05/19 13:24

---------------------- android培训、java培训、期待与您交流! ----------------------

Vector类是java提供的一种高级数据结构,可以存放一组对象,java无动态数组,而vector提供了类似的功能。

Enumeration接口类提供了访问各种数据结构的所有数据的抽象机制。要调用各种数据结构的所有数据时,都可以采用同样的接口,同样的方法。

java 2平台中Collection提供和vector相似的功能,iterator提供和我enumeration相似的功能,collection不能直接创建对象,而必须用实现了它的类来创建,arraylist就是这样一个类。

vector有线程同步,iterator没有,所以后者效率高。

set和list继承collection,前者元素不能重复,后者元素有顺序,所以可以用collection.sort对list排序。

储存数据:Hashtable.put(object key,object value);检索数据hashtable.get(object key);key和value都可以是任何类型的非空的对象;

自定义的key类一定要覆盖equals和hashcode两个方法,string类的对象默认覆盖好了这两个方法,可以利用。

properties是hashtable的子类,增加了将hashtable对象中的关键字、值对保存到文件和从文件中读取key和value到hashtable对象中的方法;比如一个应用程序的选项设置。程序启动时将key,value读到了内存,程序按新的设置运行。

System类的子类exit类(提前终止虚拟机的运行):出现异常情况,传递一个非零值;正常操作下想终止虚拟机的运行,传递零值作为参数;

CurrentTimesMillis返回自1970年起至今的以毫秒为单位的时间,一个long类型的大数,可用它来计算程序运行时间

getProperties方法获得当前虚拟机的环境变量,类似于windows的环境变量;它的返回值是包含了当前jvm的所有环境属性的properties类型的对象。
实例(打印出当前虚拟机的所有环境属性的变量和值)
import java.util.*;
class Systeminfo
{
 public static void main(String[] args)
 {
  Properties sp=System.getProperties();
  Enumeration e=sp.propertyNames();
  while(e.hasMoreElements())
  {
   String key=(String)e.nextElement();
   System.out.println(key+" = "+sp.getProperty(key));
  }
 }
}

Runtime类封装了java命令本身的运行进程,我们不能直接创建runtime实例,但但可以通过静态方法Runtime.getRuntime获得正在运行的对象的引用。
exec方法,java命令运行后,本身是多任务操作系统上的一个进程,在这个进程中启动一个新的进程,即执行其他程序时使用exec方法,它返回一个代表子进程的process对象,通过这个对象,java进程可以与子进程交互;
实例(记事本打开pro.java源文件,5秒后关闭)
class testruntime
{
 public static void main(String[] args)
 {
  Process p=null;
  try
  {
   p=Runtime.getRuntime().exec("notepad.exe pro.java");
   Thread.sleep(5000);
  }
  catch(Exception e)
  {
   System.out.println(e.getMessage());
  }
  p.destroy();
 }
}

date类,calendar类,dateformat类,math类,random类

【io/输入输出】
【file类】
file类是io包中唯一代表磁盘文件本身的对象,定义了一些与平台无关的方法来操纵文件。

java中,目录也被当作file使用,只是多了一些目录特有的功能,比如用list方法列出目录中的文件名

import java.io.*;
class testfile
{
 public static void main(String[] args)
 {
  File f=new File("c:\\1.txt");
  if(f.exists())
   f.delete();
  else
   try
   {
    f.createNewFile();
   }
   catch(Exception e)
   {
    System.out.println(e.getMessage());
   }
   System.out.println("file name:"+f.getName());
   System.out.println("file path:"+f.getPath());
   System.out.println("abs path:"+f.getAbsolutePath());
   System.out.println("parent:"+f.getParent());
   System.out.println(f.exists()?"exist":"not exist");
   System.out.println(f.canWrite()?"is writeable":"not writeable");
   System.out.println(f.canRead()?"is readable":"not readable");
   System.out.println(f.isDirectory()?"is dir":"not dir");
   System.out.println(f.isFile()?"is normal":"named pipe");
   System.out.println(f.isAbsolute()?"is abs":"not abs");
   System.out.println("file last modified:"+f.lastModified());
   System.out.println("file size:"+f.length()+"Bytes");
 }
}
输出结果:
file name:1.txt
file path:c:\1.txt
abs path:c:\1.txt
parent:c:\
not exist
not writeable
not readable
not dir
named pipe
is abs
file last modified:0
file size:0Bytes

File类不能访问文件的内容,即不能够从文件中读取数据或往文件里写数据,它只能对文件本身的属性进行操作


---------------------- android培训、java培训、期待与您交流! ----------------------

原创粉丝点击