A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 清风木扬 中级黑马   /  2014-3-27 11:27  /  1969 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

一,工具类
   
   1.Math
      abs() 绝对值  
      ceil()  大于它的最小整数  ceil(1.5) //2.0
      floor() 小于它的最大整数  floor(1.5) //1.0
      round() 四舍五入          round(1.5) //1
      pow(,)  幂运算            pow(10,2)//100
      random() 0到1的随机数
  2.Runtime(运行可执行文件)
      该类没有提供构造函数,Runtime.getRuntime()得到对象。
      Runtime r = Runtime.getRuntime();
      Process p = r.exec("notepad.exe  SystemDemo.java");
  3.System
    setOut()设定输出流。
    Properties getProperties()获取系统属性信息
    //Properties 是HashTable的子类,
        Properties prop = System.getProperties();         
        for(Object obj : prop.keySet())
        {
            String value = (String)prop.get(obj);

            System.out.println(obj+"::"+value);
        }

   
    System.setProperty(key, value)
    System.getProperty(key);
  4.Calendar
   Calendar c=Calendar.getInstance();//指定时区和语言环境获得一个日历
   System.out.println(c.get(c.HOUR_OF_DAY));// 返回给定日历字段的值

    Calendar c=Calendar.getInstance();
      
      c.set(c.YEAR, 2012);
      c.set(c.MONTH,0);
      c.set(c.DAY_OF_MONTH,1);
      c.add(c.DAY_OF_YEAR,-1);
      
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy MM dd");
      System.out.println(sdf.format(c.getTime()));
      System.out.println(c.get(c.DAY_OF_MONTH));   
二。IO
   1.IO主要类
    字符流
    Reader
           FileReader   
           BufferedReader
           InputStreamReader(将字节流转成字符流)
    Writer  
           FileWriter
           BufferedReader   
           OutputStreamWriter(将字节流转成字符流)
    字节流
    InputStream   read()     read(byte[] )
           FileInputStream  
           BufferedInputStream
    OutputStream
           FileOutputStream   available() 文件可读剩余字节数
           BufferedOutputStream

   2. Reader类中  read()与read(char[] cbuf)的区别
    int read() 读取单个字符,当返回-1时就读完了
    int read(char[] cbuf) 读取的值放在数组中,返回的是读取的字符个数,当返回-1时就读完了
  
   3.Writer类中 flush()与close()
     flush()刷新流对象中的缓冲中的数据,将数据刷到目的地中。
     close()关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据,将数据刷到目的地中。
     和flush区别:flush刷新后,流可以继续使用,close刷新后,会将流关闭。
   
   4.new FileWriter("demo.txt");/该文件会被创建到指定目录下。如果该目录下已有同名文件,将被覆盖。

   5.流操作的基本规律
      明确源和目的。
    源:输入流。InputStream  Reader
    目的:输出流。OutputStream  Writer。
      操作的数据是否是纯文本。
    是:字符流。
    不是:字节流。

       当体系明确后,在明确要使用哪个具体的对象。
    通过设备来进行区分:
    源设备:内存,硬盘。键盘
    目的设备:内存,硬盘,控制台。
   6.模拟Buffere与Readln
    class MyBufferedInputStream
    {
    private InputStream in;

    private byte[] buf = new byte[1024*4];
        
    private int pos = 0,count = 0;
   
    MyBufferedInputStream(InputStream in)
    {
        this.in = in;
    }

    //一次读一个字节,从缓冲区(字节数组)获取。
    public int myRead()throws IOException
    {
        //通过in对象读取硬盘上数据,并存储buf中。
        if(count==0)
        {
            count = in.read(buf);
            if(count<0)
                return -1;
            pos = 0;
            byte b = buf[pos];

            count--;
            pos++;
            return b&255;
        }
        else if(count>0)
        {
            byte b = buf[pos];

            count--;
            pos++;
            return b&0xff;
        }
        return -1;

    }
    public void myClose()throws IOException
    {
        in.close();
    }
      }
  
class  ReadIn
  {
    public static void main(String[] args) throws IOException
    {
        InputStream in = System.in;
        StringBuilder sb = new StringBuilder();

        while(true)
        {
            int ch = in.read();
            if(ch=='\r')
                continue;
            if(ch=='\n')
            {
                String s = sb.toString();
                if("over".equals(s))
                    break;
                System.out.println(s.toUpperCase());
                sb.delete(0,sb.length());
            }
            else
                sb.append((char)ch);

        }
    }
   }
    7.装饰设计(吃饭)
      装饰类因为增强已有对象,具备的功能和已有的是相同的,只不过提供了更强功能。
      所以装饰类和被装饰类通常是都属于一个体系中的。

三.基它IO类

   File
    创建。
    boolean createNewFile():在指定位置创建文件,如果该文件已经存在,则不创建,返回false。
                        和输出流不一样,输出流对象一建立创建文件。
                                                而且文件已经存在,会覆盖。

    boolean mkdir():创建文件夹。
    boolean mkdirs():创建多级文件夹。
    删除。
    boolean delete():删除失败返回false。如果文件正在被使用,则删除不了返回falsel。
    void deleteOnExit();在程序退出时删除指定文件。


    判断。
    boolean exists() :文件是否存在.
    isFile():
    isDirectory();
    isHidden();
    isAbsolute();

    获取信息。
    getName():
    getPath():
    getParent():

    getAbsolutePath()
    long lastModified()
    long length()
   
      /*
       File dir = new File("c:\\");
    File[] files = dir.listFiles();

    for(File f : files)
    {
        System.out.println(f.getName()+"::"+f.length());
    }
      */
      /*
      File dir = new File("d:\\java1223\\day18");

        String[] arr = dir.list(new FilenameFilter() //文件过滤器
        {
            public boolean accept(File dir,String name)
            {

                              //System.out.println("dir:"+dir+"....name::"+name);
                /*
                if(name.endsWith(".bmp"))
                    return true;
                else
                return false;
                */
                return name.endsWith(".bmp");

            }
        });


        System.out.println("len:"+arr.length);
        for(String name : arr)
        {
            System.out.println(name);
        }

     */
     打印流:
    该流提供了打印方法,可以将各种数据类型的数据都原样打印。

    字节打印流:
    PrintStream
    构造函数可以接收的参数类型:
     1,file对象。File
     2,字符串路径。String
     3,字节输出流。OutputStream



     字符打印流:
     PrintWriter
     构造函数可以接收的参数类型:
      1,file对象。File
      2,字符串路径。String
      3,字节输出流。OutputStream
      4,字符输出流,Writer。

   合并流  SequenceInputStream
   Vector<FileInputStream> v = new Vector<FileInputStream>();
    v.add(new FileInputStream("c:\\1.txt"));
        v.add(new FileInputStream("c:\\2.txt"));
        v.add(new FileInputStream("c:\\3.txt"));

        Enumeration<FileInputStream> en = v.elements();

        SequenceInputStream sis = new SequenceInputStream(en);

        FileOutputStream fos = new FileOutputStream("c:\\4.txt");

        byte[] buf = new byte[1024];

        int len =0;
        while((len=sis.read(buf))!=-1)
        {
            fos.write(buf,0,len);
        }

        fos.close();
        sis.close();

2 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
路过 学习。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马