使用多线程实现把键盘的输入显示在屏幕上和存在指定磁盘中

import java.lang.*;
import java.io.*;
public class ThreadDemo1 {
public static  void main(String[]args){
ThreadDemo2  demo2= new ThreadDemo2();
ThreadDemo3  demo3= new ThreadDemo3();
demo2.start();
demo3.start();
}
}


class ThreadDemo2 extends Thread{
public void run(){

try{

//在java中可以不指定文件类型。

File file = new File("F:"+ File.separator+ "test");     

//从键盘取得输入数据;

System.out.println("输入数据");

BufferedReader input =  new BufferedReader(new InputStreamReader(System.in));
String str=input.readLine();
PrintStream output= new PrintStream(file);
output.print(str) ;
input.close();
output.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
}

class ThreadDemo3 extends Thread{
public void run(){
try {
System.out.println("输入数据");
BufferedReader input =  new BufferedReader(new InputStreamReader(System.in));
String str=input.readLine();
System.out.println(str);
input.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

你可能感兴趣的:(java)