老师帮忙看一下

来源:3-16 自由编程

万象天心

2020-03-07 22:08:41

FileOutputStream fos1=null,fos2 = null;
		BufferedOutputStream bos = null;
		try {
			fos1=new FileOutputStream(file1);
			fos2=new FileOutputStream(file2);
			bos=new BufferedOutputStream(fos2);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long startTime=System.currentTimeMillis();
		
		try {
			for(int i=0;i<100000;i++) {
			fos1.write(i);
			}
			fos1.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

老师,为什么fos1要初始化为null呢

是因为可能会出现IO异常吗

写回答

1回答

好帮手慕小尤

2020-03-08

同学你好,局部变量的引用数据类型,需要初始化为null。按理说 fos1 = new FileOutputStream(file1); 这一句就可以初始化了,但在 try {会出现编译性异常,因为编译的时候,不知道try里面有什么语句(编译只检查语法),只有运行的时候才知道try里面有什么,如果这句前面有一句抛出异常,则就不会执行到这一句了。所以需要先为fos1初始化为null。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

0

0 学习 · 11489 问题

查看课程