ComposantsComponents

ComposantsComponents › Fichier › LectureFile › Input

tFileInputRaw

tFileInputRaw

Fichier › LectureFile › Input

Lit un fichier en entier et l'envoie en une seule ligne, dans l'unique colonne content : comme un texte décodé avec l'encodage choisi, comme un tableau d'octets, ou comme un flux d'entrée Java ouvert sur le fichier. À choisir pour traiter un document d'un seul tenant : un JSON ou un XML à analyser plus loin, une page, une image, un fichier à recopier ou à envoyer tel quel. C'est le pendant en lecture du tFileOutputRaw. Reads a whole file and sends it as a single row, in the single content column: as a text decoded with the chosen encoding, as a byte array, or as a Java input stream opened on the file. Choose it to process a document in one piece: a JSON or XML to parse further on, a page, an image, a file to copy or send as is. It is the reading counterpart of tFileOutputRaw.

EntréeInput

Aucune : il commence son sous-job. Sinon un lien Iterate : il lit un fichier à chaque itération. None: it starts its subjob. Otherwise one Iterate link: it reads one file at each iteration.

SortieOutput

Main un flux, obligatoire : une ligne, avec la colonne contentone flow, required: one row, with the content column

Déclencheurs en sortieOutput triggers

On Subjob Ok On Subjob Error On Component Ok On Component Error Run if

On Subjob : seulement quand il commence son sous-job. On Subjob: only when it starts its subjob.

UtilisationUsage

Un fichier, une ligneOne file, one row

row1 (Main) tFileInputRaw_1 tJavaRow_1

Le composant suivant reçoit tout le fichier dans content, de type Object : il le convertit ((String) row1.content) avant de l'analyser, dans un tJavaRow ou un tMap. The next component receives the whole file in content, of type Object: it casts it ((String) row1.content) before parsing it, in a tJavaRow or a tMap.

Chaque fichier d'un dossierEach file of a folder

Iterate row1 (Main) tFileList_1 tFileInputRaw_1 tJavaRow_1

