package com.haligong.Jse;
import java.io.*;
import java.util.Scanner;
/**
* Created by Administrator on 2019/7/4.
*/
public class IOStream {
public static void main(String[] args) {
// FileCopy();
//RWCopy();
//BuffCopy();
//inAndOutput1();
try {
// inAndOutput2();
inAndOutput3();
// 对比一下inAndOutput4和inAndOutput3的写法
//inAndOutput4();
} catch (Exception e) {
e.printStackTrace();
}
}
static private void FileCopy(){
{
FileInputStream fin =null;
FileOutputStream fou =null;
double ds=0;
double de=0;
try {
// fin = new FileInputStream("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\1\\1.txt");
// fou= new FileOutputStream("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\2\\2.txt");
fin = new FileInputStream("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\1\\1.wmv");
fou= new FileOutputStream("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\2\\2.wmv");
int n=-1;
byte [] buff= new byte[1024];
ds=System.currentTimeMillis();
while((n=fin.read(buff))!=-1){
fou.write(buff,0,n);
//fou.write("我爱我家".getBytes())
}
de=System.currentTimeMillis();
} catch (java.io.IOException e) {
e.printStackTrace();
} catch(Exception e){
System.out.println("以防万一");
} finally{
try {
fou.flush();
fin.close();
fou.close();
System.out.println(de-ds/1000);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static private void RWCopy(){
Reader r=null;
Writer w=null;
try{
r=new FileReader("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\1\\1.txt");
w=new FileWriter("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\1\\3.txt");
int n=-1;
char [] buffer=new char[1024];
while((n=r.read(buffer))!=-1){
w.write(buffer);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try {
w.flush();
w.close();
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
static private void BuffCopy(){
FileInputStream fin =null;
FileOutputStream fou =null;
BufferedInputStream bin =null;
BufferedOutputStream bou=null;
try{
fin=new FileInputStream("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\1\\1.txt");
fou=new FileOutputStream("D:\\Documents\\Downloads\\Y1\\这里是流的文件所在地\\2\\5.txt");
bin=new BufferedInputStream(fin,4096);
bou=new BufferedOutputStream(fou,4096);
byte [] buffer=new byte[1024*2];
int n=-1;
while((n=bin.read(buffer))!=-1){
bou.write(buffer,0,n);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try {
bou.flush();
bou.close();
fou.flush();
fou.close();
bin.close();
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
static private void inAndOutput1(){
Scanner s= new Scanner(System.in);
while(true){
String re= s.nextLine();
if(re.toLowerCase().equals("q")){
System.exit(0);
}else{
System.out.println(re);
}
}
}
static private void inAndOutput2(){
InputStream i = System.in;
while(true){
// InputStream i = System.in;
byte [] buffer=new byte[1024];
try {
i.read(buffer);//这个才是真正的读数据
String s= new String(buffer);
if(s.toLowerCase().trim().equals("q")){
System.exit(0);
}else{
System.out.println(s.trim());
}
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
// try {
// i.close(); //不能关闭,否者会把Systme.in关闭了
// } catch (IOException e) {
// e.printStackTrace();
// }
}
}
}
static private void inAndOutput3(){
InputStream i=null;
BufferedReader r=null;
i = System.in;
while(true){
r=new BufferedReader(new InputStreamReader(i));
try {
String s= r.readLine();
if(s.trim().equals("q")){
System.exit(0);
}
System.out.println(s.trim());
} catch (IOException e) {
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
} finally{//我这样把整个try catch都写里面了不对,会造成IOException:stream close 下面参考一下inAndOutput4()的写法
try {
r.close();
i.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static private void inAndOutput4(){
InputStream i=null;
BufferedReader r= null;
try{
i=System.in;
r=new BufferedReader(new InputStreamReader(i));
while(true){
String s=r.readLine();
if(s.trim().toLowerCase().equals("q")){//或者这样写s.trim().equalsIgnoreCase("q");
System.exit(0);
}else{
System.out.println(s.trim());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
r.close();
i.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在运行程序时,会报错,如下图所示:
去stackoverflow中搜索了下发现原因:
他的意思就是说如果你把BufferedReader 输入流给关闭了,那么就就会把System.in也给关闭了,导致报出Stream close异常,解决的方法也简单,就是将不写close(),或者是改变一下写的顺序问题,参考一下inAndOutput4的写法就是将流的关闭放在while循环外面,每次循环并不会产生流的关闭,只有退出while循环的时候才关闭流