Java学习第18天:其他类和IO流的初步接触

来源:互联网 发布:windows端口初始化失败 编辑:程序博客网 时间:2024/06/05 20:53
 ------- android培训、java培训、期待与您交流! ----------

until18

IO流和其它对象

1、其它对象-System

2、其它对象-Runtime

3、其它对象-Date

4、其它对象-Calendar

5、其它对象-Math-Random

6IO流概念

7IO-FileWriter

8IO异常处理方式

9IO文件的续写

10、文本文件读取方式

11、文本文件读取方式练习

12、考本文本文件

 

其它对象-System

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

out方法:标准输出

in方法:标准输入

获取系统信息:getProperties()

设置系统信息:setProperties()

PropertiesHashMap子类,故可以用map方法去除集合元素!

该集合存储的都是字符串,没有泛型限定

是可以跨平台的方法!

 

import java.util.*; public class Demo_1 { public static void main(String[] args) {// PropertiesProperties prop = System.getProperties();for(Object obj : prop.keySet()){System.out.println(obj+"=="+prop.get(obj));}//获取指定信息String value = System.getProperty("java.runtime.name");System.out.println(value);} }


 

其它对象-Runtime

Runtime对象:

该类并没有提供构造函数也不可以new对象!那么该类中的方法都是静态的。发现该类还有还有非静态的方法!说明获取该类对象有方法(该类提供的!返回值是本类类型!)

-------单列设计模式!

 

该方法是:static Runtime getRuntime();

 

public class Demo2 { public static void main(String[] args)throws Exception {Runtime r = Runtime.getRuntime();Process p = r.exec("E:\\Program Files\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe");//exec返回的类型是ProcessThread.sleep(5000);p.destroy();// 杀掉子进程。} }


其它对象-Date

GMT:格林威治

UT:科学计算

不过该方法已经过时了!参看DATEFORMAT(日期格式化)

 

yyyy-MM-dd  

其它对象-Calendar

i

mport java.util.Calendar; public class Demo4 { public static void main(String[] args) {Calendar cd = Calendar.getInstance();//System.out.println(cd);cd.set(2013,2,1);cd.add(Calendar.DAY_OF_MONTH,-1);String[] moons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};String[] days = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};int mn = cd.get(Calendar.MONTH);int day = cd.get(Calendar.DAY_OF_WEEK);System.out.println(cd.get(Calendar.YEAR)+"年");System.out.println(moons[mn]);System.out.println(cd.get(Calendar.DAY_OF_MONTH));} }

其它对象-Math-Random

import java.util.Random; public class Demo5 { public static void main(String[] args) {// TODO Auto-generated method stub//ceil返回大于指定数据的最小整数double b1 = Math.ceil(1.45);System.out.println(b1);//floor返回小于指定数据的最大整数double b2 = Math.ceil(1.99);System.out.println(b2);//round四舍五入!//pow(2,3)===2的三次方!也就是8Random r = new Random();for(int x = 0;x<10;x++){//double d = (int)(Math.random()*10+1);int d = r.nextInt(100);System.out.println(d);}} } 


IO流概念

用于处理设备之间的数据传输!

java对数据的操作是通过流的方式进行的!

数据:有字节流和字符流(字符流融合了编码表)

流:输入流和输出流!流对象在IO包当中

常用的基础流有:

字节抽象类:InputStream OutputStream

|--FileInputStream

字符抽象类:Reader  Writer

|--FileReader

IO-FileWriter

注意:创建FileWrite对象的时候,该对象在被new关键的时候要明确被操作的文件!该文件会被创建,如果存在的话就会被覆盖!

flush()是把流中的数据读入文件中

close()是关闭流!并且数据读入

IO异常处理方式

import java.io.*; public class Demo6 { public static void main(String[] args) {FileWriter fw = null;try {fw = new FileWriter("demo.txt");fw.write("好晚咯!加油");fw.flush();}catch (IOException e) {System.out.println(e.toString());}finally{try {if(fw!=null)fw.close();} catch (IOException e2) {System.out.println(e2.toString());}}} } 


IO文件的续写

 创建流时,传递一个true参数,代表不覆盖已有的文件。并在已有文件的末尾处进行数据续写。如果文件不存在,
        则创建一个文件。
         如:FileWriter fw = new FileWriter("Demo.txt",true);



文本文件读取方式

public class Demo7 { public static void main(String[] args) throws IOException {// 文件的读取FileReader fr = new FileReader("Demo.txt");int ch = 0;/*读取方式一while((ch=fr.read())!=-1){System.out.println((char)ch);}*///读取方式二char[] chs = new char[2];while((ch=fr.read(chs))!=-1){System.out.println(new String(chs,0,ch));}} }


文本文件读取方式练习

public class Demo8 { public static void main(String[] args) throws IOException {// TODO Auto-generated method stubFileReader fr = new FileReader("demo.java");int num = 0;char[] ch = new char[1024];while((num=fr.read(ch))!=-1){System.out.println(new String(ch,0,num));}} } 


考本文本文件

package until_18; import java.io.*; public class Demo9 { public static void main(String[] args) {// TODO Auto-generated method stubcopy2();}public static void copy1(){FileWriter fw = null;FileReader fr = null;try {fw = new FileWriter("demo1.txt",true);fr = new FileReader("demo.txt");int len = 0;while((len=fr.read())!=-1){fw.write(len);}} catch (IOException e) {System.out.println(e.toString());}finally{try {if(fw!=null)fw.close();} catch (Exception e2) {System.out.println(e2.toString());}try {if(fr!=null)fr.close();} catch (Exception e2) {System.out.println(e2.toString());}}}public static void copy2(){FileWriter fw = null;FileReader fr = null;try {fw = new FileWriter("demo2.txt",true);fr = new FileReader("demo.txt");int len = 0;char[] ch = new char[1024];while((len=fr.read(ch))!=-1){fw.write(ch,0,len);}} catch (IOException e) {System.out.println(e.toString());}finally{try {if(fw!=null)fw.close();} catch (Exception e2) {System.out.println(e2.toString());}try {if(fr!=null)fr.close();} catch (Exception e2) {System.out.println(e2.toString());}}} }


 


 ------- android培训、java培训、期待与您交流! ----------
0 0
原创粉丝点击