System.in引发的问题


public class Test1 {

	public static void main(String[] args) throws IOException {
		
		for (int i = 0; i < 5; i++) {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String readLine = br.readLine();
			System.out.println(readLine);
			br.close();
		}
		
	}
}



import java.io.*;

class Inlet {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str = br.readLine();
		System.out.println(str);
		br.close();
		BufferedReader bb = new BufferedReader(new InputStreamReader(System.in));
		str = bb.readLine();
		System.out.println(str);
		bb.close();
	}
}

这两段代码都会出现问题!

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 与
BufferedReader bb = new BufferedReader(new InputStreamReader(System.in));
都是由System.in封装而来,实质并没有改变,当你调用br .close(),会自动调用br 的实质System.in.close(),所以对于bb来说它已经关闭了。
而在第一个里面,你虽然关闭了br,但是第二个是通过new FileInputStream("e:\\lesson\\in.txt")新建的,所以你明白了吗?
关闭流的顺序一般遵循:先开后关,后开先关。

封装的话:先内后外的关

因为流的关闭是级联的。你把br close的时候System.in也被close所以你第一次是成功的后面都会报流已被关闭的异常 

不论你new 多少个BufferedReader这些都是外层的缓冲流。也叫高级流。但是实际上底层用的还是System.in。
这个流被关闭了。你new 多少个BufferedReader有什么用。我上面就说了这个System.in是System类被加载的时候就分配好的final变量。所以你System.in已经被关闭了。你外层的流在怎么new也是无用,因为底层的流被关闭了

System类暴露了一个setIn方法是可以用来设置System.in的也就是说你关闭了之后要在用System.setIn放入一个新的的InputStream给System.in  


转载于:https://my.oschina.net/u/1155509/blog/148664

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
System.Data.OleDb.OleDbException HResult=0x80004005 Message=外部表不是预期的格式。 Source=System.Data StackTrace: at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection) at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.OleDb.OleDbConnection.Open() at excl.Form1.TransferData(String excelFile, String sheetName, String connectionString) in G:\c++通讯录\excl\Form1.cs:line 39 此异常最初是在此调用堆栈中引发的: System.Data.OleDb.OleDbConnectionInternal.OleDbConnectionInternal(System.Data.OleDb.OleDbConnectionString, System.Data.OleDb.OleDbConnection) System.Data.OleDb.OleDbConnectionFactory.CreateConnection(System.Data.Common.DbConnectionOptions, System.Data.Common.DbConnectionPoolKey, object, System.Data.ProviderBase.DbConnectionPool, System.Data.Common.DbConnection) System.Data.ProviderBase.DbConnectionFactory.CreateConnection(System.Data.Common.DbConnectionOptions, System.Data.Common.DbConnectionPoolKey, object, System.Data.ProviderBase.DbConnectionPool, System.Data.Common.DbConnection, System.Data.Common.DbConnectionOptions) System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionPoolGroup, System.Data.Common.DbConnectionOptions) System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(System.Data.Common.DbConnection, System.Threading.Tasks.TaskCompletionSource<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions, System.Data.ProviderBase.DbConnectionInternal, out System.Data.ProviderBase.DbConnectionInternal) System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions) System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions) System.Data.ProviderBase.DbConnectionInternal.OpenConnection(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory) System.Data.OleDb.OleDbConnection.Open() excl.Form1.TransferData(string, string, string) (位于 Form1.cs 中)
06-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值