Java: what file extension should I use when writing objects with ObjectOutputStream - java

The API shows .tmp, my text book uses .dat and I've seen .ser
Does it matter?
I'm writing an arraylist of objects

Extensions doesn't matter. You could also use your name nicolas as extension too. Extensions are for OS to associate files to particular program.

You can use any custom extensions. Extensions are meant to identify default program for the file. For example, abc.txt file should be opened with text programs. You can change extension of your video file to .txt and your computer will try to open it with text processing program. Hence, you can provide any extension unless you want to open file by particular program.

Many people use .ser. You could use .bin, anything you like really, except the ones that indicate text: .txt, .doc, etc. It's binary, not text.

Related

Are there any differences between plain text files exported from Google Docs and plain text files written in Notepad?

I'm writing an application that is able to import data from a plain text file line-by-line in Kotlin so long as the first line contains a certain string, and through trying to make it work I've run into an issue: the text files I generate through the application or type in Notepad will import fine so long as they meet the criteria set in my code, but an identical text file exported from Google Docs will not.
Here's an example. Say I write up a file in Notepad called locationstoimport.txt in Notepad containing the following:
STRING_TO_MATCH
ADDRESS_1
ADDRESS_2
ADDRESS_3
ADDRESS_4
ADDRESS_5
When I try to import this into my application, I have no issues. The string in the first line, in this case STRING_TO_MATCH, is accepted by the application, and so the five addresses listed are imported.
Now, say I either (A) copy and paste the text from locationstoimport.txt into a Google Doc or (B) open locationstoimport.txt in Google Docs on my phone, then export it as a new text file called googledocslocationstoimport.txt, which contains the following:
STRING_TO_MATCH
ADDRESS_1
ADDRESS_2
ADDRESS_3
ADDRESS_4
ADDRESS_5
When I try to import this into my application, the application rejects it, saying the first line does not match. In theory it should since the data contained in both files is exactly the same and both files are plain text files, but my application doesn't seem to think so.
Is there some difference between plain text files written in Notepad, generated by Android applications, and exported by Google Docs that I should be aware of? Worth noting: when I export the first file to Google Docs and save it again as a .txt file, it's 5 bytes larger than the original. I don't know if that is affecting what is going on, or if it should be.
Thanks!

How to read in a file extension and the perform a certain action, Java

so I have made a simple text editor built in Java; however, I need to know how to be able to recognize a certain file extension and then perform a certain action. To be frank, I have two files containing java and python keywords, the user should be able to save the file as .java or .py or open a .java or .py file and the keywords should be a different color from the rest of the text. I am confused as to how to read these extensions in.
Seems that the simple case is something like filename.endsWith(".java"); to check if the filename has a .java extension. There may be other ways to discover a file type by mimetype, but I am afraid I do not remember that at the moment.
Edit: Maybe you should be careful to convert the string to lowercase before, to make it possible to recognize file extension even if it has a different case, no matter if your system has a case sensitive filesystem or not
Edit2: Actually I found that you can use java7 features to detect file type. If you have a java.nio.file.Path object, you can do:
String type = Files.probeContentType(filepath);
The type returned is the mimetype, for example text/x-java for java source code. Not sure how good it is, and not sure how it recognizes things, but I have tested that it works on example java and python source files.

Validation of files based on their file extensions

