黑马程序员_javaIO_第一天

来源:互联网 发布:基础编程学习 编辑:程序博客网 时间:2024/05/17 23:34

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

今天是开始黑马基础学习的第一天,首先看的是毕老师的javaIO基础的第18天 和第19天。

巩固和熟悉了java中的几个类。System, Runtime,Date,SimpleDateFormat还有IO中的类,inputStream,outStream,reader,writer,filereader,filewriter。开始以为从新再开J2se部分的东西会很乏味,最后才知道我错了,毕向东老师讲的很不错,说话很有趣,很有吸引力,也很真实,和马士兵的风格有些累死,不过比上学堂的基础视频要深不少。总之,今天学的很充实,学会的也不少。

二话不说上代码吧。

/***Administrator
 *2012-6-9上午8:45:42
 *SystemDemo.java
 */
package jun.io.path;


import java.util.Properties;
import java.util.Set;


/**
 * @author Administrator 对System对象的复习
 */
public class SystemDemo {


public static void main(String[] args) {
//System 中的方法都是静态的 且不能被实例化
Properties prop = System.getProperties();
//properties 是继承hasetable的子类 

/* for (Object obj : prop.keySet()) {
String value = (String) prop.get(obj);
System.out.println(obj + "=="+value);
}*/

/* Set<String>  name= prop.stringPropertyNames();
for(String s:name) {
String value = prop.getProperty(s);
System.out.println(s+"="+ value);
}*/
//根据key 获取虚拟机加载的属性
String osName = prop.getProperty("os.name");
System.out.println(osName);
//自定义加载的key-value 的系统信息
String osName1 = prop.getProperty("os.name");
System.out.println(osName1);

//这个setProperty(String key, String  value) 的返回的obj 到底是什么 
//可以修改原有的key-value
Object obj= prop.setProperty("os.name", "window 8");
Object  obj1= prop.setProperty("nihao", "hahah");
System.out.println(obj);  //这儿输出的是Windows 7 
System.out.println(obj1);  //为什么这儿是null
Set<String>  name= prop.stringPropertyNames();
for(String s:name) {
String value = prop.getProperty(s);
System.out.println(s+"="+ value);
}
//jvm动态加载些属性值 
System.out.println(prop.getProperty("jun"));
//在vm自变量中加入   -Djun=jun  即可看见动态的获取的值
}
}


package jun.io.path;

