ICode9

精准搜索请尝试: 精确搜索
  • java-try-with-resources无法调用close()2019-11-20 15:01:37

    我正在使用方便的try-with-resources语句关闭连接.在大多数情况下,此方法效果很好,但仅以一种完全简单的方法无法正常工作.即,这里: public boolean testConnection(SapConnection connection) { SapConnect connect = createConnection(connection); try ( SapApi sapApi = co

  • java-最终尝试通过codestyle / checkstyle紧密自动重构为try-with-resources2019-11-19 02:01:54

    我现在在一家公司工作,直到大约一年前才使用Java 1.6.他们切换到1.7,但是仍然有很多重构工作要做​​(我希望1.8很快会在议程中). 在Eclipse中,我们将Checkstyle插件用于跨项目的相同代码样式.今天我们同意这样的构造: Connection conn = null; try{ conn = new Connection();

  • Java将扫描仪与尝试资源一起使用2019-11-10 19:01:24

    我有两个版本的Java代码,它们会在用户键入“ q”之前获取用户输入 版本1: public class Test { public static void main(String[] args) { String input = ""; while (!input.equals("q")) { Scanner scanner = new Scanner(System.in);

  • java-SpotBugs是否报告未在此处关闭资源的误报?2019-11-10 18:00:31

    我有这样的代码: public static MyObject forId(long myObjectId, Connection cxn) throws SQLException { try (PreparedStatement stmt = cxn.prepareStatement(selectMyObjectById))) { stmt.setLong(1, myObjectId); try (ResultSet res = stmt.executeQue

  • 声纳:如何使用try-with-resources关闭FileOutputStream2019-10-11 18:03:24

    声纳给出了一个错误,该FileOutputStream应该关闭.我需要修改以下代码以使用try-with-resources.我该怎么做呢? public void archivingTheFile(String zipFile){ byte[] buffer = new byte[1024]; try{ FileOutputStream fos = new FileOutputStream(zipFile);

  • java – 为什么不使用Catch或者最后编写Try-With-Resources?2019-10-04 13:13:21

    为什么在没有Catch或Finally的情况下编写Try,如下例所示? protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintW

  • java – 尝试/尝试使用资源以及Connection,Statement和ResultSet关闭2019-10-02 00:01:01

    我最近和我的教授讨论了如何处理基本的jdbc连接方案.假设我们想要执行两个查询,这就是他的建议 public void doQueries() throws MyException{ Connection con = null; try { con = DriverManager.getConnection(dataSource); PreparedStatement s1 = con

  • java – Stream上的collect操作是否关闭流和底层资源?2019-09-27 06:03:43

    以下代码是否需要包含在try-with-resources中以确保底层文件已关闭? List<String> rows = Files.lines(inputFilePath).collect(Collectors.toList()); 解决方法:正如重载Files#lines(Path, Charset)方法的javadoc所述 The returned stream encapsulates a Reader. If timely di

  • Java中的try-with-resources语句2019-08-30 21:00:01

    在这个Java程序示例中: package test; import java.sql.DriverManager; import java.sql.Connection; import java.sql.Statement; public class Test { private static void example(){ String url = "jdbc:oracle:thin:@//localhost:7856/xe"; Str

  • java – 在try中使用资源,使用之前创建的资源语句2019-07-15 07:01:46

    从Java 7开始,我们可以使用try资源: try (One one = new One(); Two two = new Two()) { System.out.println("try"); } catch (Exception ex) { ... } 现在我的问题是,为什么我必须在try-statement中创建对象?为什么我不允许在语句之前创建对象,如下所示: One one = new One()

  • java – try-with-resources中的死代码警告,但不是在翻译的try-catch-finally中2019-07-11 07:02:35

    下面的代码使用Java 8中引入的try-with-resources结构.偶然的Throw()方法被声明为抛出OccasionalException,Resource的close()方法抛出一个CloseException. Eclipse(版本:Neon Release(4.6.0),Build id:20160613-1800)在标有//死代码的行上添加一个警告,表示该分支是死代码.隐含地,Ec

  • java – eclipse try-with-resource模板?2019-07-08 19:00:55

    Eclipse支持try-with-resource,有点像这样: try(Outputstream resource = new FileOutputStream(file)){ // do something... } 自从这个特色添加到eclipse以来已经有好几年了,但是没有模板“try-with-reousource”.只存在一个是“try-catch”. 我尝试制作模板,比如try($type {} $

  • Java 8 FilterOutputStream异常2019-06-28 23:48:31

    这是对Java 8中的FilterOutputStream.close()方法的更改,它导致了一些问题. (见http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf) 在以前的Java版本中,以下代码无需抛出异常即可运行.但是,在Java 8下,当try-with-resources机制关闭流时,我们总是会遇到异常. try( Inpu

  • 使用try-with-resources和HikariCP连接泄漏2019-06-23 17:49:55

    以下代码触发连接泄漏警告.我使用的是OpenJDK 1.7.0_80和HikariCP 2.2.5(也可以使用最新的HikariCP 2.3.9重现).我错过了什么吗? import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import org.junit.Test; import com.zaxxer.hika

  • java – 我应该为每行日志使用try-with-resources语句吗?2019-06-12 01:58:18

    我想在每次发生事件时将(附加文本)记录到文件中.我发现这可能是使用Java 7的try-with-resources语句执行此操作的正确方法: public void log(String textLine) { try(PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("Logfile.txt", true)))) {

  • java – try-with-resource vs finally优先级2019-06-11 21:48:02

    try-with-resource构造的优先规则是什么?这是一个例子(注意:所有提到的类都实现了Closeable): try (Page closeablePage = page; PipedOutputStream out = outs; SequenceWriter sequenceWriter = mapper.writer().writeValuesAsArray(out)) { // do stuff } finally {

  • java – 具有可关闭参数的try-with-resources的行为2019-05-17 10:50:56

    Java-7的try-with-resources是否需要将closable直接分配给变量?简而言之,这段代码是…… try (final ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream(data))) { return ois.readObject(); } 相当于这个块?… try (fin

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有