Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/mwexler/public_html/tp/textpattern/lib/txplib_db.php on line 14
The Net Takeaway: Judoscript and Files


Danny Flamberg's Blog
Danny has been marketing for a while, and his articles and work reflect great understanding of data driven marketing.

Eric Peterson the Demystifier
Eric gets metrics, analytics, interactive, and the real world. His advice is worth taking...

Geeking with Greg
Greg Linden created Amazon's recommendation system, so imagine what can write about...

Ned Batchelder's Blog
Ned just finds and writes interesting things. I don't know how he does it.

R at LoyaltyMatrix
Jim Porzak tells of his real-life use of R for marketing analysis.







Judoscript and Files · 03/10/2005 05:41 PM,

First off, if you found this page via a web search or bookmark, you may be much happier in the JudoScript Section of this site to see the multiple articles about Judoscript, including this one, but also about Databases, Graphics, Files, etc.

Reading and Writing Files
Documented at

Finding Them
To get a list of files and directories, use the listFiles command.

 listFiles '*.judo' in 'C:/Documents and Settings/Michael/My Documents';
 for x in $_ {
   println x;

Note the use of single quotes and forward slashes! Windows people are used to the backward slash and double quotes around filenames with spaces in them; break that habit here. An alternative is to use double backslashes (\\) instead of single ones (\) in Windows.

The listFiles command can also look inside Zip and Tar files, as well as run recursively (and you can limit the recursion). You can order by different file attributes, exclude files by name (or use “dirOnly” to just list directories), and also gather counts and total size while reading the tree. Speaking of trees, listFiles can also return its findings as a Tree (collection of TreeNodes, JudoScript specific datastructure).

Finally, note that listFiles can also use the “addToClasspath” action to add its findings right to the (current and temporary) classpath.

BTW, JudoScript can also mkdir, rmdir, rename/move files, change file attributes, execute shell commands on files, and work with archives. If you are interested, read the chapter.

Oh, one more little trick: copy can use URLs. You can save results of a link like this:

 copy '' to 'i.html';

If the file name is not specified in the URL, JudoScript provides a default file name, “default.htm”. (This works fine for text html; I haven’t tested for binary files… but should work just as well, I bet).

Working with Them
Methods include .fileExists, .isDir(); .isFile(); .fileTime(); .fileSize()

To read from or write to a file, it must be opened first in binary stream mode, binary random access mode or text mode. The open file is stored as a handle, and closed when done.

Text Files
Following the Judoscript example program, the following program adds line numbers and prints on the standard output:

  if #args.length() == 0 {
    println 'Usage: java judo ', #prog, ' <filename>';
    exit 0;
  file = openTextFile(#args[0]);
  for lineNum=1; (line=file.readLine()) != eof; ++lineNum {
    println lineNum : >3, ': ', line;


Can skip characters with file.skip(5); .read() reads a single character as ascii: ch =;

So, the do … as lines mechanism makes it easy to walk through a file line by line. If you don’t give a variable, it uses the _$ default variable. the loopIndex() function lets you know where you are in the implicit loop, acting as a “line counter” in this case.

  // Read file, split each line on its tabs, etc.
  do afile as lines {
    if (loopIndex() < 10) continue;
    fields = $_.csv('t');
  // ....

To write, use println, print, or flush. flush does exactly the same as print except it flushes the output stream.
println always flushes. The target can be out (the default), err, log, pipe or a variable that denotes an open text file. Here is an example:

  dat = openTextFile('important.dat', 'w'); // 'w': write.
  dat.println  '2001/10/1,XYZ,12.00,100000';

So, the JudoScriptish way to handle looping, though, is pretty cool. Here’s a sample from James on the Yahoo Groups list. It uses a local “heredoc” to keep the data in the script (and read from $$local), but same idea. Note how he uses an if to skip the header if it exists by testing for it. Also, this one splits on spaces, but it could just have easily split on \t tabs

  usage {
  minArgs = 3;
  args = 'dbUrl, dbUser, dbPassword';
  desc = 'Inserting records into a db table';

  db::connect to #args[0], #args[1], #args[2];

  var started = false;
  do $$local as lines {
    if started {
      var fields = $_.csv(" ");
      insert into t_wd_ratings(userid, itemid, ratingvalue)
                     values ( (* fields[0] *), (* fields[1] *), 1)
    elif $_.startsWith('MKCCUSID_C ') {
      started = true;

  db::disconnect a;

  EndScript ===============================

136 1008363 1
137 1008363 C 1
138 1008363 P 1
631 1009537 2
632 1009537 C 2
633 1009537 P 2
634 1008410 FRA 1
635 1008411 1
636 1008411 C 1

Binary Files
Binary files are opened with openFile() system function. Usually, a Java byte array is used to read chunks of the file. See:

* * *


  Textile Help
Please note that your email will be obfuscated via entities, so its ok to put a real one if you feel like it...

powered by Textpattern 4.0.4 (r1956)