if(reader!=null); try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if(connection!= null){ connection.disconnect(); } 为什么是不为空时关闭连接
时间: 2024-02-10 19:04:38 浏览: 38
这段代码中的if语句是有问题的,if语句后面多了一个分号,这会导致if语句中的代码块始终会被执行,而不管reader是否为空。正确的写法应该是:
```
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
```
这里的if语句是用来判断reader是否为空,如果不为空则执行关闭操作。因为在Java中,如果一个资源没有被正确关闭,可能会导致内存泄漏或者资源的浪费,所以在使用完资源后需要及时关闭。而在这段代码中,connection的关闭没有在if语句中判断,是因为即使connection为空,调用disconnect()方法也不会抛出异常,所以直接关闭即可。
相关问题
指出以下代码错误的地方,public static String readSysClassFile(String sys_path){ String prop =""; BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(sys_path)); prop = reader.readLine(); if (prop != null && prop. length()>0){ prop = prop .substring(0,15); } }catch ( IOException e){ e.printStackTrace(); }finally { if(reader != null){ try{ reader.close(); }catch ( IOException e){ e.printStackTrace(); } } } if (Textutils.isEmpty(prop)){ prop=“NULL”; } return prop; }
以下是代码中的错误:
1. 在第10行,substring()函数的第二个参数应该是一个整数值,但是代码中使用了中文逗号“,”,应该用英文逗号“,”代替。
2. 在第20行,Textutils应该是TextUtils,大小写有误。
3. 在第21行,字符串应该用双引号“"”包裹,而不是中文双引号““”。
以下是修改后的代码:
```
public static String readSysClassFile(String sys_path){
String prop ="";
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(sys_path));
prop = reader.readLine();
if (prop != null && prop.length() > 0){
prop = prop.substring(0, 15);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(reader != null){
try{
reader.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
if (TextUtils.isEmpty(prop)){
prop = "NULL";
}
return prop;
}
```
下面的函数是从 /sys下面读取某个文件的内容,请指出其中的错误 public static String readSysClassFile(String sys_path) { String prop = ""; BufferedReaderreader = null; try{ reader = new BufferedReader(new FileReader(sys_path)); prop = reader.readline(); if (prop != null && pro.length() > 0){ #Substring the prop prop = pro.Substring(0,15); } } } catch (IOException e){ e.printStackTrace(); }finally { if (reader != null){ try { reader.close(); }carch (IOException e){ e.printStackTrace(); } } } if (TextUtils.isEmpty(prop)){ prop= "NULL" } return prop; }
这段代码中有以下错误:
1. `BufferedReaderreader = null;` 应该为 `BufferedReader reader = null;`
2. `prop = reader.readline();` 应该为 `prop = reader.readLine();`
3. `if (prop != null && pro.length() > 0){` 应该为 `if (prop != null && prop.length() > 0){`
4. `#Substring the prop` 这一行应该是注释,应该改为 `// Substring the prop`
5. `}carch (IOException e){` 应该为 `}catch (IOException e){`
6. `if (TextUtils.isEmpty(prop)){` 前面没有导入 `TextUtils` 类,需要加上 `import android.text.TextUtils;`
修改后的代码如下:
```
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public static String readSysClassFile(String sys_path) {
String prop = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(sys_path));
prop = reader.readLine();
if (prop != null && prop.length() > 0) {
// Substring the prop
prop = prop.substring(0, 15);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (TextUtils.isEmpty(prop)) {
prop = "NULL";
}
return prop;
}
```