package com.codeski.nbt;

import java.awt.GraphicsEnvironment;
import java.io.Console;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.codeski.nbt.tags.NBTCompound;

public class Main
{
	public static void main(String[] args) throws FileNotFoundException, IOException
	{
		Console console = System.console();
		if (console != null)
			if (args.length == 2) {
				// Read in a user specified file...
				File in = new File(args[0]);
				System.out.println("Reading " + in.getCanonicalPath() + "...");
				NBTCompound root = new NBTReader(in).read();
				// Write the file to NBT, JSON, or XML...
				File out = new File(args[1]);
				String extension = args[1].substring(args[1].lastIndexOf('.') + 1);
				System.out.println("Writing " + out.getCanonicalPath() + "...");
				NBTWriter nw = new NBTWriter(out);
				if (extension.equalsIgnoreCase("dat"))
					nw.writeNBT(root);
				else if (extension.equalsIgnoreCase("json"))
					nw.writeJSON(root);
				else if (extension.equalsIgnoreCase("xml"))
					nw.writeXML(root);
				else
					System.out.println("Cannot write file... Supported file extensions are dat, json, and xml.");
			} else
				System.out.println("Usage: java -jar " + new File(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getName() + " <file-to-read> <file-to-write>");
		else if (!GraphicsEnvironment.isHeadless()) {
			JFileChooser fc = new JFileChooser();
			FileFilter nbt = new FileNameExtensionFilter("Named Binary Tag (.dat)", "dat");
			fc.addChoosableFileFilter(nbt);
			int ret = fc.showOpenDialog(null);
			if (ret == JFileChooser.APPROVE_OPTION) {
				File in = fc.getSelectedFile();
				NBTCompound root = new NBTReader(in).read();
				FileFilter json = new FileNameExtensionFilter("JavaScript Object Notation (.json)", "json");
				fc.addChoosableFileFilter(json);
				FileFilter xml = new FileNameExtensionFilter("Extensible Markup Language (.xml)", "xml");
				fc.addChoosableFileFilter(xml);
				fc.setFileFilter(fc.getAcceptAllFileFilter());
				ret = fc.showSaveDialog(null);
				if (ret == JFileChooser.APPROVE_OPTION) {
					File out = fc.getSelectedFile();
					NBTWriter nw = new NBTWriter(out);
					if (fc.getFileFilter().equals(nbt))
						nw.writeNBT(root);
					else if (fc.getFileFilter().equals(json))
						nw.writeJSON(root);
					else if (fc.getFileFilter().equals(xml))
						nw.writeXML(root);
					else {
						String ext = out.getName().substring(out.getName().lastIndexOf('.') + 1, out.getName().length());
						if (ext.equalsIgnoreCase("dat"))
							nw.writeNBT(root);
						else if (ext.equalsIgnoreCase("json"))
							nw.writeJSON(root);
						else if (ext.equalsIgnoreCase("xml"))
							nw.writeXML(root);
						else
							nw.writeNBT(root); // Default case...
					}
				}
			}
		}
	}
}

@@ -1203,24 +1203,26 @@
 t);%0A%09%09%09%09else
+ %7B
 %0A%09%09%09%09%09System
@@ -1239,40 +1239,9 @@
 ln(%22
-Cannot write file... Supported f
+F
 ile 
@@ -1253,13 +1253,16 @@
 sion
-s are
+ was not
  dat
@@ -1273,19 +1273,72 @@
 on, 
-and
+or
  xml.
-%22);
+ Writing NBT by default.%22);%0A%09%09%09%09%09nw.writeNBT(root);%0A%09%09%09%09%7D
 %0A%09%09%09

Make default file type for writing file in CLI