0

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

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by ~s.o.s~
0

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!

0

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])
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.