day18Properties。Runtime对象。Date。Calenda。Math。IO流。字符流。

/*
Properties。Runtime对象。Date。Calenda。Math。IO流。字符流。
*/


/*
学会自己查阅api方法。
System:类中的方法和属性都是静态的
out:标准输出,默认是控制台
in:标准输入,默认是键盘。

Properties getProperties() 
          确定当前的系统属性。
*/
import java.util.*;

class  SystemDemo
{
	public static void main(String[] args) 
	{
		//因为Properties是Hashtable的子类,也就是Map集合的一个子类对象
		//那么可以通过map的方法取出该集合中的元素
		//该集合中存储的都是字符串,没有泛型定义。
		Properties prop = System.getProperties();

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

		for (String name :setName )
		{
			String value = prop.getProperty(name);
			System.out.println(name +".."+value);
		}
	}
}

import java.util.*;

class  System1
{
	public static void main(String[] args) 
	{
		//因为Properties是Hashtable的子类,也就是Map集合的一个子类对象
		//那么可以通过map的方法取出该集合中的元素
		//该集合中存储的都是字符串,没有泛型定义。

		Properties prop = System.getProperties();

		
		/*错误的
		Set<String> nameSet = prop.stringPropertyNames();//1.6版本才有。

		for (String name :nameSet)
		{
			String value = prop.getProperty(name);
			System.out.println(name +".."+value);
		}
		*/

	
		
		/*
		for (Map.Entry<Object,Object> me:prop.entrySet() )
		{
			String name =(String) me.getKey();
			String value = (String)me.getValue();
			System.out.println(name+"::"+value);
		}
	*/

	/*获取所有属性信息
		for (Object obj:prop.keySet())
		{
			String value = (String)prop.get(obj);
			System.out.println(obj+"::"+value);
		
		}
		*/

		//如何在系统中自定义一些特有信息呢?
		System.setProperty("mekey","myvalue");
		
		//获取指定属性信息 value:Windows XP,如果没有,返回null
		String value = System.getProperty("os.name");

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

		//可不可以在虚拟机jvm启动时动态加载一些属性信息呢?
		String v = System.getProperty("haha");
		//java-Dhaha=qqqqq System1
		//v = qqqq
	}
}

/*
Runtime对象
该类并没有提供构造函数
说明不可以new对象,那么会直接想到该类中的方法是静态的
发现该类中还有非静态方法,
说明该类肯定会提供方法获取本类对象,而且该方法是静态的
并且返回值类型是本类类型。
该方法就是static Runtime getRuntime();

由这个特点可以看出该类使用了单例设计模式
*/
class  RuntimeDemo

{
	public static void main(String[] args) throws Exception
	{
		Runtime r = Runtime.getRuntime();
		//Process p = r.exec("c:\\winmine.exe");
		//r.exec("c:\\winmine.exe");没找到就抛异常。
		//r.exec("winmine.exe");如果当前目录没有,会去Path环境变量里找
		//Thread.sleep(4000);//为了观看现象
		//p.destroy();//杀掉子进程,秒杀。只能杀这个对象进程
		Process p = r.exec("notepad.exe SystemDemo.java");
		//打开相关文件
	}
}
import java.util.*;
import java.util.Date;
import java.text.*;
class  DateDemo
{
        public static void main(String[] args) throws ParseException
        {
        /*
                Date d = new Date();
                System.out.println(d);//打印的时间,希望有些格式

                //将模式封装到SimpleDateformat对象中。
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

                //调用format方法,让模式格式化指定Date对象。
                String time = sdf.format(d);
                System.out.println(time);
                */
                //二个日期之间的天数。 
                String str1 = "2012-3-17";
                String str2 = "2012-4-18";
                        test(str1,str2);    

        public  static void test(String str1,String str2)throws ParseException
        {        
                DateFormat dateFormat = DateFormat.getDateInstance();

                        dateFormat = new SimpleDateFormat("yyyy-MM-dd");

                        Date date1 = dateFormat.parse(str1);
                        Date date2 = dateFormat.parse(str2);//String格式日期转成日期对象

                        long time1 = date1.getTime();//2.通过日期对象获取毫秒数
                        long time2 = date2.getTime();
                        
                        int date = getDate(time1,time2);
                        System.out.println(date);
        }
        public  static int getDate(long l1,long l2)
                        {
                                long time = Math.abs(l1 - l2);//正数
                                int date = (int)(time/1000/60/60/24);//3.通过毫秒数得到天数
                                return date;
                        }                       
                }                     
}

