How to iterate over the files of a certain directory, in Java? [duplicate]

If you have the directory name in myDirectoryPath, import java.io.File; … File dir = new File(myDirectoryPath); File[] directoryListing = dir.listFiles(); if (directoryListing != null) { for (File child : directoryListing) { // Do something with child } } else { // Handle the case where dir is not really a directory. // Checking dir.isDirectory() above … Read more

How can I catch all the exceptions that will be thrown through reading and writing a file?

If you want, you can add throws clauses to your methods. Then you don’t have to catch checked methods right away. That way, you can catch the exceptions later (perhaps at the same time as other exceptions). The code looks like: public void someMethode() throws SomeCheckedException { // code } Then later you can deal … Read more

Download File Using jQuery

I might suggest this, as a more gracefully degrading solution, using preventDefault: $(‘a’).click(function(e) { e.preventDefault(); //stop the browser from following window.location.href=”https://stackoverflow.com/questions/1296085/uploads/file.doc”; }); <a href=”no-script.html”>Download now!</a> Even if there’s no Javascript, at least this way the user will get some feedback.

In C# check that filename is *possibly* valid (not that it exists) [duplicate]

Just do; System.IO.FileInfo fi = null; try { fi = new System.IO.FileInfo(fileName); } catch (ArgumentException) { } catch (System.IO.PathTooLongException) { } catch (NotSupportedException) { } if (ReferenceEquals(fi, null)) { // file name is not valid } else { // file name is valid… May check for existence by calling fi.Exists. } For creating a FileInfo … Read more

How to get absolute path to file in /resources folder of your project

The proper way that actually works: URL resource = YourClass.class.getResource(“abc”); Paths.get(resource.toURI()).toFile(); It doesn’t matter now where the file in the classpath physically is, it will be found as long as the resource is actually a file and not a JAR entry. (The seemingly obvious new File(resource.getPath()) doesn’t work for all paths! The path is still … Read more

Replace input type=file by an image

This works really well for me: .image-upload>input { display: none; } <div class=”image-upload”> <label for=”file-input”> <img src=”https://icons.iconarchive.com/icons/dtafalonso/android-lollipop/128/Downloads-icon.png”/> </label> <input id=”file-input” type=”file” /> </div> Basically the for attribute of the label makes it so that clicking the label is the same as clicking the specified input. Also, the display property set to none makes it so … Read more

Write variable to a file in Ansible

An important comment from tmoschou: As of Ansible 2.10, The documentation for ansible.builtin.copy says: If you need variable interpolation in copied files, use the ansible.builtin.template module. Using a variable in the content field will result in unpredictable output. For more details see this and an explanation Original answer: You could use the copy module, with … Read more