try-with-resources

Break doesn't work in try with resources, but work in try without resources

别来无恙 提交于 2019-12-12 04:12:42
问题 Break doesn't work in try with resources, but work in try without resources! This is a simple example for this situation. I caught this "bug" in work project. When I use try without resources try { Resources resources = getResources() // some code here, look below } I have only one iteration of my cycle, and it's right because I have the condition "if true then break", but when I changed try without recourse on try WITH resources. try (Resources resources = getResources()) { // some code here

Is this file object left open?

非 Y 不嫁゛ 提交于 2019-12-12 03:54:17
问题 As an exercise for school I wrote a method in Java that searches for a character in a file. Here is the code: public static void countLetter(char needle, String hayStack) throws IOException { File f = new File(hayStack); try (Scanner in = new Scanner(f)) { String str = null; while (in.hasNext()){ str += in.next(); } char[] charArr = str.toCharArray(); int counter = 0; for (char c: charArr) { if (c == needle){ counter++; } } System.out.println(counter); } } This does what I need it to but I

Try-with-resources closes sockets of spawned childs

淺唱寂寞╮ 提交于 2019-12-11 11:12:29
问题 I want to write a simple server that listens on a port and spawns new threads for handling new connections. I attempted to use try-with-resources for accepting new connections but failed because sockets in child threads seem to be closed immediately and I don't understand why. Here are 2 simplified examples. a) The working example of the server (without try-with-resources): package MyTest; import java.io.BufferedReader; import java.io.IOException; import java.io.OutputStreamWriter; import

Java Try With Resources Does Not Work For Assignment?

自古美人都是妖i 提交于 2019-12-11 02:24:45
问题 Alright, so I was just writing a quick class and I tried to use the try with resources instead of the try-catch-finally (hate doing that) method and I keep getting the error "Illegal start of type". I then turned to The Java Tutorials section on it: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html and it showed that you can assign a new variable in the parenthesis. I'm not sure what is going on. private static final class EncryptedWriter { private final Path

Why is BufferedReader not closed when obtaining `Stream<String>` in try-with-resources?

牧云@^-^@ 提交于 2019-12-10 21:27:43
问题 The reader should be closed when a Stream is used in a try-with-resources. Given this: try(Stream<String> lines = new BufferedReader(reader).lines()) { return lines.map(it -> trim ? it.trim() : it) .collect(Collectors.toList()); } ... the reader is not being closed?? This test fails: AtomicBoolean closed = new AtomicBoolean(false); Reader r = new StringReader(" Line1 \n Line2") { @Override public void close() { super.close(); closed.set(true); } }; try(Stream<String> lines = new

Dead code warning in try-with-resources, but not in translated try-catch-finally

拥有回忆 提交于 2019-12-10 15:34:19
问题 The following code uses the try-with-resources construction introduced in Java 8. The occasionallyThrow() method is declared to throw an OccasionalException , the Resource 's close() method to throw a CloseException . Eclipse (Version: Neon Release (4.6.0), Build id: 20160613-1800) adds a warning on the line marked with // dead code that the branch is dead code. Implicitly, Eclipse affirms that the line marked with // alive code is not dead code. Object tryWithResources() throws

java try-with-resource not working with scala

我只是一个虾纸丫 提交于 2019-12-09 14:51:46
问题 In Scala application, am trying to read lines from a file using java nio try-with-resource construct. Scala version 2.11.8 Java version 1.8 try(Stream<String> stream = Files.lines(Paths.get("somefile.txt"))){ stream.forEach(System.out::println); // will do business process here }catch (IOException e) { e.printStackTrace(); // will handle failure case here } But the compiler throws error like ◾not found: value stream ◾A try without a catch or finally is equivalent to putting its body in a

Why close method of java.lang.AutoCloseable throws Exception, but close method of java.io.Closeable throws IOException?

房东的猫 提交于 2019-12-08 18:45:04
问题 I was reading this link for try-with-resources and it says: The close method of the Closeable interface throws exceptions of type IOException while the close method of the AutoCloseable interface throws exceptions of type Exception . But why? The close method of AutoCloseable could have also thrown IOException is there any example that support that close method of AutoCloseable must throw exceptions of type Exception 回答1: The AutoClosable interface is located in java.lang and is intended to

Java using scanner with try-with-resources

こ雲淡風輕ζ 提交于 2019-12-07 16:00:41
问题 I have two versions of Java code that gets user input until user types "q" Version 1: public class Test { public static void main(String[] args) { String input = ""; while (!input.equals("q")) { Scanner scanner = new Scanner(System.in); System.out.print("Input: "); input = scanner.nextLine(); System.out.println("Input was: " + input); } } } Version 2: public class Test { public static void main(String[] args) { String input = ""; while (!input.equals("q")) { try(Scanner scanner = new Scanner

Why not using a try with lock in java?

心已入冬 提交于 2019-12-07 05:47:45
问题 I've read this topic, and this blog article about try with resources locks, as the question popped in my head. But actually, what I'd rather like would be a try with lock , I mean without lock instantiation. It would release us from the verbose lock.lock(); try { //Do some synchronized actions throwing Exception } finally { //unlock even if Exception is thrown lock.unlock(); } Would rather look like : ? implements Unlockable lock ; ... try(lock) //implicitly calls lock.lock() { //Do some