Closing a file in java, using functional programming [duplicate] - java

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();
}

Related

Regarding java's try/catch syntax; are these blocks equivalent? [duplicate]

This question already has answers here:
Why should I not wrap every block in "try"-"catch"?
(16 answers)
Closed 5 years ago.
In the following block:
try (final InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath()));
final ArchiveInputStream ais = factory.createArchiveInputStream(fn, fis)) {
System.out.println("Created " + ais.toString());
ArchiveEntry ae;
while ((ae = ais.getNextEntry()) != null) {
System.out.println(ae.getName());
}
}
is this equivalent to the following block:
try {
final InputStream fis = new BufferedInputStream(Files.newInputS...;
} catch {
System.out.println("Created " + ais.toString());...
}
I stumbled across this syntax for try/catch in an apache common's library, and I'm not really sure how to take it. If I'm not correct in the only assumption that I can think of here, can anybody help me understand it or point me to a reference that explains this syntax? I've googled and searched aplenty on here and haven't been able to find anything applicable, though admittedly my search-fu is not the best.
Thanks in advance!
No. The first is try with resources, which isn't an intuitive name. It's used when you need to use a resource and then close it. It saves you from having to manually close those every time and effectively limits the scope of the resources to within the curly braces.
The latter one is not the same: the resource fis will still be open after the try/catch block has exited. try-with-resources was introduced to fix this issue.

Writing string to file after particular string [duplicate]

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.

set execute permition on file [duplicate]

This question already has answers here:
How do I programmatically change file permissions?
(12 answers)
How to change image permission mode to 777 using Java code?
(5 answers)
Closed 7 years ago.
I need to set execute permition on Linux script file using java 1.4. I would prefer to use java native library. If it is not possible with native library what is lightweight lib that allows to do it?
JAVA5 and prior there is workaround using exec extracted from here:
public static void runCmd (String[] cmd) {
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader r = new BufferedReader(
new InputStreamReader (
p.getInputStream()
)
);
} catch(Exception e) {
}
}
USE
runCmd(new String[] {
"/bin/chmod",
"755",
"/path/to/your/script"
});
By terminal in Linux
UP TO JAVA6
For permissions use File::setExecutable(boolean [, boolean])
File file = new File("/your/path/to/file/file_script.sh");
file.setExecutable(true);
Check this MKYONG permissions tutorial

Why does java.io.File not implement Autocloseable? [duplicate]

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
}

Add extension to File [duplicate]

This question already has answers here:
Rename a file using Java
(15 answers)
Closed 8 years ago.
I have a File named myfile without extension. I want to add an extension to it so it will be myfile.ext. I don't want to open it and save it with an extension. Just "rename" the File class instance.
How can I do it?
P.S. If that matters, I need this because I want to use this:
File myfiletopeon=myfile;
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().open(myfiletopeon);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null,"Problem","Problem",JOptionPane.ERROR_MESSAGE);
}
}
in order to open the file and I need an extension so that the file can be opened
How about using File.renameTo() method?

Categories