JAVA基础--db18_javaSystem&Math&Date&IO流

来源:互联网 发布:重装mac 保留 编辑:程序博客网 时间:2024/06/18 10:49

类System:类中的方法和属性都是静态的

System.in//读取键盘的输入

常见方法:

long      currentTimeMillis();

getProperties()获取系统信息:

package db_01;

 

importjava.util.Properties;

importjava.util.Set;

 

public classHelloWorld {

   public static voidmain(String[] args){

      Properties prop = System.getProperties();

      Set<String> set = prop.stringPropertyNames();

      for(String s : set){

         String value  = prop.getProperty(s);

         System.out.println(s+"-----------"+value);

      }

   }

}

private static finalString LINE_SEPARATOR = System.getProperty(“line.separator”);

类Runtime:没有构造方法,不能创建对象;单例设计模式

public classHelloWorld {

   public static voidmain(String[] args) throws IOException,InterruptedException{

      Runtime r =Runtime.getRuntime();

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

      Thread.sleep(5000);

      p.destroy();

   }

}

Math类:


random随机数

Date:日期类

两个构造:Date():将当前日期封装成对象          

Date(long date):将指定毫秒值封装成对象

日期对象和毫秒值之间的转换:

毫秒值è日期   newDate(system.currentTimeMillis)

日期è毫秒值  getTime方法(所以肯定还有setTime方法将毫秒值转成日期)

日期转成字符串:其实就是将日期格式转换,使用的是DateFormat类中的format方法

Jdk1.1之前用date,之后用Calendar类实现日期和时间字段之间转换,使用DateFomat类来格式化和解析日期字符串。Date中的相应方法已废弃。


不同的方法,显示不同的样式

SimpleDateFormat:DateFormat的子类,可以自定义风格


日期字符串è日期对象


其中,第三行是默认风格的可以直接解析,第四行是自定义风格的。

toLocalString()方法:


Calendar类:抽象类

有偏移方法,可以对日期偏移

Calendar c =Calendar.getInstance();

IO流:


输入输出相对于内存而言:

往内存弄就是输入(读),内存往外就是输出(写)

字节流+编码表=字符流

字符流的由来:其实就是,字节流读取文字字节数据后,不直接操作,而先查指定的码表,获取对应的文字。再对这个文字就行操作。

IO流常用基类:


入门程序、;


换行&续写:

public classHelloWorld {

   private static final String LINE_SEPARATOR = System.getProperty("line.separator");

   public static voidmain(String[] args) throws IOException {

      FileWriter fileWriter = newFileWriter("demo.txt");

      //文件没有就创建,有就覆盖前一个

      fileWriter.write("abcdexxxxx"+LINE_SEPARATOR +"xxxxxxxxx言言x");//将数据写到流里了,还没到目的地

      fileWriter.flush();//将数据写到目的地

      fileWriter.close();//关闭资源,关闭前会刷新缓冲区的数据到目的地

   }

}

换行:定义系统变量,这样适合每一种系统,记得要将常量定义在类中,不能定义在主函数里。

续写:new的时候,找对构造函数即可

FileWriter fw = newFileWriter(“Demo.txt”,true);

读取:


public classHelloWorld {

   public static voidmain(String[] args) throws IOException{

      FileReader fr = newFileReader("E:\\a.txt");

     

      int ch = 0;

      while ((ch=fr.read())!=-1){

      //因为上面读不到返回-1,所以这里写-1 

         System.out.println(ch);

      }

   }

}

上面是方法一:只读1个

下面是方法二:读的数组,其实就是每次读数组的长度,之前只读一个,当不满长度的时候,就读len的长度。和之前相比就是一次吃1粒米和1勺米的意思。


 

0 0
原创粉丝点击