简体   繁体   中英

How to write a Java program to open a specific file extension (.pef)?

I need to write a java program to open a specific file extension (This is to open a .pef file). This java program is being written to open this .pef file and print it. But I don't know how to write a program that can open the .pef file extension by default.

This solution is Windows specific

In this post I am assuming that your program is already capable of reading files of type .pef and printing them. If not then please refer to Treebranch's and Himanshu's posts here to figure out how to do so. Next I will assume that your program is to be invoked on the Windows command line as:

program.exe <filename.pef>

To programatically change the association, follow these steps:

Define a file type

ftype peffile="program.exe" "%1"

Remember to use the absolute path of program.exe here.

Associate this file type with .pef

assoc .pef=peffile

See:

  • Desktop.open(File) Launches the associated application to open the file.
  • Desktop.edit(File) Launches the associated editor application and opens a file for editing.
  • Desktop.print(File) Prints a file with the native desktop printing facility, using the associated application's print command.

This presumes the application has already been associated with the file-type. To create the file association, launch your app. using Java Web Start & declare the file type in the launch file.

I am not familiar with this file type, but I am guessing you are going to have to read the bytes of the file and then go from there. You should be able to use a FileInputStream . Try looking at this tutorial .

You can use FileInputStream to read a file.

FileInputStream fr=new FileInputStream("xyz.pef");
int i=0;
while((i=fr.read())!=-1)
   System.out.print((char)i);
fr.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM