i'm a java c/c++ person. trying out ruby for the 1st time!

how do i do something like this in ruby:

//this is java 
public static void main ( String args [] ) {
	File inFile  = new File ( args [ 0 ] ); 
        File outFile = new File ( args [ 1 ] );
DataInputStream reader = new DataInputStream ( new BufferedInputStream ( new FileInputStream ( inFile ) ) );
		BufferedWriter writer = new BufferedWriter ( new FileWriter ( outFile ) );
String str;
while (str = reader.readline() != null){
writer.write (str);
}
}

note that this code may not compile i pulled it off of a java program i did a while back.

drjay

well i figured it out btw. so for anyone in the future looking to do something similar to the java code above:

#arguments
inFile = ARGV[0] 
outFile = ARGV[1]

def foo(infile, outfile)
	begin
		puts "Opening input file..."
		fin = File.open(inFile, "r")
		puts "Creating output file..."		
		fout = File.open(outFile, "w")
		puts "Reading from input file and writing to output file..."		
		str = ins.readlines()
		fout.puts str
		puts "Closing files..."
		fin.close
		fout.close
		puts "Process complete!"
	rescue => err
		puts "Exception: #{err}"
		err
	end
end

foo(inFile,  outFile)

takes in 2 files as command line arguments. 1st file being the input file and the second being the output file. then it writes the input to the output file.

happy coding

drjay!

Guess I'm late for the party but a simpler way of doing it would be:

require 'fileutils'
FileUtils.copy_file(ARGV[0], ARGV[1])
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.