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?
Related
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.
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
}
This question already has answers here:
How do I read a resource file from a Java jar file?
(9 answers)
Closed 8 years ago.
OK guys, so I'm trying to compile my game into a jar file, but I can't get the loading of images to work. When run from NetBeans, all is fine. But in the JAR, the URL is always null.
Here is the code I'm using:
URL url = this.getClass().getResource("/textures/Lava.jpg");
BufferedImage sourceImage = null;
try
{
sourceImage = ImageIO.read(url);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
I have tried unziping the JAR and checking the contents, my textures folder is there and the images inside also. Any ideas what I'm doing wrong?
Previously answered here:
Accessing a file inside a .jar file
Better use
new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resources/" + filename)))
you should get the URL path as relative path of your system.
This question already has answers here:
How to create a folder in Java?
(8 answers)
Closed 3 years ago.
I tried to use the File class to create an empty file in a directory like "C:/Temp/Emptyfile".
However, when I do that, it shows me an error : "already made folder Temp". Otherwise, it won't create one for me.
So, how do I literally create folders with java API?
Looks file you use the .mkdirs() method on a File object: http://www.roseindia.net/java/beginners/java-create-directory.shtml
// Create a directory; all non-existent ancestor directories are
// automatically created
success = (new File("../potentially/long/pathname/without/all/dirs")).mkdirs();
if (!success) {
// Directory creation failed
}
You can create folder using the following Java code:
File dir = new File("nameoffolder");
dir.mkdir();
By executing above you will have folder 'nameoffolder' in current folder.