public static void printFile(String filename,String printerindx){
            FileInputStream psStream=null;
            int Printerinx=Integer.parseInt(printerindx);
            try {
               psStream = new FileInputStream(filename);
            } catch (FileNotFoundException ffne) {
                ffne.printStackTrace();
            } 
            if (psStream == null) {
                return;
            }   
            DocFlavor psInFormat = null;

            int index=filename.lastIndexOf(".");
            String extension=filename.substring(index+1);

            if(extension.equals("txt"))//||extension.equals("log")||extension.equals("xml")||extension.equals("htm")||extension.equals("html"))
            psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;

            else if(extension.equals("jpg"))
                psInFormat = DocFlavor.INPUT_STREAM.JPEG;

            else if(extension.equals("png"))
                psInFormat = DocFlavor.INPUT_STREAM.PNG;

            else if(extension.equals("gif"))
                psInFormat = DocFlavor.INPUT_STREAM.GIF; 


            Doc myDoc = new SimpleDoc(psStream, psInFormat, null);  
            PrintRequestAttributeSet aset = 
                new HashPrintRequestAttributeSet();
            aset.add(new Copies(1));
            aset.add(MediaSizeName.ISO_A4); 

            //aset.add(Sides.DUPLEX);
            aset.add(OrientationRequested.PORTRAIT);
            PrintService[] services = 
            PrintServiceLookup.lookupPrintServices(psInFormat, null);
            System.out.println("Printer Selected "+services[Printerinx]);   

            //PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();

            DocFlavor[] docFalvor = services[Printerinx].getSupportedDocFlavors();
            for (int i = 0; i < docFalvor.length; i++) {
                System.out.println(docFalvor[i].getMimeType());
            }   
            if (services.length > 0) {
               DocPrintJob job = services[Printerinx].createPrintJob();
            try 
               {
                job.print(myDoc, aset);     
                  System.out.print("Printing Doc");    
               } catch (PrintException pe)
               {           
                   System.out.print(pe);          
               }
            }
            }