import java.util.*;
import java.util.*;
import java.util.Date;
import java.text.*;
class  CalendarDemo
{
	public static void main(String[] args) 
	{
		/*
		Date d = new Date();
		System.out.println(d);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年");

		String s = sdf.format(d);
		System.out.println(s);
		*/	
		Calendar c = Calendar.getInstance();

		String[] month = {"一月","二月","三月","四月",
						"五月","六月","七月","八月",
						"九月","十月","十一月","十二月",};
				//查表法
		String[] weeks = {"","星期日","星期一","星期二","星期三",
			"星期四","星期五","星期六"};
		int index = c.get(Calendar.MONTH);
		int index1 = c.get(Calendar.DAY_OF_WEEK);

		System.out.println(c.get(Calendar.YEAR)+"年");
		//System.out.println((c.get(Calendar.MONTH)+1)+"月");
		System.out.println(month[index]);
		
		System.out.println(c.get(Calendar.DAY_OF_MONTH)+"日");
		//System.out.println("星期"+c.get(Calendar.DAY_OF_WEEK));
		System.out.println(weeks[index1]);

		//System.out.println(c.get(Calendar.DAY_OF_WEEK));
	}
}
import java.util.*;
/*
练习:
获取任意年的二月有多少天?
	思路:根据指定年设置一个时间就是c.set(year,2,1);//某一年的三月一日
	
	c.add(Calendar.DAY_OF_MONTH,-1);3月1日,向前推一天,就是2月的最后一天
	
2获取昨天的现在的这个时刻?
c.add(Calendar.DAY_OF_MONTH,-1)
*/
class CalendarDemo2 
{
	public static void main(String[] args) 
	{
		Calendar c = Calendar.getInstance();
		
		//对日历重新设定
		//c.set(2012,2,23);//写2代表是3月
		//printCalendar(c);

		//时间向后推几年add();
		//c.add(Calendar.YEAR,4);
		//printCalendar(c);
		
		//c.add(Calendar.MONTH,-1);加月
		//printCalendar(c);

		c.add(Calendar.DAY_OF_MONTH,18);
		printCalendar(c);
	}
	public static void printCalendar(Calendar c)
	{//和上面的一样,功能封装
		String[] month = {"一月","二月","三月","四月",
						"五月","六月","七月","八月",
						"九月","十月","十一月","十二月",};

		String[] weeks = {"","星期日","星期一","星期二","星期三",
			"星期四","星期五","星期六"};
		int index = c.get(Calendar.MONTH);
		int index1 = c.get(Calendar.DAY_OF_WEEK);

		System.out.println(c.get(Calendar.YEAR)+"年");
		//System.out.println((c.get(Calendar.MONTH)+1)+"月");
		System.out.println(month[index]);
		
		System.out.println(c.get(Calendar.DAY_OF_MONTH)+"日");
		//System.out.println("星期"+c.get(Calendar.DAY_OF_WEEK));
		System.out.println(weeks[index1]);
	}
}