Le tFileList donne un fichier à chaque itération ; le nom de fichier est alors ((String) globalMap.get("tFileList_1_CURRENT_FILEPATH")) (voir l'exemple). The tFileList gives one file at each iteration; the file name is then ((String) globalMap.get("tFileList_1_CURRENT_FILEPATH")) (see the example).

  1. Déposez le tFileInputRaw depuis la famille Fichier › Lecture de la palette. Drop the tFileInputRaw from the File › Input family of the palette.
  2. Indiquez le Nom de fichier, puis le Mode : texte (avec son Encodage), octets ou flux. Set the Filename, then the Mode: text (with its Encoding), bytes or stream.
  3. Reliez sa sortie Main au composant qui traite le contenu : le lien est obligatoire. Connect its Main output to the component that processes the content: the link is required.

Paramètres simplesBasic settings

Les libellés sont ceux de l'onglet Composant ; le nom technique, en gris, est celui du paramètre dans le fichier du job. Le composant n'a pas de paramètres avancés propres. Labels are those of the Component tab; the technical name, in grey, is the parameter name in the job file. The component has no advanced settings of its own.

ParamètreSetting DéfautDefault VisibleShown Description
SchémaSchemaSCHEMA content (Object) ToujoursAlways Fixe : une seule colonne, content, de type Object, en lecture seule. Elle reçoit un String, un byte[] ou un java.io.InputStream selon le Mode. Fixed: a single column, content, of type Object, read only. It receives a String, a byte[] or a java.io.InputStream depending on the Mode.
Nom de fichierFilenameFILENAME chemin par défaut + /in.txtdefault path + /in.txt ToujoursAlways Le fichier lu, en expression Java : context.folder + "/page.html". Un fichier absent ou illisible : voir les erreurs. The file read, as a Java expression: context.folder + "/page.html". A missing or unreadable file: see the errors.
ModeAS_STRING · AS_BYTEARRAY · AS_INPUTSTREAM Chaîne de caractèresString ToujoursAlways Lire le fichier comme une chaîne de caractères : tout le texte, décodé avec l'Encodage. Lire ce fichier comme tableau d'octets : tous les octets, sans conversion. Lire le fichier (Stream the file) : un java.io.FileInputStream ouvert sur le fichier, lu par le composant suivant ; le tFileInputRaw le ferme ensuite. Voir les exemples. Read the file as a string: all the text, decoded with the Encoding. Read the file as a bytes array: all the bytes, without conversion. Stream the file: a java.io.FileInputStream opened on the file, read by the next component; the tFileInputRaw closes it afterwards. See the examples.
EncodageEncodingENCODING "ISO-8859-15" Mode chaîne de caractèresString mode L'encodage du fichier texte. Le défaut n'est pas UTF-8 : un fichier UTF-8 lu tel quel donne Café au lieu de Café. Une marque d'ordre des octets (BOM) n'est pas retirée : elle reste en tête du texte (voir l'exemple). The encoding of the text file. The default is not UTF-8: a UTF-8 file read as is gives Café instead of Café. A byte order mark (BOM) is not removed: it stays at the start of the text (see the example).
Arrêter en cas d'erreurDie on errorDIE_ON_ERROR NonNo ToujoursAlways Décochée, une erreur de lecture est seulement affichée sur la sortie d'erreur, et la ligne part quand même, avec content nul. Cochée, elle arrête le job. Voir les erreurs. Unchecked, a read error is only printed on the error output, and the row goes on anyway, with a null content. Checked, it stops the job. See the errors.

Valeurs et variables globalesValues and global variables

Ce que contient contentWhat content holds

  • Chaîne : le fichier entier, saut de ligne final compris, rien n'est retiré. String: the whole file, final line break included, nothing is removed.
  • Octets : tous les octets du fichier. Bytes: all the bytes of the file.
  • Flux : un java.io.FileInputStream encore ouvert, à lire par le composant suivant, pendant le traitement de la ligne. Le tFileInputRaw le ferme à la fin de son exécution. Stream: a java.io.FileInputStream still open, to be read by the next component, while the row is processed. The tFileInputRaw closes it at the end of its run.
  • Erreur sans arrêt : null. Error without stopping: null.

La colonne est de type Object : convertissez-la avant de vous en servir, (String) row1.content ou (byte[]) row1.content. Les modes chaîne et octets chargent tout le fichier en mémoire. The column is of type Object: cast it before using it, (String) row1.content or (byte[]) row1.content. The string and bytes modes load the whole file in memory.

Variables globalesGlobal variables

FILENAME_PATH String
Le nom de fichier tel que calculé, sans transformation ; rempli seulement si la lecture a réussi.The file name as computed, unchanged; filled only if the read succeeded.
ERROR_MESSAGE String
Message de l'erreur de lecture. Absente de la liste des variables du Studio, mais remplie : utile avec Arrêter en cas d'erreur décoché.Message of the read error. Missing from the Studio variable list, but filled: useful with Die on error unchecked.
((String) globalMap.get("tFileInputRaw_1_FILENAME_PATH"))
((String) globalMap.get("tFileInputRaw_1_ERROR_MESSAGE"))

ExemplesExamples

Les fichiers sont dans le dossier de la variable de contexte folder, raccourcie ici en C:/data. Dans chaque exemple, un tJavaRow affiche ce que contient content : pour un texte, sa longueur, le texte avec ses sauts de ligne visibles, puis le code de chaque caractère ; pour des octets, leur valeur en hexadécimal ; sinon la classe de l'objet. Les sorties sont celles de la console, copiées depuis une exécution du job. The files are in the folder of the folder context variable, shortened here to C:/data. In each example, a tJavaRow prints what content holds: for a text, its length, the text with its line breaks made visible, then the code of each character; for bytes, their value in hexadecimal; otherwise the class of the object. The outputs are the console ones, copied from a run of the job.

Code du tJavaRowtJavaRow code

Object c = input_row.content;
if (c instanceof String) {
    String s = (String) c;
    StringBuilder hex = new StringBuilder();
    for (char ch : s.toCharArray()) {
        hex.append(String.format("%04X ", (int) ch));
    }
    System.out.println("String, " + s.length() + " chars: " + s.replace("\r", "\\r").replace("\n", "\\n") + " | " + hex.toString().trim());
} else if (c instanceof byte[]) {
    StringBuilder hex = new StringBuilder();
    for (byte b : (byte[]) c) {
        hex.append(String.format("%02X ", b));
    }
    System.out.println("byte[], " + ((byte[]) c).length + " bytes: " + hex.toString().trim());
} else {
    System.out.println(c == null ? "null" : c.getClass().getName());
}

Texte et encodageText and encoding

row1 (Main) OnSubjobOk OnSubjobOk tFileInputRaw_1 tJavaRow_1 tFileInputRaw_2 … tFileInputRaw_4 tJava_1

Quatre sous-jobs qui se suivent, chacun un tFileInputRaw en mode chaîne relié à un tJavaRow. Le même texte, Café 10 € suivi d'un saut de ligne, dans trois fichiers (octets en hexadécimal) : Four subjobs one after the other, each one a tFileInputRaw in string mode connected to a tJavaRow. The same text, Café 10 € followed by a line break, in three files (bytes in hexadecimal):

raw/cafe_utf8.txt    43 61 66 C3 A9 20 31 30 20 E2 82 AC 0A
raw/cafe_bom.txt     EF BB BF 43 61 66 C3 A9 20 31 30 20 E2 82 AC 0A
raw/cafe_latin.txt   43 61 66 E9 20 31 30 20 A4 0A
  1. cafe_utf8.txt, encodage par défaut (ISO-8859-15).cafe_utf8.txt, default encoding (ISO-8859-15).
  2. cafe_utf8.txt, "UTF-8".cafe_utf8.txt, "UTF-8".
  3. cafe_bom.txt, "UTF-8".cafe_bom.txt, "UTF-8".
  4. cafe_latin.txt, encodage par défaut. Puis le tJava affiche FILENAME_PATH du premier.cafe_latin.txt, default encoding. Then the tJava prints the FILENAME_PATH of the first one.
String, 13 chars: Café 10 â¬\n | 0043 0061 0066 00C3 00A9 0020 0031 0030 0020 00E2 0082 00AC 000A
String, 10 chars: Café 10 €\n | 0043 0061 0066 00E9 0020 0031 0030 0020 20AC 000A
String, 11 chars: Café 10 €\n | FEFF 0043 0061 0066 00E9 0020 0031 0030 0020 20AC 000A
String, 10 chars: Café 10 €\n | 0043 0061 0066 00E9 0020 0031 0030 0020 20AC 000A
FILENAME_PATH: C:/data/raw/cafe_utf8.txt

1 : chaque octet UTF-8 devient un caractère (13 au lieu de 10) ; le caractère 0082, invisible, est entre â et ¬. 3 : la marque d'ordre des octets reste en tête du texte, en caractère invisible FEFF. Le saut de ligne final est gardé dans tous les cas. 1: each UTF-8 byte becomes one character (13 instead of 10); the invisible character 0082 is between â and ¬. 3: the byte order mark stays at the start of the text, as the invisible character FEFF. The final line break is kept in every case.

OctetsBytes

Mode tableau d'octets, deux sous-jobs : image.bin (le début d'un PNG, 89 50 4E 47 0D 0A 1A 0A 00 FF), puis raw/cafe_utf8.txt de l'exemple précédent. Bytes array mode, two subjobs: image.bin (the start of a PNG, 89 50 4E 47 0D 0A 1A 0A 00 FF), then raw/cafe_utf8.txt from the previous example.

byte[], 10 bytes: 89 50 4E 47 0D 0A 1A 0A 00 FF
byte[], 13 bytes: 43 61 66 C3 A9 20 31 30 20 E2 82 AC 0A

Les octets sont ceux du fichier, un par un : l'encodage n'intervient pas. The bytes are those of the file, one by one: the encoding plays no part.

Flux, et copie d'un fichierStream, and copy of a file

row1 (Main) OnSubjobOk row2 (Main) OnSubjobOk tFileInputRaw_1 tJavaRow_1 tFileInputRaw_2 tFileOutputRaw_1 tJava_1

Mode Lire le fichier sur image.bin. Le tJavaRow affiche la classe reçue ; le second sous-job passe le flux à un tFileOutputRaw qui écrit copy/image.bin, puis le tJava affiche la copie en hexadécimal : Stream the file mode on image.bin. The tJavaRow prints the class received; the second subjob gives the stream to a tFileOutputRaw that writes copy/image.bin, then the tJava prints the copy in hexadecimal:

java.io.FileInputStream
copy/image.bin: 89 50 4E 47 0D 0A 1A 0A 00 FF

Le fichier est recopié sans être chargé en mémoire : la bonne façon de copier ou d'envoyer un gros fichier binaire. The file is copied without being loaded in memory: the right way to copy or send a large binary file.

Chaque fichier d'un dossierEach file of a folder

Iterate row1 (Main) tFileList_1 tFileInputRaw_1 tJavaRow_1

Le tFileList liste les *.txt du dossier raw de l'exemple sur l'encodage, triés par nom. Le tFileInputRaw, en mode chaîne et "UTF-8", lit ((String) globalMap.get("tFileList_1_CURRENT_FILEPATH")). Code du tJavaRow : The tFileList lists the *.txt of the raw folder from the encoding example, sorted by name. The tFileInputRaw, in string mode and "UTF-8", reads ((String) globalMap.get("tFileList_1_CURRENT_FILEPATH")). tJavaRow code:

System.out.println(((String) globalMap.get("tFileList_1_CURRENT_FILE")) + ": " + String.valueOf(input_row.content).replace("\n", "\\n"));
cafe_bom.txt: Café 10 €\n
cafe_latin.txt: Caf� 10 �\n
cafe_utf8.txt: Café 10 €\n

Une ligne par fichier. Le fichier ISO-8859-15, lu en UTF-8, montre des caractères de remplacement : l'encodage vaut pour tous les fichiers de la boucle. One row per file. The ISO-8859-15 file, read as UTF-8, shows replacement characters: the encoding applies to all the files of the loop.

ErreursErrors

Le nom de fichier désigne un fichier absent, context.folder + "/absent.txt". Le nom du job est remplacé par MyJob, le dossier de travail par C:\data ; la trace d'exception est abrégée. The file name points to a missing file, context.folder + "/absent.txt". The job name is replaced with MyJob, the working folder with C:\data; the exception trace is shortened.

Arrêter en cas d'erreur décoché (défaut)Die on error unchecked (default)

row1 (Main) OnSubjobOk tFileInputRaw_1 tJavaRow_1 tJava_1

Le tJavaRow est celui des exemples ; le tJava affiche ensuite les deux variables globales : The tJavaRow is the one of the examples; the tJava then prints the two global variables:

System.out.println("FILENAME_PATH: " + globalMap.get("tFileInputRaw_1_FILENAME_PATH"));
System.out.println("ERROR_MESSAGE: " + globalMap.get("tFileInputRaw_1_ERROR_MESSAGE"));
java.io.FileNotFoundException: File 'C:\data\absent.txt' does not exist
null
FILENAME_PATH: null
ERROR_MESSAGE: File 'C:\data\absent.txt' does not exist

La première ligne est sur la sortie d'erreur. La ligne part quand même vers le tJavaRow, avec content nul, le sous-job est réussi et le job se termine avec le code 0. Testez content ou ERROR_MESSAGE, ou cochez la case. The first line is on the error output. The row goes on anyway to the tJavaRow, with a null content, the subjob succeeds and the job ends with exit code 0. Test content or ERROR_MESSAGE, or check the box.

Arrêter en cas d'erreur cochéDie on error checked

Même job : il s'arrête (code 1), ni le tJavaRow ni le tJava ne s'exécutent. Same job: it stops (exit code 1), neither the tJavaRow nor the tJava runs.

Exception in component tFileInputRaw_1 (MyJob)
java.io.FileNotFoundException: File 'C:\data\absent.txt' does not exist
	at org.apache.commons.io.FileUtils.openInputStream(FileUtils.java:2368)
	at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:2486)
	…

