What is your "favorite" API annoyance or missing feature or misengineered part?
I have often come across situations like :-
try{
...
stmts
...
}
catch(Exception ex) {
...
stmts
...
} finally {
connection.close // throws an exception
}
which still needs a try - catch block inside finally.
What is the best practice to overcome this?
Usually, when dealing with Java IO code, here is what I wrote
FileOutputStream out = null;
try
{
out = new FileOutputStream("myfile.txt");
// More and more code goes here...
}
catch (Exception e)
{
}
finally
{
// I put the close code in finally block, to enture the opened
// file stream is always closed even there is exceptio...
Alright, I have been doing the following (variable names have been changed):
FileInputStream fis = null;
try
{
fis = new FileInputStream(file);
... process ...
if (fis != null)
fis.close();
}
catch (IOException e)
{
... blah blah blah ...
}
Recently, I started using FindBugs, which suggests that I am not properly closing streams. I decide to see if there's anyth...
As most should know close() also closes any streams uses.
This allows the follow code:
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(...)));
...
br.close();
This is nice, since we don't need a reference to FileInputStream and remember to close it.
But does it also work for FileLocks?
final FileInputStream fis = new FileInputStream(new File("buffer.txt"))...
Below is my code that replaces the DataInputStream to wrap an InputStream, but provides extra methods to read little endian data types in addition to the normal methods that read big endian types. Feel free to use it if you'd like.
I have a few reservations as follows. Notice the methods that do not change functionality (the functions that read big endian types). There is no way I could implem...
OK, so it's easy to name an interface (or class for that matter) if you can easily think of a noun: User, Window, Database, Stream, etc.
What about an adjective or adjective concept? e.g. something that has a timestamp (HasTimestamp, Timestamped, Timestampable...?) or something that is tracked or watched (Trackable, IsTracked, Watchable, IsWatched...?)
I'm converting some C# code to Java and it contains the using statement. How should I replicate this functionality in Java? I was going to use a try, catch, finally block but I thought I'd check with you guys first.
I'm trying to delete a file that another thread within my program has previously worked with.
I'm unable to delete the file but I'm not sure how to figure out which thread may be using the file.
So how do I find out which thread is locking the file in java?
I'd like to have a FileWriter opened during the whole time a class instance exists. So I need to close it in a destructor. But how to specify a destructor in Scala?
Seeing a checked expection in API is not rare, one of the most well known examples is IOException in Closeable.close(). And often dealing with this exception really annoys me. Even more annoying example was in one of our projects. It consists from several components and each component declares specific checked exception. The problem (in my opinion) is that at design time it was not exactly know...
In C++ its possible to have a default stream like
class c
{
public:
c(istream fin =cin):fin(fin){}
}
Similary can I do this in java or is this wrong practice.Or is there a better way of doing this? I want to choose between reading from the console and reading from a file.
class c
{
c()
{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
}
c(int i)...
Could someone explain me some situation (example) when we can use this construction?
try{
//dangerous code here
} finally {
//always called
}
I really understand how it works but newer use in real situation.
Need to design an API method whihc take OutputStream as a parameter.
Is it a good practice to close the stream inside the API method or let the caller close it?
test(OutputStream os) {
os.close() //???
}
In the following program, loop is iterating 1000 times, and I am writing all the entries in a file using a FileWriter, but unfortunately programs ends up writing only 510(sometimes 415, sometimes 692, always less then 1000) entries in the file, but loop is iterating 1000 times.
import java.io.* ;
import java.util.*;
public class DemoWriter {
public static void main(String[] args) throws ...