How to find file extension if the file has been renamed? - java

How to find file extension if the file has been renamed? Is there any tool are available for this?
Example: I have a file "1.doc"; I hope everybody know this is a Word document I just renamed as "1.txt". But the file is a Word document originally; how can I get the original file extension?

You cannot get the previous file extension back. All you can do is look at the contents of the file, and try to guess what it is.
Most (many?) file formats begin with a "magic" value that is intended to identify the file format, but this method can never be 100% accurate.
As #AaronDigulla mentionned in a comment, there are libraries out there that help you in the task of "guessing", and the answer(s) to that question contain pointers to some such libraries.

Once you rename the file, the previous name is not preserved.
As far as automatically guessing the format of the file from its contents, take a look at the Unix file utility. The version that's installed on my Ubuntu box lists Microsoft Word among the "magic" signatures that it knows about.

What you want to do is find out what its MIME type is. You can use this tutorial http://www.roseindia.net/java/java-get-example/get-mime-type.shtml

before renaming it save it in a separate string called originalName...because when you change the name of the string, the memory allocated is changed and theres is no go back or undo

Consider using javax.activation.FileDataSource from the JavaBeans Activation Framework. In particular use the getContentType() method.

Related

How to use file associations with jpackage?

I am using jpackage to pack my Java application and want to use it to create file associations. I see that the utility supports this via the --file-associations command. Using this, you can point it to a file that includes file association information.
Does anyone know of documentation that gives a better description of how to create this file-association file? The help documentation in the utility itself describes keys that must appear in it, but there's no hint at how it needs to be formatted.
Here is what the packager lists in its own help:
--file-associations — Path to a Properties file that contains list of key, value pairs (absolute path or relative to the current directory). The keys "extension", "mime-type", "icon", and "description" can be used to describe the association. This option can be used multiple times.
Does anyone here know where I might be able to find an example of this that is written correctly or more detailed documentation on exactly how the feature is used? Would be much appreciated if someone could just point me in the right direction.
You need to write each key-value on each line separated by an equal sign without any quote and save as a text file with .properties extension for example
extension=<Your file extension without leading dot e.g. docx>
mime-type=<Your mimetype e.g. application/msword>
icon=<Path to the icon file e.g. word.ico (Windows) or word.icns (macOS)>
description=<Some description e.g. Microsoft Word Open XML Format Document>
I could not find any official documents either. However, this is how I created my properties file which works on the release version of Java 14.

Getting the File Signature of a File through Lotus Script

Is there a way to get the file signature of a file inside a document? I tried checking the Embedded Object Class but it seems that there is no function for getting the file signature. Is there a way to get it just by using lotus script?
If not then I believe I'll need to maybe use a java class agent right? Can you provide a link that is doing this function or maybe can you guide me with some codes for this one. I am familiar with java but when it is being used in lotus notes agents I am not much familiar with the class being used.
Basically what I need to do is check the files in the documents and check if they are a valid file with the valid signature. Just checking the extension name is not enough as it might be renamed but the signature of the file is not valid so I'll need to confirm the file signature in checking. Thanks.
You have to write the file to file system and then you can read the file from there. Use a temp directory and delete every file after usage.
Look here for code to write attachments to file system as a starting point. Property EmbeddedObjects is available for documents too in case you want to analyse all attachments of a document.
You can accomplish the same in Java. Just look for Java classes in Language cross-reference in documentation.

Creating a file just to be uploaded into the online form

couldn't find relevant question on SO I'm asking a new one. I can create a file using FileWriter class, but it requires to specify the path for that file (physically creates the file). What I want to achieve is to create a file like in-memory, without specyfying the path or saving it on the disk and then upload it into the online form with selenium webdriver, is that somehow possible?
But there also is another problem, html fileUpload element will accept the path such as:
driver.findElement(By.id("Content_CV")).sendKeys("C:\\Users\\name\\Documents\\my_cv.pdf");
but will it accept the file itself? Probably not, so assuming that some of you knows how to create a file without saving it on the disk, would there be a way of providing path to that file anyway (given its virtual location)?
I'm trying to figure it out and I did some google research, yet here I am. Thanks for any attempt of help :)
If your question is just how to create a temporary file without caring about how to name it and where to create it without overwriting existing files, then you can simply use File.createTempFile. This will create an actual new temporary file on disk in the directory designated by the operating system for that purpose. You might also want to have a look at File.deleteOnExit.
If you are not looking for a java solution, you can simply create the file in a RAM drive
Software for this exist for all mayor operating systems.
Have a look at the wikipedia link above for more information.

Where to put text files in directory in Android

I have a text file i want to include in my Android application, it is not a string file it is a standard text file. It contains data that defines the characteristics of a "map" that is drawn on a board. The file is not an XML file so i am unsure where i should put it or if this isn't good file structure for android? Are you suppose to do this? If you are then under what directory are you suppose to put them? How then are you suppose to access the file? I know how to use FileInputStreams and FileOutputStreams i just need to know how to access the file. All relevant answers are welcome and appreciated!
Use assets or raw folder in your android folders structure to keep that file. For more info read this
You have to put your file in the assets folder as Waqas said.
Now to access it you do it like that.
I give you an example using BufferedReader
BufferedReader reader = new BufferedReader(
new InputStreamReader(getAssets().open("YourTextFile.txt")));
Be careful. In my case, I don't know why for now, I cannot read text files bigger than ~1MB and I had to split them in multiple small files. It seems other had the same problem of file size but I didn't find any information about that on Android developer site. If any one knows more about this ....
FOLLOW UP
My problem with the 1MB was due to a know bug/limitation of earlier versions of Android. Since using recent versions of Android, that problem is not present anymore.
I would just like to add to the accepted answer (I don't have enough reputation to comment unfortunately.) The link there to the tutorial that explains how to set up the res/raw method or the assets method is mostly good, but there's actually a MUCH easier way. Look at the function described there called LoadFile. That function is rather verbose. Lets say all you need is an InputStream variable so that you can read and write to a file. Then delete everything after line 77! Also you don't need the resource id at all! You can use this function:
//get the file as a stream
iS = resources.openRawResource(R.raw.mygpslog)
Now all you have to do is return iS and you will have your much desired file handle.
For reference, the tutorial is right here -> http://www.41post.com/3985/programming/android-loading-files-from-the-assets-and-raw-folders

Copying Files with Spaces

I'm trying to copy one file to another directory, but the way I am copying the file is not working when the source path has a directory with spaces in it, e.g.
/Volumes/public/Music/Directory With Spaces/01.mp3
I am using: http://commons.apache.org/io/
I can't find a way around this, any ideas?
Edit: The problem should probably be putting paths with spaces into a java.io.File object.
If you are using version 1.1, then you should be able to use '%20' to refer to a space.
Source: http://commons.apache.org/io/upgradeto1_1.html
If you create a java.io.File object with the directory you stated, does it find it? Does it find the file (i.e. file.exists() returns true)? My thought is that you need to encode it in a File object, or a URI/URL object. However, I am not intimately familiar with the Apache IO libraries, as I tend to use the standard ones in the Java releases.
If the path works with the standard Java IO libraries, then that would point to some different handling with the Apache IO libraries. If it doesn't, I would attempt to get it working with those first, and then use a File object to get it working fully.
Try it with escaped spaces:
/Volumes/public/Music/Directory\ With\ Spaces/01.mp3

Categories

Resources