From: Leif Jantzen Date: 2002-12-12T09:02:30+09:00 Subject: Re: help -- persuade my boss to adopt ruby Shannon Fang wrote: > Hi Ruby Lovers, > > File.open("output.txt","w+") do |f| > File.open("input.txt").each_line do |line| > fld=line.match(/.{4}(.{3})(.{6})(.{3}).{17}(.{6})/) > f.print fld[0],"-",fld[1],"-",fld[2],fld[3] > end > end > You might try something like the following. It has been tested on a sample file of my own design... There may be better/more compact/idiomatic ways to do this... Although this example is slightly more complex than the ruby example, this should not be the only reason to choose ruby over java. Think carefully before you choose. # File: ProcessFile.java import java.io.* ; import java.util.regex.* ; class ProcessFile { public static void main(String[] argv){ try { File f_in = new File("input.txt"); File f_out = new File("output.txt"); String line ; Pattern p = Pattern.compile("(.{4}(.{3})(.{6})(.{3}).{17}(.{6}))") ; LineNumberReader in = new LineNumberReader(new FileReader(f_in)); PrintWriter out = new PrintWriter(new FileWriter(f_out)); while ( null != ( line=in.readLine())) { Matcher m = p.matcher(line); if ( true == m.find()) { System.out.println(m.group(1) + "-" + m.group(2) + "-" + m.group(3)+ m.group(4)); } } } catch ( Exception e ) { e.printStackTrace(); } } }