Conseils et limitesTips and limits

Erreur silencieuse par défautSilent error by default

Avec Arrêter en cas d'erreur décoché, un fichier absent ne fait pas échouer le job : la suite reçoit une ligne dont content est nul. Cochez la case dans un job planifié, ou vérifiez d'abord le fichier avec un tFileExist. With Die on error unchecked, a missing file does not make the job fail: the next components receive a row whose content is null. Check the box in a scheduled job, or check the file first with a tFileExist.

Encodage et BOMEncoding and BOM

Le défaut est ISO-8859-15 : pour un JSON ou un XML, choisissez presque toujours "UTF-8". Une marque d'ordre des octets reste en tête du texte et peut gêner un analyseur ; retirez-la au besoin (s.replace("\uFEFF", "")). The default is ISO-8859-15: for a JSON or an XML, nearly always choose "UTF-8". A byte order mark stays at the start of the text and may disturb a parser; remove it if needed (s.replace("\uFEFF", "")).

Gros fichiersLarge files

Les modes chaîne et octets mettent tout le fichier en mémoire. Pour recopier ou envoyer un gros fichier, prenez le mode flux ; pour le traiter ligne par ligne, un tFileInputFullRow ou un tFileInputDelimited. The string and bytes modes put the whole file in memory. To copy or send a large file, take the stream mode; to process it line by line, a tFileInputFullRow or a tFileInputDelimited.

Lire le flux pendant la ligneRead the stream during the row

En mode flux, le fichier n'est ouvert que le temps du traitement de la ligne : le composant suivant doit le lire tout de suite. Le ranger dans une variable pour un autre sous-job ne marche pas, il sera fermé. In stream mode, the file is only open while the row is processed: the next component must read it right away. Storing it in a variable for another subjob does not work, it will be closed.

Voir aussiSee also