How to create my own file extension like .odt or .doc? [closed]

不问归期 提交于 2019-11-29 05:22:03
Crippledsmurf

A file extension is just the portion of the file name after the last period.

For example in the path:

C:\Users\Tests\My Documents\file.txt

The file extension is .txt which typically indicates that the file contains text data. To create your own file extension all you need to do is to place the desired extension after the last period in the filename.

In Java you can create a file using an object of type File like this:

File file = new File("file.txt")

The file will be created in the current working directory and will have the extension txt because this is the value after the final period in the file name.

A file format refers to the layout of data inside a file. Creating a custom file format involves thinking about how you want to store your data within the file, and writing it to the file in a way which matches that layout.

For example if I had an address book application I might decide to store peoples names and phone numbers, separated by tabs and save this data in a file with extension address

My AddressBook.Save() function might look something like this Java code. It should be noted that I haven't programmed in Java for a number of years and mistakes are likely.

void Save(File file)
{
 FileWriter writer = new FileWriter(file);

foreach (AddressBookEntry entry in this.entries)
{
this.SaveEntry(entry,writer);
}
} 


void SaveEntry(AddressBookEntry entry,  FileWriter writer)
{
  String record = entry.getFirstName() + "\t" + entry.getLastName() + "\t" +
  entry.getPhoneNumber();
  writer.write(record, 0, record.length();
}

If we had an address entry like this:

First Name:Test
Last Name: Bob
Phone Number=555-1212

Then the entry would appear in the .address file as follows

Test Bob 555-1212

I hope that's helped explain the difference between a file extension and a file format and has gone some way to showing you how to create your own format, with a custom extension.

This is not Java related. Pick an extension that no one else is using and when you write the file using Java, just append the extension to the filename.

If would suggest following someone else's format like .odt or even HTML for something like this. If you are serious about creating your own format then you should take a look through java's inbuilt xml libraries and make a format using xml, http://docs.oracle.com/javase/7/docs/api/ is a good place to start then browse to the javax.xml.* packages.
Like other's have said if you just want to change the file extension, just put you extension at the end of the file path.
Getting the OS to associate your program with that extension is a lot more complicated though and not really a java issue, it is also platform dependent.

Andrew Thompson

File extensions can be declared using Java Web Start:

Java Web Start (JWS) is the Oracle Corporation technology used to launch rich client (Swing, AWT, SWT) desktop applications directly from a network or internet link. It offers 'one click' installation for platforms that support Java.

JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..

This demo. of the file services declares an interest in the .zzz file extension.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!