import java.io.IOException;
public class RuntimeDemo {
public static void main(String[] args) {
//Runtime 这个类使用了单例设计模式
Runtime rt = Runtime.getRuntime();
try {
// 可以启动任意程序
rt.exec("notepad  D:/code/JSP验证码的产生.txt");
rt.exec("D:\\Program Files\\Tencent\\QQ\\Bin\\qq.exe");
Process pr = rt.exec("calc.exe");


Thread.sleep(3000);
//杀掉子进程
pr.destroy();


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}


Runtime 是个很强大的类, 可以启动电脑上的任何程序 ,还可以杀死自己启动的进程。每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。可以通过getRuntime 方法获取当前运行时。

应用程序不能创建自己的 Runtime 类实例。 


下面是基本的IO测试

/***Administrator
 *2012-6-9上午11:32:36
 *TestIO.java  文件的读写
 */
package jun.io.path;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


import org.junit.Test;


public class TestIO {


public static void main(String[] args) {
FileWriter fw = null;
FileReader fr = null;
try {
// 创建FileWriter对象 明确操作对象
// 注意 该目录下有同名文件 将被覆盖 传true 表示追加数据 这个方法 是对outputStream的覆写


fw = new FileWriter("demo.txt", true);
fw.write("abcdesdfadsf");


// 调用write方法 将字符串写入流中 就是写入内存中
fw.flush();
// 刷新流对象中的缓冲 将写入内存中的数据写到所指向的文件对象中
char[] ch = new String("helloworld").toCharArray();
fw.write(ch, 0, ch.length);
fw.flush();
// 创建文件读取流对象 且应该保证这个文件存在 要不会抛FileNotFoundException
fr = new FileReader("demo.txt");
/*
* int cha0=fr.read(); //int read() 方法一次读一个 而且是自动向下读 read读到结尾时 会返回-1
* System.out.println("读单个字符"+ (char)cha0); int cha1=fr.read();
* System.out.println("读单个字符"+ (char)cha1);
*/
/*
* int b=0; while((b=fr.read()) != -1) {
* System.out.print((char)(b)); }
*/
// 技术细节 linux 中换行"\n" 
//windows中的换行 (在xp系统记事本中看"\n"是个黑点的话 win7下没有影响) 应采用"\r\n"

String str = "我要拥抱\n大海\n飞向\r\n蓝天";
fw.write(str);
fw.flush();


// 通过字符数组进行读取
int len = 0;
char[] buf = new char[100];
while ((len = fr.read(buf)) != -1) {
System.out.println(new String(buf, 0, len));


}


// IO异常的处理方式


} catch (IOException e) {
e.printStackTrace();
} finally {


try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


}
//练习  读取一个java文件并打印在控制台
@Test
public void testPrint() {
String filepath="src//jun//io//path18//DateDemo.java";
BufferedReader br =null;
try {
br =new BufferedReader(new FileReader(new File(filepath)));
String s=null;
while((s=br.readLine())  != null)
{
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}


}



使用缓冲的IO BufferedReader BufferedWriter

/***Administrator
 *2012-6-9下午2:10:52
 *TestIO1.java
 */
package jun.io.path;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;


import org.junit.Test;


public class TestIO1 {
@Test
public void testCopy() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader("src/1.txt"));
bw = new BufferedWriter(new FileWriter("src/1.txt"));


for (int i = 0; i <= 10; i++) {
UUID uuid = new UUID(64, 64).randomUUID();
bw.write(uuid.toString());
bw.newLine();// 换行
}
// 注意缓冲区的刷新
bw.flush();
// BufferedReader 返回只并不包含行终止符号 所以要注意换行
String s = null;
while ((s = br.readLine()) != null) {
System.out.println(s);
}


} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


Date类的格式化 和使用 

/***Administrator
 *2012-6-9上午9:55:04
 *DateDemo.java
 */
package jun.io.path;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


import org.junit.Test;


public class DateDemo {


public static void main(String[] args) {
// java.sql.Date 是java.util.Date的子类 注意两者的转化和读取


Date date = new Date();
System.out.println(new java.sql.Date(date.getTime()));
System.out.println(date.toLocaleString());
Date date1 = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日  E  hh:mm:ss");
System.out.println(sdf.format(date1));
// System.out.println(sdf.toLocalizedPattern());
// 查表法
String[] month = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月",
"九月", "十月", "十一月", "十二月" };
String[] week = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };


Calendar cal = Calendar.getInstance();
int index_month = cal.get(Calendar.MONTH);
int index_week = cal.get(Calendar.DAY_OF_WEEK);


System.out.println(month[index_month]);
System.out.println(week[index_week - 1]);


System.out.println(cal.get(Calendar.YEAR) + "年"
+ ((cal.get(Calendar.MONTH)) + 1) + "月"
+ cal.get(Calendar.DAY_OF_MONTH) + "日 " + "星期"
+ (cal.get(Calendar.DAY_OF_WEEK) - 1));


// 设置时间 set()


cal.set(2012, 5, 20);
System.out.println(cal.get(Calendar.YEAR) + "年"
+ ((cal.get(Calendar.MONTH)) + 1) + "月"
+ cal.get(Calendar.DAY_OF_MONTH) + "日 " + "星期"
+ (cal.get(Calendar.DAY_OF_WEEK) - 1));
// 时间量的偏移
cal.add(Calendar.DAY_OF_MONTH, -10);
System.out.println(cal.get(Calendar.YEAR) + "年"
+ ((cal.get(Calendar.MONTH)) + 1) + "月"
+ cal.get(Calendar.DAY_OF_MONTH) + "日 " + "星期"
+ (cal.get(Calendar.DAY_OF_WEEK) - 1));


}


public int getFebDays(int year) {
int days = 0;
if (year <= 0) {
System.out.println("你的输入有误");
days =-1;
} else {
if ((year % 4 == 0) && (year % 100 != 0) || (year % 100 == 0)) {
days = 29;
} else {
days = 28;
}



}
return days;
}

public int getFebDays1(int year) {
//毕老师提供的思路  时间量偏移
int days = 0;
Calendar cal= Calendar.getInstance();
cal.set(year, 2, 1);
cal.add(Calendar.DAY_OF_MONTH, -1);
days = cal.get(Calendar.DAY_OF_MONTH);
return days;
}


// 练习1 获取任意年的二月有多少天
@Test
public void test1() {

/* System.out.println(getFebDays(-2001));
System.out.println(getFebDays(2001));
System.out.println(getFebDays(2012));*/

System.out.println(getFebDays1(-2001));
System.out.println(getFebDays1(2001));
System.out.println(getFebDays1(2012));

}


// 联系2 获取昨天的现在这个时刻

@Test
public void test2() {
Calendar cal =Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1);
System.out.println((new Date(cal.getTimeInMillis())).toLocaleString());
}


}

当然今天作重要的是了解啦装饰设计模式。这种设计模式是对原有对象进行功能增强,可以在定义新类的构造函数,将对象传入。java的IO包中很多地方都采用了这样一种设计模式,如我们常用的 

new BufferedReader(new FileReader(new File(String filepath)) ) ;

BufferedReader BufferWriter BufferedInputStream BufferedOutputStream这样的类都是修饰类,如BufferedReader是对FileReader进行修饰加强,bufferedReader中有个特好用的方法readline(),通过这个方法每次可以读一行,提高了读写的效率,返回每行的串表示(不含换行标记),读到文本末尾时,再调用则返回null。

装饰设计模式是将继承体系进行优化,装饰比继承更灵活,避免了继承的臃肿和关系的冗余。装饰因为是增强已有对象的功能,常常装饰类和被装饰类是同一个体系。如FileReader和BufferReader都是Reader的子类。


技术细节:1.liunx 中的换行和windows中的换行不一样。linux中的换行是"\n" ,windows则需要用“\r\n”这样在记事本中才可以识别。(若用“\n”写时,windows 7 中没有效果,xp中则会显示黑点)。当然,BufferedWriter中有个newLine() 调用这个方法可以跨平台的。这个问题在写文本文件换行时要注意。

2.在进行文件读写时,建议采用装饰类。BufferedInputStream (BufferedReader) BufferedOutputStream( BufferedWriter),这样的带有缓冲功能的对象能提高读写效率,这也是sun公司所推荐的。所以读写时,应该在流上套上Buffered 这样便于操作和提高效率。当然,要注意流的刷新和关闭。


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

原创粉丝点击