I get files from queues in Java. They may be of following formats.
docx
pdf
doc
xls
xlsx
txt
rtf
After reading their extensions, I want to validate whether they are actually files of these types.
For example, I got a file and checked that it has extension .xls. Afterwards, I want to check whether it is actually an .xls file or someone uploaded file of some other format after changing its extension.
EDIT: I'd like to check the file's MIME type by actually checking its content, not its extension. How it can be done?
I don't think this is a problem you should be solving. Any solution to this problem would be brittle and based upon your current understand of what constitutes a valid file of a particular type.
For example, take a XLS file. Do you know for sure what Excel accepts when opening such a file? Can you be sure you'll keep abreast of any changes in future releases that might support a different encoding style?
Ask yourself - what's the worse that could happen if the user uploads a file of the wrong type? Perhaps you'll pass the file to the application that handles that file extension and you'll get an error? Not a problem, just pass that to the user!
Without using external libraries:
You can get the file mimetype using MimetypesFileTypeMap:
File f = new File(...);
System.out.println(new MimetypesFileTypeMap().getContentType(f));
You can get a similar result with:
URLConnection.guessContentTypeFromName
Both these solutions, according to the documentation, look only at the extension.
A better option: URLConnection.guessContentTypeFromStream
File f= new File(...);
System.out.println(URLConnection.guessContentTypeFromStream(new FileInputStream(f)));
This try to guess from the first bytes of the file - be warned this is only a guess - I found it works in most cases, but fails to detect some obvious types.
I recommend a combination of both.

How to know file type without extension

While trying to come-up with a servlet based application to read files and manipulate them (image type conversion) here is a question that came up to me:
Is it possible to inspect a file content and know the filetype?
Is there a standard that specifies that each file MUST provide some type of marker in their content so that the application will not have to rely on the file extension constraints?
Consider an application scenario:
I am creating an application that will be able to convert different file formats to a set of output formats. Say user uploads an PDF, my application can suggest that the possible conversion formats are microsoft word or TIFF or JPEG etc.
As my application will gradually support different file formats (over a period of time), I want my application to inspect the input file instead of having the user to specify the format. And suggest to user the possible formats of output.
I understand this is an open ended, broad question. Please let me know if it needs to be modified.
Thanks,
Ayusman
Yeap you can figure out the type without an extension using the magic number.
Also, the way the file command figures it out, is actually through a 3 step check:
Check for filesystem properties to identifie empty files, folders, etc...
The said magic number
In text files, check for language in it
Here's a library that'll help you with Magic Numbers: jmimemagic

creating own file extension [duplicate]

This question already has answers here:
How to create my own file extension like .odt or .doc? [closed]
(3 answers)
Closed 8 years ago.
I'm on my way in developing a desktop application using netbeans(Java Dextop Application) and I need to implement my own file format which is specific to that application only. I'm quite uncertain as to how should I go about first.What code should I use so that my java application read that file and open it in a way as I want it to be.
If it's character data, use Reader/Writer. If it's binary data, use InputStream/OutputStream. That's it. They are available in several flavors, like BufferdReader which eases reading a text file line by line and so on.
They're part of the Java IO API. Start learning it here: Java IO tutorial.
By the way, Java at its own really doesn't care about the file extension or format. It's the code logic which you need to write to handle each character or byte of the file according to some file format specification (which you in turn have to writeup first if you'd like to invent one yourself).
I am not sure this directly addresses your question, but since you mentioned a custom file format, it is worth noting that applications launched using Java Web Start can declare a file association. If the user double clicks one of those file types, the file name will be passed to the main(String[]) of the app.
This ability is used in the File Service demo. of the JNLP API - available at my site.
As to the exact format of the file & the best ways to load and save it, there are a large number of possibilities that can be narrowed down with more details of the information it contains.
Choosing a new/existing file extension does not affect your application (or in any case anyone's). It is upto the programmer what files he wants his app to read.
For example, you may consider you can't read a pdf or doc directly as a text file....but that is not because they are written/ stored differently, but because they have headers or characters which your app does not understand. So we might use a plugin or extension which understands those added headers ( or rather the grammar of the pdf /doc file) removes them & lets our app know what text (or anything else) it contains.
So if you wish to incorporate your own extension, & specifically want no other application to be able to read it, just write the text in a way that only your program is able to understand. Though writing a file in binary pretty much ensures that your file is not read directly just by user opening a file, but it is however still possible to read from it, if it is merely collection of raw characters.
If you ask code for hiding a data, I'd say there are plenty of algorithms you might use, which usually get tagged as encryptions cause you are basically trying to lock/hide your stuff. So if you do not really care for the big hulla-bulla, simply trying to keep a file from being directly read & successful attempts to read the file does not cause any harm to your application, write it in binary.

Categories