JavaIO流及多线程的学习[实验室第四讲]

1.字节流
面向设备写入,无缓冲区。

    /**
     * 字节流读取文件
     * @throws IOException 
     */
private static void readTest() throws IOException {
    File file = new File("D:/java/file.txt");
    FileReader fr = new FileReader(file);
    System.out.println((char)fr.read());
        //返回的是ASC码值
    }
    /**
     * 字节流读取文件
     * @throws IOException 
     */
private static void readTest() throws IOException {
    File file = new File("D:/java/file.txt");
    FileReader fr = new FileReader(file);
    char[] ch = new char[(int)file.length()];
    fr.read(ch);
    for(char c : ch){
        System.out.println(c);
    }
            }

2.字符流
面向缓冲区操作。用flush()方法

    public static void writeTest() throws IOException{
        //创建文件
        File file = new File("D:/java/file.txt");
        //用字符流向文件写入内容
        FileWriter fw = new FileWriter(file);
        fw.write("Android");
        fw.flush();
        fw.close();
    }

缓冲流

    /**
     * 缓冲字符流读取操作
     * @throws IOException 
     */
    public static void bufferTest() throws IOException{
        File file = new File("D:/java/file.txt");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        System.out.println(br.readLine());
    }

转换流
直接将你创建的流转换为writer的流,写入字符或者字符串

    /**
     * 转换流
     * @throws IOException 
     */
    public static void OutputStreamWriter() throws IOException{
        File file = new File("D:/java/file.txt");
        FileOutputStream fos = new FileOutputStream(file);
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        osw.write("Android",0,7);//字符,偏移量,写入的长度。
        osw.flush();
        osw.close();
        }

对象流
将对象以文件的形式写入磁盘里保存起来。

    public static void ObjectText() throws IOException, ClassNotFoundException{
        Person p = new Person("宁静",250);
        File file = new File("D:/java/file.txt");/*
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(p);
        oos.flush();
        oos.close();*/

        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Person person = (Person) ois.readObject();
        System.out.println(person);
    }

多线程:
1.并发和并行
并行:在同一时刻,多件事情一定同时进行。
并发:在同一时刻,多件事情一定不是同时进行的。//轮流执行
3.创建线程 开启线程
创建线程有两种基本方式:1).类


public class ThreadA extends Thread{
    public ThreadA(String name){
        super(name);
    }
    public void run(){
        for(int i=0;i<10;i++)
            System.out.println(getName() + "-->" + i);
    }
}

public class ThreadB extends Thread{
    public ThreadB(String name){
        super(name);
    }
    public void run(){
        for(int i=0;i<10;i++)
            System.out.println(getName() + "-->" + i);
    }
}

public class xiancheng {
    public static void main(String[] args){
        ThreadA tA = new ThreadA("线程A");
        ThreadB tB = new ThreadB("线程B");
        tA.start();
        tB.start();
    }
}

2).Runnable接口

public class ThreadTask implements Runnable {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i=0;i<10;i++)
            System.out.println(Thread.currentThread().getName() + "-->" + i);
        }

}

public class xiancheng {
    public static void main(String[] args){
        ThreadTask task = new ThreadTask();
        Thread t = new Thread(task);
        t.start();
    }
}
//两线程执行同一个任务
public class ThreadTask implements Runnable {
    private int num = 0;
    private Object lock = new Object();//同步锁
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(num<100){
            synchronized(lock)//同步代码块
            {
                num++;
                System.out.println(Thread.currentThread().getName() + "-->" + num);
            }
        }

    }
}

public class xiancheng {
    public static void main(String[] args){
        ThreadTask task = new ThreadTask();
        Thread t1 = new Thread(task,"线程A");
        Thread t2 = new Thread(task,"线程B");
        t1.start();
        t2.start();
    }
}
发布了22 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Joki233/article/details/53699794