Impression de fichier .txt

Résolu
cs_redbar Messages postés 112 Date d'inscription mardi 26 janvier 2010 Statut Membre Dernière intervention 26 mai 2014 - 28 mars 2014 à 16:47
cs_redbar Messages postés 112 Date d'inscription mardi 26 janvier 2010 Statut Membre Dernière intervention 26 mai 2014 - 15 avril 2014 à 11:45
Bonjour à tous,

Je développe en ce moment même une application qui permettra d'imprimer des ticket de caisse.

J'arrive à faire mon état parfaitement qui est un fichier ".txt" mais mon problème est que l'imprimante ne vois pas le fichier lorsque je lance l'impression.

J'ai fouillé sur internet et j'ai eu un code que j'utilise avec quelque modification mais ce code me retourne une erreur " Fichier introuvable" et pourtant le fichier existe parfaitement sur le disque. Je souhaiterais que vous m'aidiez à pouvoir réussie l'impression.

voici le code


public static void imprimerTicket(String nomFichier){
//creation de l'objet FileInputStream

ObjectInputStream ois = null;

PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;


// lister les imprimantes qui supportent ce flavor
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
// choix de l'imprimante
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob();
try {
ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File(nomFichier))));

DocAttributeSet das = new HashDocAttributeSet();

Doc doc = new SimpleDoc(ois, flavor, das);


// lancement de l'impression
job.print(doc, pras);
} catch (PrintException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
if(ois!=null){
ois.close();
}
} catch (IOException e){
e.printStackTrace();
}
}
}
}

Merci d'avance

10 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
14 avril 2014 à 14:21
La classe fileReader n'implémente pas Printable, donc, c'est normal que rien ne soit imprimé. Voici un exemple qui fonctionne :
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;



public class Ticket implements Printable {

	public List<String> lignes;

	public Ticket(String path){
		lignes = new ArrayList<String>();
		BufferedReader fluxEntree=null;
		String ligneLue;
		try {
			fluxEntree = new BufferedReader(new FileReader(path));

			ligneLue = fluxEntree.readLine();
			while(ligneLue!=null){
				lignes.add(ligneLue);
				ligneLue = fluxEntree.readLine();
			}
		} 
		catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
	throws PrinterException {
		if (pageIndex > 0) {
			return NO_SUCH_PAGE;
		}
		for(int i=0; i<lignes.size(); i++){
		graphics.drawString(lignes.get(i), 50, 30+i*15);
		}
		return PAGE_EXISTS;
	}
	
	public static void main(String[] args){
		PrinterJob job = PrinterJob.getPrinterJob();

		job.setPrintable(new Ticket("D:\\fichier.txt"));

		boolean doPrint = job.printDialog();
		if(doPrint) {
			try {
				job.print();
			}
			catch (PrinterException e1) {
				e1.printStackTrace();
			}
		}
	}


}


Si tu veux que ce soit un peu plus travaillé, je te conseille ce tutoriel : http://codes-sources.commentcamarche.net/faq/10779-imprimer-avec-java
2
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
28 mars 2014 à 20:49
Salut,

Comment appelles tu ta méthode ? Avec quel argument ?

Essayes de remplacer :
catch (FileNotFoundException e) {
   JOptionPane.showMessageDialog(null, e.getMessage());


par :

catch (FileNotFoundException e) {
   e.printStackTrace();


Cela te donnera plus de précisions sur l'origine de l'erreur.
0
cs_redbar Messages postés 112 Date d'inscription mardi 26 janvier 2010 Statut Membre Dernière intervention 26 mai 2014
29 mars 2014 à 18:12
Après avoir suivi tes conseils voici l'erreur que me retourne l'application

java.io.FileNotFoundException: ticket.txt (Le fichier spécifié est introuvable)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at mti.Utilitaire.imprimerTicket(Utilitaire.java:401)
at mti.Caisse$AnnulerListener.actionPerformed(Caisse.java:771)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.WaitDispatchSupport$2.run(Unknown Source)
at java.awt.WaitDispatchSupport$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(Unknown Source)
at java.awt.Dialog.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at java.awt.Dialog.setVisible(Unknown Source)
at mti.Caisse.<init>(Caisse.java:112)
at mti.Utilitaire.demarrerCaisse(Utilitaire.java:114)
at mti.MenuPrincipal$40.actionPerformed(MenuPrincipal.java:834)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
30 mars 2014 à 12:05
Salut,

C'est donc normal, il faut indiquer le chemin complet vers ton fichier .txt

C:\\Dossier\\ticket.txt
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_redbar Messages postés 112 Date d'inscription mardi 26 janvier 2010 Statut Membre Dernière intervention 26 mai 2014
31 mars 2014 à 12:19
Bonjour cs_julien39
Je n'ai plus l'erreur mais l'impression reste toujours en cours et n'abouti pas.

et quand je vérifie la tâche d'impression le nom du document qui devrait être "ticket.txt" est plutot "java printing".

l'imprimante le réagit pas mais il n'y a pas d'erreur.
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
31 mars 2014 à 15:49
Non, c'est bien java printing, ce n'est pas ticket.txt qui est imprimé mais c'est un programme java qui envoi l'impression.

Si tu vois java printing, c'est que tout s'est bien passé, le problème est donc plutôt lié à ton imprimante qu'au programme java.

Essayes de diriger ton impression vers une imprimante virtuelle comme PDFCreator pour vérifier que le java est ok.
0
cs_redbar Messages postés 112 Date d'inscription mardi 26 janvier 2010 Statut Membre Dernière intervention 26 mai 2014
31 mars 2014 à 17:47
Je viens de faire l'impression sur l'imprimante virtuelle PDFCreator mais je constate que il n'y a pas d'information sur le fichier.

C'est en fait une page vierge qui est convertie en PDF le fichier ticket.txt n'est pas converti.
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
14 avril 2014 à 07:48
Est ce que tu peux me montrer ton code stp ?
0
cs_redbar Messages postés 112 Date d'inscription mardi 26 janvier 2010 Statut Membre Dernière intervention 26 mai 2014
14 avril 2014 à 13:35
voici le code qui me permet d'imprimer le ticket de caisse
public static void imprimerTicket(String nomFichier){
//creation de l'objet FileInputStream
FileInputStream fis = null;

PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;


// lister les imprimantes qui supportent ce flavor
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
// choix de l'imprimante
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob();
try {

fis = new FileInputStream(new File(nomFichier));
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);



// lancement de l'impression
job.print(doc, pras);
} catch (PrintException ex) {
ex.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
if(fis!=null){
fis.close();
}
} catch (IOException e){
e.printStackTrace();
}
}
}
}

et cette méthode est appelé dans une autre classe
de la manière suivante;
class ImprimerListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent arg0) {

String pathFichierRecu = "C:\\RECU_DE_CAISSE\\ticket.txt";

...

Utilitaire.imprimerTicket(pathFichierRecu);

}
}

merci
0
cs_redbar Messages postés 112 Date d'inscription mardi 26 janvier 2010 Statut Membre Dernière intervention 26 mai 2014
15 avril 2014 à 11:45
Merci julien39 , le code marche parfaitement
je vais l'élaborer encore plus en suivant le tuto que tu m'a conseiller.
0
Rejoignez-nous