if(null != inputStream){ inputStream.close(); }
时间: 2024-10-19 18:10:22 浏览: 28
`if (fileInputStream != null)` 的条件用于检查文件输入流 `fileInputStream` 是否已经被初始化并且非空。在Java中,如果尝试对一个 `null` 对象调用方法(如 `.close()`),会抛出 `NullPointerException`。这是因为`null` 表示对象尚未创建或已被垃圾回收,此时试图访问其属性或方法是非法的。
在你的示例中,`if (null != inputStream)` 是为了确保在调用 `inputStream.close()` 前,`inputStream` 已经被成功创建并赋值,避免因尝试关闭未分配的对象而导致的异常。这是一种常见的实践,用来防止资源泄露和运行时错误。
如果你有一个 `FileInputStream` 的实例,可以这样演示它的作用:
```java
FileInputStream inputStream = new FileInputStream("path_to_file"); // 创建并初始化流
// ... 执行其他操作 ...
if (inputStream != null) {
try {
inputStream.close(); // 如果流存在,关闭它
} catch (IOException e) {
System.out.println("Error closing the stream: " + e.getMessage());
}
}
```
相关问题
public static void copy(File src, File dst) { try { InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE); out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE); byte[] buffer = new byte[BUFFER_SIZE]; while (in.read(buffer) > 0) { out.write(buffer); } } finally { if (null != in) { in.close(); } if (null != out) { out.close(); } } } catch (Exception e) { e.printStackTrace(); } } public static void copyFile(File sourceFile, File targetFile) { BufferedInputStream inBuff = null; BufferedOutputStream outBuff = null; try { inBuff = new BufferedInputStream(new FileInputStream(sourceFile)); outBuff = new BufferedOutputStream(new FileOutputStream(targetFile)); byte[] b = new byte[1024 * 1024]; int len; while ((len = inBuff.read(b)) != -1) { outBuff.write(b, 0, len); } outBuff.flush(); } catch (Exception e) { } finally { if (inBuff != null) try { inBuff.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (outBuff != null) try { outBuff.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static String generateFileName(String fileName) { String formatDate = new SimpleDateFormat("yyMMddHHmmss") .format(new Date()); int random = new Random().nextInt(10000); int position = fileName.lastIndexOf("."); String extension = fileName.substring(position); return formatDate + random + extension; }
这段代码实现了文件的复制和生成新的文件名。首先是一个copy方法,使用了InputStream和OutputStream来读取和写入文件,使用了缓冲区来提高读写效率。接着是一个copyFile方法,使用了BufferedInputStream和BufferedOutputStream来读取和写入文件,并且设置了缓冲区的大小为1MB。最后是一个generateFileName方法,通过获取当前时间和一个随机数来生成一个新的文件名,保证了文件名的唯一性。值得注意的是,这些方法中都有异常处理机制,确保了程序的稳定性。
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.core; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class ConnDB { public Connection conn = null; public Statement stmt = null; public ResultSet rs = null; private static String propFileName = "/com/connDB.properties"; private static Properties prop = new Properties(); private static String dbClassName = "com.mysql.jdbc.Driver"; private static String dbUrl = "jdbc:mysql://127.0.0.1:3306/db_librarysys?user=root&password=aaaaaa&useUnicode=true"; public ConnDB() { try { InputStream in = this.getClass().getResourceAsStream(propFileName); prop.load(in); dbClassName = prop.getProperty("DB_CLASS_NAME"); dbUrl = prop.getProperty("DB_URL", dbUrl); } catch (Exception var2) { var2.printStackTrace(); } } public static Connection getConnection() { Connection conn = null; try { Class.forName(dbClassName).newInstance(); conn = DriverManager.getConnection(dbUrl); } catch (Exception var2) { var2.printStackTrace(); System.out.println("wgh:" + dbUrl); } if (conn == null) { System.err.println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:" + dbClassName + "\r\n链接位置1:" + dbUrl); } return conn; } public ResultSet executeQuery(String sql) { try { this.conn = getConnection(); this.stmt = this.conn.createStatement(1004, 1007); this.rs = this.stmt.executeQuery(sql); } catch (SQLException var3) { System.err.println(var3.getMessage()); } return this.rs; } public int executeUpdate(String sql) { boolean var2 = false; int result; try { this.conn = getConnection(); this.stmt = this.conn.createStatement(1004, 1007); result = this.stmt.executeUpdate(sql); } catch (SQLException var4) { result = 0; } return result; } public void close() { try { if (this.rs != null) { this.rs.close(); } if (this.stmt != null) { this.stmt.close(); } if (this.conn != null) { this.conn.close(); } } catch (Exception var2) { var2.printStackTrace(System.err); } } }
这是一个 Java 类,名为 ConnDB,用于连接数据库。它通过读取配置文件中的信息,获取数据库的连接信息。其中,getConnection() 方法用于获取数据库连接对象,executeQuery() 方法用于执行查询语句并返回结果集,executeUpdate() 方法用于执行更新语句并返回影响的行数,close() 方法用于关闭结果集、语句和连接。这个类使用了 JDBC API 来操作数据库。
阅读全文