This question already has answers here:
Why doesn't java.io.File have a close method?
(6 answers)
Closed 8 years ago.
After upgrading to Java 7 I get the following code flagged by Eclipse:
try (File file = new File(FILE_NAME)) {
file.delete();
}
Error is:
The resource type File does not implement java.lang.AutoCloseable
And Java's documentation doesn't have File listed in the AutoCloseable docs:
http://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html
So besides adding the catch block, what is the suggested alternative?
As Jeffrey said in the comment to the question, you need to differentiate between a File and an InputStream, e.g. FileInputStream.
There is nothing to close in a File, but there is something to close in a stream or a reader.
try (FileInputStream fs = new FileInputStream (new File(FILE_NAME))) {
// do what you want with the stream
}
Related
public static String getMD5Checksum(String filePath) throws Exception {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
return DigestUtils.md5Hex(fis);
}
In the above code fis is not closed but it is not thrown as error in SonarQube. The DigestUtils.md5Hex method does not close the stream too.
Both SonarQube and Sonar Java plugins are in latest versions.
You don't use the FileInputStream, but if you add the following line (or similar):
fis.read();
It will add the valid rule: Resources should be closed (squid:S2095)
Connections, streams, files, and other classes that implement the Closeable interface or its super-interface, AutoCloseable, needs to be closed after use.
But you have a point that we need to release resource as FileInputStream
Yes, you need to close the inputstream if you want your system resources released back.
You can raise this question in SonarSource community
EDIT
Resources should be closed not warns on new FileInputStream added as a possible sonarqube bug
This question already has answers here:
Close Java 8 Stream
(5 answers)
Closed 5 years ago.
I'm new and I have a question.
How can I close a file in java using functional programming(using lambda)?
How can I convert for example file.close() into something that looks in a functional manner?
The closest you can get to clean the 'close a resource' is try with resources (But thats not functional..):
try (BufferedReader br = new BufferedReader(new FileReader("myfile")){
} catch (IOException e) {
e.printStackTrace();
}
This question already has answers here:
ObjectInputStream available() method doesn't work as expected (Java)
(5 answers)
Closed 6 years ago.
First of all, I'm new to the site, and found it very helpful for having previous questions of mine that had already been answered, but I couldn't find anything about this specifically, so I hope this hasn't already been answered. This is for a game I'm making where RoadPanel.shopList is a static arraylist that holds all the shops in the map (and they are drawn and updated, etc. from here). shopIn has the same filepath as the file that is (correctly) written to while the game is running (I opened the file to make sure it was being written to properly). I don't know why it says that there are no available files to read from the InputStream, so I'm really stuck...
public static void loadGame() throws ClassNotFoundException, IOException
{
ObjectInputStream shopIn = new ObjectInputStream(new FileInputStream("src/save/shops.ser"));
System.out.println("Available: " + shopIn.available());
while(shopIn.available() > 0)
{
System.out.println("hit");
Shop s = (Shop)shopIn.readObject();
RoadPanel.shopList.add(s);
}
}
Thanks for your help,
pete.
Your code is invalid. available() isn't a test for end of stream. See the Javadoc. And it isn't implemented for some streams, including ObjectInputStream.
You should read until EOFException is thrown.
This question already has answers here:
Get file name from FileOutputStream
(5 answers)
Closed 6 years ago.
hi i have a FileInputStream which is pointing remote file in the server.
how to know the filename and its extension which is pointed by this stream.
if i want to write the remote file in my local computer, i have to know the file name and its extension that's what i want to know
There are no public methods that return the File, Path or String from FileInputStream.
The best workaround is to wrap FileInputStream, store File in the wrapper class and implement getFile() method.
try this:
if(file.isDirectory())
{
File f=file.listFiles();
for(File name : f)
pirnt(name.getName());
}
This question already has answers here:
How to append text to an existing file in Java?
(31 answers)
Closed 7 years ago.
I want to write particular string in a file after a string. My file has this already -
##############################
path :
I need to write the String /sdcard/Docs/MyData after path :
Could anyone tell me how I could achieve this?
If I understand correctly you mean to append your path at the end of your file.
If so the use of a FileWriter is a good way to do it.
new FileWriter("Your path", true)
Notice that the boolean true in this case indicates that you want to append to your file, removing this altogether or using false instead would mean you want to overwrite the file.
An example for your case:
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("C:/YourEpicPath/ThisFileNeedsSomeAppending.txt", true)))) {
out.println("/sdcard/Docs/MyData");
}catch (IOException e1) {
//exception handling left as an exercise for the reader
}
Here is some documentation if you need for android, normally there shouldn't be any big differences.