1.常用API:
public static void main(String[] args) throws Exception {
// 获得当前时间毫秒值
System.out.println(System.currentTimeMillis());
// 属性类
Properties p = System.getProperties();
Set<String> set = p.stringPropertyNames();
for (Iterator<String> it = set.iterator(); it.hasNext();) {
String key = it.next();
System.out.println(key + "=" + p.getProperty(key));
if (key.equals("myclasspath"))
System.out.println("---------" + p.getProperty(key));
}
// 跨平台的换行
System.out.println("---" + p.getProperty("line.separator") + "---");
// 设置属性
System.out.println(p.setProperty("myclasspath", "G:\\study"));
// Runtime 类
Runtime run = Runtime.getRuntime();
System.out.println(run.toString());
Process process = run.exec("notepad G:\\1.txt");
// Thread.sleep(1000);
process.destroy();
// Math类
// 大于的最小整数
System.out.println(Math.ceil(1.1));
// 小于的最大整数
System.out.println(Math.floor(1.2));
// 四舍五入 -1
System.out.println(Math.round(-1.5));
// 2^8
System.out.println(Math.pow(2, 8));
System.out.println();
// Date DateFormat类
Date date = new Date();
System.out.println(date);
Thread.sleep(1000);
long time = System.currentTimeMillis();
// 毫秒转日期
System.out.println(new Date(time));
// 日期转毫秒
System.out.println(date.getTime());
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
String str_date1 = df.format(date);
System.out.println(str_date1);
// 自定义输出格式
DateFormat dfDemo = new SimpleDateFormat("yyyy-MM-dd");
String str_date2 = dfDemo.format(date);
System.out.println(str_date2);
// 字符串转日期
date = df.parse(str_date1);
System.out.println(date);
date = dfDemo.parse("1992-12-2");
System.out.println(date);
// "2012-3-17"到"2012-4-6"中间有多少天
str_date1 = "2012-3-17";
str_date2 = "2012-4-6";
time = dfDemo.parse(str_date2).getTime()
- dfDemo.parse(str_date1).getTime();
System.out.println(time/1000/60/60/24);
//Calendar
Calendar c = Calendar.getInstance();
System.out.println("year="+c.get(Calendar.YEAR));
System.out.println("month="+(c.get(Calendar.MONTH)+1));
System.out.println("day="+c.get(Calendar.DAY_OF_MONTH));
System.out.println("week="
+getWeek(c.get(Calendar.DAY_OF_WEEK)));
c.set(2012, 9, 8);
c.add(Calendar.YEAR, -2);
System.out.println("year="+c.get(Calendar.YEAR));
System.out.println("month="+(c.get(Calendar.MONTH)+1));
System.out.println("day="+c.get(Calendar.DAY_OF_MONTH));
System.out.println("week="
+getWeek(c.get(Calendar.DAY_OF_WEEK)));
//打印每年二月有几天
c.set(2012, 2, 1);
c.add(Calendar.DAY_OF_MONTH, -1);
System.out.println("year="+c.get(Calendar.YEAR));
System.out.println("month="+(c.get(Calendar.MONTH)+1));
System.out.println("day="+c.get(Calendar.DAY_OF_MONTH));
System.out.println("week="
+getWeek(c.get(Calendar.DAY_OF_WEEK)));
}
public static String getWeek(int num){
String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
return weeks[num];
}
2.IO流:用来处理设备之间的数据传送,Java对数据的操作是通过流的方式,Java用于操作流的对象都在IO包中。输入:将外设中的数据读取到内存中;输出;将内存的数据写入到外设中。流按操作数据分为:字节流与字符流。字符流的来由:字节流读取文字字节数据后,不直接操作而是先查指定的编码表,获取对应的文字,在对这个文字进行操作,相当于:字节流+编码表。
3.字节流的抽象基类:InputStream,OutputStream。字符流的抽象基类:Read,Writer。操作文字数据,先考虑字符流,
public class IODemo {
public static void main(String[] args) throws Exception {
// 写文件到内存
FileWriter fw = null;
try {
fw = new FileWriter("Demo.txt", false);
fw.write("11111111"
+ System.getProperty("line.separator"));
fw.write("11111111"
+ System.getProperty("line.separator"));
fw.write("4444444");
// 输出到存储
fw.flush();
} catch (IOException e) {
System.out.println(e.toString());
} finally {
try {
fw.close();
} catch (IOException e) {
throw new RuntimeException("关闭失败");
}
}
//读取
FileReader fr = null;
//第一种读取方式
try {
fr = new FileReader("Demo.txt");
int ch;
while((ch = fr.read())!= -1){
System.out.println((char)ch);
}
} catch (IOException e) {
System.out.println(e.toString());
} finally {
try {
fr.close();
} catch (IOException e) {
throw new RuntimeException("读取流关闭失败");
}
}
//第二种读取方式
try {
fr = new FileReader("Demo.txt");
int length = 0;
char[] buf = new char[1024];
while((length = fr.read(buf))!= -1){
//输出到控制台
System.out.println(new String(buf,0,length));
}
} catch (IOException e) {
System.out.println(e.toString());
} finally {
try {
fr.close();
} catch (IOException e) {
throw new RuntimeException("读取流关闭失败");
}
}
}
}
4.使用字符流复制一份文件:
public class IODemo {
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
//第一种读取
try {
fr = new FileReader("Demo.txt");
fw = new FileWriter("Copy_Demo.txt");
int length = 0;
char[] buf = new char[1024];
while ((length = fr.read(buf)) != -1) {
// 缓冲区的出现,提高了对数据的读写效率
fw.write(buf, 0, length);
}
} catch (IOException e) {
System.out.println(e.toString());
} finally {
if (fw != null)
try {
fw.close();
} catch (IOException e) {
throw new RuntimeException("输出流关闭异常");
}
if (fr != null)
try {
fr.close();
} catch (IOException e) {
throw new RuntimeException("读取流关闭异常");
}
}
//第二种读取
try {
fr = new FileReader("Demo.txt");
fw = new FileWriter("Copy2_Demo.txt");
int ch;
while ((ch = fr.read()) != -1) {
// 缓冲区的出现,提高了对数据的读写效率
fw.write(ch);
}
} catch (IOException e) {
System.out.println(e.toString());
} finally {
if (fw != null)
try {
fw.close();
} catch (IOException e) {
throw new RuntimeException("输出流关闭异常");
}
if (fr != null)
try {
fr.close();
} catch (IOException e) {
throw new RuntimeException("读取流关闭异常");
}
}
}
}
5.字符流的缓冲区:缓冲区的出现,提高了对数据的读写效率。对应类:BufferedReader,BufferedWriter。PS:缓冲区要结合流才可以使用,作用是在流的基础上对流的功能进行了增强。
6.使用缓冲读取流:
public class IODemo{
public static void main(String[] args) {
//读取流
FileReader fr = null;
//输出流
FileWriter fw = null;
//缓冲
BufferedReader br = null;
BufferedWriter bw = null;
//作为缓冲
String line = null;
try{
//初始化
fr = new FileReader("Demo.txt");
fw = new FileWriter("bufDemo.txt");
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
//read读取换行
//int ch;
//while((ch = br.read())!=-1){
// bw.write(ch);
// bw.newLine();
// bw.flush();
//}
//readLine不读取换行符
while((line = br.readLine())!= null){
bw.write(line);
//换行
bw.newLine();
bw.flush();
}
bw.flush();
}catch(IOException e){
System.out.println(e.toString());
}finally{
if(bw!=null)
try{
bw.close();
}catch (IOException e){
throw new RuntimeException("缓冲输出关闭失败");
}
if(br!=null)
try{
br.close();
}catch (IOException e){
throw new RuntimeException("缓冲读取流关闭失败");
}
}
}
}
7.LineNumberReader的使用:
public class IODemo{
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("Demo.txt");
LineNumberReader lnr = new LineNumberReader(fr);
BufferedWriter bw =
new BufferedWriter(
new FileWriter("lnrDemo.txt"));
String line = null;
lnr.setLineNumber(0);
while((line = lnr.readLine())!=null){
bw.write(lnr.getLineNumber()+":::"+line);
bw.newLine();
bw.flush();
}
bw.flush();
bw.close();
lnr.close();
}
}
8.装饰类与继承的示例:
class Animal{
public void show(){
System.out.println(".....Animal....");
}
}
class Dog{
private Animal animal;
public Dog(Animal animal){
this.animal = animal;
}
public void show(){
System.out.println("dog...........");
animal.show();
System.out.println(".............dog");
}
}
class Pig extends Animal{
@Override
public void show() {
System.out.println("--------pig");
super.show();
System.out.println("pig--------------");
}
}
public class IODemo{
public static void main(String[] args) {
Animal animal = new Animal();
Dog dog = new Dog(animal);
dog.show();
System.out.println("--------------------------------");
Pig pig = new Pig();
pig.show();
}
}
9.自定义缓冲类:
class MyBuffered {
private Reader r;
// 定义一个缓冲区
private char[] buf = new char[1024];
// 定一只一个指针用于操作缓冲区中的元素,当操作最后一个值后,pos变为0
private int pos;
// 定义一个计数器,用于技术缓冲区的大小
private int count = 0;
public MyBuffered(Reader r) {
this.r = r;
}
public int myRead() throws IOException {
if (count == 0) {
count = r.read(buf);
// 每一次获取缓冲之pos归0
pos = 0;
}
if (count < 0)
return -1;
char ch = buf[pos];
pos++;
count--;
return ch;
}
public String myReadLine() throws IOException{
StringBuilder sb = new StringBuilder();
int ch = 0;
while((ch = myRead())!=-1){
if(ch == '\r')
continue;
if(ch == '\n')
return sb.toString();
sb.append((char)ch);
}
if(sb.length() != 0){
return sb.toString();
}
return null;
}
public void myClose() throws IOException{
r.close();
}
}
public class IODemo {
public static void main(String[] args) throws Exception {
MyBuffered mb = new MyBuffered(
new FileReader("Demo.txt"));
String line = null;
while((line = mb.myReadLine())!= null){
System.out.println(line);
}
mb.myClose();
}
}
10.字节流:基本操作与字符流相同,但它不仅可以操作字符,还可以操作其他媒体文件。
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream("Demo.txt");
FileOutputStream fos = new FileOutputStream("fosDemo.txt");
//第一种
//int ch;
//while((ch = fis.read())!=-1){
// fos.write(ch);
//}
//第二种
int length = 0;
byte[] buf = new byte[1024];
while((length = fis.read(buf))!=-1){
fos.write(buf, 0, length);
}
fis.close();
fos.close();
}
11.FileInputStream,FileOutputStream中的flush方法内容为空,没有任何实现,调用无意义。而字节流的缓冲区,同样提高了对字节流的读写效率。
12.MP3文件拷贝比较:
public static void main(String[] args) throws Exception {
//BufferedInputStream bis =
// new BufferedInputStream(new FileInputStream(
// "将军令.mp3"));
//BufferedOutputStream bos = new BufferedOutputStream(
// new FileOutputStream("将军令1.mp3"));
//long time = System.currentTimeMillis();
//int ch;
//while((ch = bis.read())!=-1){
// bos.write(ch);
//}
//bos.close();
//bis.close();
//System.out.println(System.currentTimeMillis() - time);
FileInputStream fis = new FileInputStream("将军令.mp3");
FileOutputStream fos = new FileOutputStream("将军令2.mp3");
int length;
byte[] buf = new byte[1024];
while((length = fis.read(buf))!=-1){
fos.write(buf,0,length);
}
fos.close();
fis.close();
}
13.键盘读取:
public static void main(String[] args) throws IOException {
InputStream in = System.in;
OutputStream out = System.out;
int ch;
StringBuilder sb = new StringBuilder();
while ((ch = in.read()) != -1) {
if (ch == '\r')
continue;
if (ch == '\n') {
String temp = sb.toString();
out.write(temp.toUpperCase().getBytes());
out.write("\n".getBytes());
sb.delete(0, sb.length());
if (temp.equals("over"))
break;
}
sb.append((char) ch);
}
}
14.转换流:字符流与字节流之间的桥梁,方便字符流与字节流之间的操作,直接流中的数据都是字符的时候,转作字符流操作更高效。转换流:InputStreamReader,OutputStreamWriter。
15.将键盘输入存储到一个文件中:
public class IODemo {
public static void main(String[] args) throws Exception {
// 键盘输入字节流转为字符流处理
BufferedReader br =
new BufferedReader(
new InputStreamReader(System.in));
// 字符缓冲输出流转为字节流输出到文件
BufferedWriter bw =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("1.txt")));
String line = null;
while ((line = br.readLine()) != null) {
if (line.equals("over"))
break;
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
16.将文本内容输出到控制台:
public class IODemo{
public static void main(String[] args) throws Exception{
//转换流读取文本
BufferedReader br =
new BufferedReader(
new InputStreamReader(
new FileInputStream("Demo.txt")));
//输出流为控制台
BufferedWriter bw =
new BufferedWriter(
new OutputStreamWriter(System.out));
String line = null;
while((line = br.readLine())!= null){
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
17.将一个文本的内容复制到另一个文本:
public class IODemo{
public static void main(String[] args) throws Exception{
//转换流读取文本
BufferedReader br =
new BufferedReader(
new InputStreamReader(
new FileInputStream("Demo.txt")));
//输出流为控制台
BufferedWriter bw =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("xxx.txt")));
String line = null;
while((line = br.readLine())!= null){
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
18.流的操作规律:四个明确:1.明确源和目的。源:InputStream,Reader目的 OutputStream,Writer。2.明确数据是否是纯文本数据。源:纯文本:Reader 否:InputStream 目的:纯文本:Writer 否:OutputStream。3.明确具体的设备:硬盘 File,键盘 System.in 内存 数组,网络 Socket流。目的设备:硬盘 File 控制台 System.out 内存 数组 网络 Socket流。4.是否需要其它额外功能:是否需要高效 是 加上缓冲 。