Creating Files

E

E. Saynatkari

Andy said:
Could someone please tell me how to create a new file?

You could use FileUtils.touch or just File.open a filename
in write-mode. You can take a look at the documentation for
those classes by typing

ri FileUtils
ri File

On your command-line.
Thank you.


E
 
T

Tony Mobily

Hi,

For an empty file, I guess the easiest way would be:

# Create a new file and write on it
#
begin
f=File::eek:pen("test.txt","w")
rescue SystemCallError
puts "Problem with File::eek:pen #1"
#... something went wrong...
else
f.close
end


# The file will be closed afterwards
#
begin
File::eek:pen("/etc/test.txt","w") do | f |
# Do somethinf with "f" ...
end
rescue SystemCallError
# The file can't be created
puts "Problem with File::eek:pen #2"
end


# If all you want is create an empty file...
#
begin
File::eek:pen("test.txt","w") {}
rescue SystemCallError
# The file can't be created
puts "Problem with File::eek:pen #3"
end

read "ri IO::eek:pen" for more info!

Merc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top