import java.util.*;
class  MathDemo
{
	public static void main(String[] args)
	{
		Random r = new Random();
		for(int x=0;x<10;x++)
		{
			//int d =(int)(Math.random()*10+1);//第一次
			int d = r.nextInt(10)+1;
			sop(d);
		}
	}
	public static void show() 
	{
		double d = Math.ceil(12.34);
		//ceil方法特点是:返回大于指定数据的最小整数。(12)返回12.0

		double d1 = Math.floor(12.34);
		//floor方法特点是:返回小于指定数据的最大整数

		long l = Math.round(12.34);//四舍五入
		double d2 =	Math.pow(2,3);//二的三次方
		sop("d="+d);
		sop("d1="+d1);
		sop("L="+l);
		sop("d2="+d2);
	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}
/*练习:给定一个小数。保留小数的后二位
先转换成String,再取最后二位,小数点后有几位。
*/
/*
。IO流用来处理设备之间的数据传输
。Java对数据的操作是通过流的方式
。Java用来操作流的对象都在IO包中
。流按流向分为:输入流,输出流
流按操作数据分为两种:字节流和字符流

早期都是用字节流,为什么出现字符流呢?
字节流都能搞定,为了方便处理常见的字符。
字符流对象内部融合编码表。特别是字符转换流的内部融合编码表。

IO常用四个基类:
1.字节流的二个抽象基类:
	InputStream,OutputStream
2.字符流的二个抽象基类:
	Reader,Writer.

注:由这四个类派生出来的子类名字都是
以其父类名作为子类名的后缀。(前缀名是功能)
如:InputStream的子类FileInputStream
	Reader的子类 FileReader.

字符流特点:
IO流是用来操作数据的,
数据的最常见表现形式是:文件。
那么先以操作文件为主来演示
需求:在硬盘上创建一个文件并写入文字数据

找到一个专门用于操作文件的Writer子类对象
FileWriter.以其父类名作为子类名的后缀,前缀名是功能)
没有空构造函数。要操作文件,就要先有文件。
*/
import java.io.*;
class  FileWriterDemo
{
	public static void main(String[] args) throws IoException
	{
		//创建一个FileWriter对象,该对象一被初始化,
		//就必须要明确被操作的文件。
		//而且该文件会被创建到指定的目录下。
		// new FileWriter("demo.txt")如果该目录下已有同名文件,将被覆盖。
		//其实该步就是明确数据存放的目的地。
		FileWriter fw = new FileWriter("demo.txt",true);
		// 会抛出异常。new FileWriter("kk:\\demo.txt");
		//new FileWriter("demo.txt",true)传入true,不覆盖已有文件。并在末尾处添加。

		fw.write("abcde");//调用Write方法,其实写到流里面去了。

		//fw.flush();//刷新流对象中的缓冲中的数据,
					//将数据刷到目的地中。还可以再写入数据。

		fw.close();//关闭前先刷新,不可以再写入,流已经关闭。
	}
}

import java.io.*;
/*
IO异常专业的处理方式,
三句都会出异常,当第一句出异常时,
再执行第二句没意义,所以放在一起try
关流动作一定要执行。一般关流要用finally.
在try里面定义的变量,在finally里访问不到。
所以在外面建立引用,在try内初始化,这样fw作用整个函数。

close要单独try,
创建对象不成功呢?fw = new FileWriter("k;\\demo.txt");
所以加上,if(fw!=null)。另外每个流要分别去关。。。

*/

class FileWriterDemo2
{
	public static void main(String[] args) 
	{
		FileWriter fw =null;//为了最后的finally里的fw有指向
		try
		{
			 fw = new FileWriter("demo.txt");
			 //传入("demo.txt",true),不覆盖已有文件。并在末尾处添加。
		
			fw.write("abcdefg");//调用Write方法,其实写到流里面去了。+"/r/n"是换行。
		}
		catch (IOException e)
		{
			System.out.println(e.toString());
		}
		finally 
		{
			try
			{
				if(fw!=null)//创建对象不成功呢?一定要判断
					fw.close();
			}
			catch (IOException e)
			{
				System.out.println(e.toString());
			}
		}
	}
}

import java.io.*;
class  FileReaderDemo
{
	public static void main(String[] args) throws IOException
	{
		FileReader fr = new FileReader("demo.txt");
		//创建一个文件读取对象,和指定名称的文件相关联。
		//要保证该文件是已经存在的,如果不存在,
		//会发生FileNotFoundException

		//调用读取流对象的read方法。
		//一次读取一个字符,而且会自动向下读.
		//int ch1 = fr.read();
		/*
		System.out.println("ch1="+(char)ch1);

		int ch2 = fr.read();

		System.out.println("ch2="+(char)ch2);

		int ch3= fr.read();

		System.out.println("ch3="+(char)ch3);

		int ch4 = fr.read();

		System.out.println("ch2="+ch4);
		*/

		int ch = 0;
		while((ch=fr.read())!=-1)
		{
			System.out.println((char)ch);
		}
		/*
		while(true)
		{
			int ch = fr.read();
			if(ch==-1)
			{
				break;
			}

			System.out.println((char)ch);
		}
		*/
	
		fr.close();
	
	}
}

/*
第二种方式,通过字符数组进行读取
Read(char[] ch)

相比之下,第二种方式比第一种好。
第一种,读一个打一下,,
第二种,读一个存一下,存满后再全部一次性打。
*/
import java.io.*;
class  FileReaderDemo
{
	public static void main(String[] args) throws IOException
	{
		FileReader fr = new FileReader("demo.txt");

		//定义一个字符数组,用于存储读到的字符
		//该read(char[]) 返回的是读到的字符个数。重要!!

		char[] buf = new char[3];

		int num = fr.read(buf);

		System.out.println("num="+num+"..."+new String(buf,0,num));

		fr.close();
	/*
	FileReader fr = new FileReader("demo.txt");

	char[] buf = new char[1024];
	int num = 0;

	while((num = fr.read(buf))!=-1)
	{
		System.out.println(new String(buf,0,num));
	}
	fr.close();
	*/
	}
}
/*
将c盘一个文本文件复制到D盘。
复制的原理:
其实就是将C盘下的文件数据存储到D盘的一个文件中。
步骤:
1.在D盘创建一个文件,用于存储C盘文件中的数据
2定义读取流和C盘文件关联
3通过不断的读写完成数据存储
4关闭资源
*/
import java.io.*;
class  CopyText
{
	public static void main(String[] args) //throws IOException
	{
		copy_2();
	}
	//第一种:从C盘读一个字符,就向D盘写一个字符。
	public static void copy_1() throws IOException
	{
		FileReader fr = new FileReader("DateDemo.java");//与已有文件关联
		
		//创建目的地。
		FileWriter fw = new FileWriter("demo_copy.txt");//目的地

		int num = 0;

		while((num = fr.read())!=-1)
		{
			fw.write(num);//写入,关键,为什么写num?一个一个写
		}				//num这定义int类型,是为了避免char为-1情况,类型提升,
						//write()方法里有自动的类型下降,所以数据还是一样的,后面会讲到。
		fr.close();
		fw.close();
	}
	//第二种,先读取,存入缓存,存满再写
	public static void copy_2()
	{
		FileWriter fw = null;
		FileReader fr = null;
		try
		{
			fr = new FileReader("IoDemo.java");
			fw = new FileWriter("io_copy.txt");

			char[] buf = new char[1024];//缓冲区
			int len = 0;
			while((len=fr.read(buf))!=-1)//这里如何try,分开不好就try全部
			{
				fw.write(buf,0,len);
			}
		}
		catch (IOException e)
		{
			//System.out.print("复制失败,创建失败");
			throw new RuntimeException("读写失败");
		}
		finally
		{
			if(fr!=null)
			{
				try
				{
					fr.close();
				}
				catch (IOException e)
				{
					System.out.print("关闭失败");
				}
			}
			if(fw!=null)
				try
				{
					fw.close();
				}
				catch (IOException e)
				{
					System.out.print("关闭失败");
				}
		}
	}
}



插入图片io流拷贝文本文件图例










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值