缓冲流,转换流,对象流,进程,线程

1. 缓冲流:过滤流,一定要套接节点流。
1) 字节:BufferedInputStream/BufferedOutputStream:内部带了一个缓冲区,把物理文件中的内容读取到内部缓冲区中,之后再从内部缓冲区中读取到程序中。

例:

package com.wx;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * @author wx
 *  字节缓冲输入,输出流
 */
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
bis=new BufferedInputStream(new FileInputStream("d:"+File.separator+"123abc.jpg"));
bos=new BufferedOutputStream(new FileOutputStream("F:"+File.separator+"test.jpg"));

int i=bis.read();
while(i!=-1){
bos.write(i);
i=bis.read();
}
bos.flush();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}   

2) 字符:BufferedReader/BufferedWriter:

例1:

package com.wx;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
 * @author wx
 * 字符缓冲输出流  BufferedWriter
 */
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new FileWriter("d:"+File.separator+"a.txt"));
bw.write("第一行");
bw.newLine();
bw.write("第二行");
bw.newLine();
bw.write("第三行");
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
}


例2:
package com.wx;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
 * @author wx
 * BufferedReader 字符缓冲输入流
 */
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br=null;
try {
br=new BufferedReader(new FileReader("d:"+File.separator+"a.txt"));
String str=br.readLine();
while(str!=null){
System.out.println(str);
str=br.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
}


2. 转换流:转换流用于在字节流和字符流之间转换。Jave SE API提供了两种转换流:

 1) InputStreamReader需要和InputStream“套接”,它可以将字节流中读入的字节解码成字符

例:

package com.wx;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 * @author wx
 *  InputStreamReader  字节解码成字符
 */
public class Test4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStreamReader isr=null;
try {
isr=new InputStreamReader(new FileInputStream("d:"+File.separator+"b.txt"),"iso-8859-1");
int i=isr.read();
while(i!=-1){
System.out.println((char)i);
i=isr.read();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(isr!=null){
try {
isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
}


 2) OutputStreamWriter需要和OutputStream“套接”,它可以将要写入字节流的字符编码成字节

例:

package com.wx;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
 * @author wx
 * OutputStreamWriter 字符---》字节
 */
public class Test5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
OutputStreamWriter osw=null;
try {
osw=new OutputStreamWriter(new FileOutputStream("d:"+File.separator+"b.txt"));
osw.write("张三");
osw.write("王五");
osw.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(osw!=null){
try {
osw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}


3. 对象流:用于存储和读取基本类型数据或对象的过滤流

 1)ObjectOutputStream保存基本类型数据或对象:序列化

例:

package com.wx;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import com.model.Student;
/**
 * @author wx
 * 序列化
 */
public class Test6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new FileOutputStream("d:"+File.separator+"c.dat"));
oos.writeObject(new Student(1001,"张三",10,"北京"));

oos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(oos!=null){
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}


 2)ObjectInputStream读取基本类型数据或对象:反序列化

例:

package com.wx;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import com.model.Student;
/**
 * @author wx
 * 反序列化
 */
public class Test7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ObjectInputStream ois=null;
try {
ois=new ObjectInputStream(new FileInputStream("d:"+File.separator+"c.dat"));
Object obj=ois.readObject();
Student stu=(Student)obj;
System.out.println(stu.getId()+stu.getName()+stu.getAge()+stu.getAddress());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(ois!=null){
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}


注意:
 1) 能被序列化的对象所对应的类必须实现java.io.Serializable这个标识性接口
    基本类型的包装类、String类、java.util.Date、java.util.Calendar类都实现了Serializable接口。
 2) transient关键字修饰成员变量时,表示这个成员变量是不需要序列化的。
    static修饰的成员变量也不会被序列化。
 3) 实现了Serializable接口的类都应该生成一个private static final long serialVersionUID 序列化版本ID作为标识。这个标识会序列化到流中

4. 其它流
 1) 数据流:DataInputStream/DateOutputStream
 2) 打印流:PrintStream/PrintWriter,这类流的操作永远不会抛出IO异常
 3) 随机访问文件:RandomAccessFile类
 
 5. 进程:一个正在运行的程序,有独立的内存空间。进程之间的切换开销大。
   线程:是进程中的一条执行路径,共享进程的内存空间。线程之间的切换开销小。

★6. 创建线程的两种方式:

   1) 继承自java.lang.Thread类,重写它的run()方法,run()方法体就是线程体。

例:

package com.wx;
/**
 * @author wx
 * 继承Thread类 创建线程
 */
public class Test8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("主线程执行了");

MyThread my=new MyThread();
//启动线程
my.start();
MyThread1 my1=new MyThread1();
my1.start();

System.out.println("主线程执行完毕");
}
}
/**
 * 线程类
 * @author wx
 *
 */
class MyThread extends Thread{
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=1;i<=100;i++){
System.out.println("子线程1:"+i);
}
}

}

class MyThread1 extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=1;i<=100;i++){
System.out.println("子线程2:"+i);
}
}
}


   2) 实现java.lang.Runnable接口,实现它的run()方法。把这个Runnable实例作为参数构造成Thread的实例 

例:

package com.wx;
/**
 * @author wx
 *  
 */
public class Test9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("主线程:"+Thread.currentThread().getName());
MyThread3 my=new MyThread3();
Thread t=new Thread(my);
t.setName("线程一:");
t.start();
System.out.println("线程是否活着:"+t.isAlive());
System.out.println("线程的状态:"+t.getState());
System.out.println("线程名称:"+t.getName());
System.out.println("主线程执行完毕");
}
}

class MyThread3 implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=1;i<=100;i++){
System.out.println("子线程1:"+i);
}
}
}


   启动方式:
   1) MyThread mt = new MyThread();  mt.start(); 
   2) MyRun mr = new MyRun();  Thread t = new Thread(mr);  t.start();

7. Thread类中常用方法:
   1) public static Thread currentThread(); //当前线程的引用
   2) public ClassLoader getContextClassLoader();
   // InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("xxx.yyy");
   3) public String getName();
      public void setName(String name);
   4) public Thread.State getState();
   
8.线程的分类

用户线程:用户自己创建的线程
守护线程:专门给用户线程提供服务的就是守护线程。
GC就是典型的守护线程

为什么需要多线程?

提高用户体验
充分利用CPU
改善程序结构



例:

package com.wx;
/**
 * @author wx
 *用程序模拟铁路售票系统:实现通过四个售票点发售某日某次列车的1000张车票,一个售票点用一个线程表示。
 */
public class Test10 {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyTickets my=new MyTickets();
//创建四个窗口
Thread t1=new Thread(my);
t1.setName("窗口一:");
t1.start();
Thread t2=new Thread(my);
t2.setName("窗口二:");
t2.start();
Thread t3=new Thread(my);
t3.setName("窗口三:");
t3.start();
Thread t4=new Thread(my);
t4.setName("窗口四:");
t4.start();
}
}

class MyTickets implements Runnable{
int count=0;
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
if(count<100){
System.out.println(Thread.currentThread().getName()+"正在卖出第"+(count+1)+"票");
count++;
}else{
break;
}
}
}
}

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章