View Problem

Write a string to a file

ExpandDiskEdit
clojure
(with-out-writer "output.txt" (println "Hello file!"))
ExpandDiskEdit
cpp C++/CLI .NET 2.0
IO::StreamWriter^ stream;

try
{
stream = gcnew IO::StreamWriter("test.txt", false);
stream->WriteLine("This line overwites file contents!");
}
DiskEdit
csharp
System.IO.File.WriteAllText("filename.txt", "Some text to write to the file");
ExpandDiskEdit
erlang
Line = "This line overwites file contents!\n",
{ok, IODevice} = file:open("test.txt", [write]), file:write(IODevice, Line), file:close(IODevice).
ExpandDiskEdit
fantom
File(`out.txt`).out.writeChars("some text").flush
ExpandDiskEdit
fsharp
let stream = new StreamWriter("test.txt", false)
stream.WriteLine("This line overwrites file contents!")
ExpandDiskEdit
groovy
file.delete()
file << 'some text'
DiskEdit
groovy
file.text = 'some text'
DiskEdit
haskell
writeFile "filename" "stringe"
ExpandDiskEdit
java
FileWriter fw = null;

try
{
fw = new FileWriter("test.txt");
fw.write("This line overwites file contents!");
}
ExpandDiskEdit
java
PrintWriter pw = null;

try
{
pw = new PrintWriter(new BufferedWriter(new FileWriter("test.txt")));
pw.print("This line overwites file contents!");
}
DiskEdit
ocaml
try
let cout = open_out filename in
let co = Format.formatter_of_out_channel cout in
Format.fprintf co "%s\n" text_to_write;
close_out cout
with Sys_error _ as e ->
Format.printf "Cannot open file \"%s\": %s\n" filename (Printexc.to_string e)
DiskEdit
perl
open(my $fh, '>', $path) or die "can't open $path: $!";
print $fh "This line overwites file contents!";
close $fh;
ExpandDiskEdit
php
/****
* For some (security)reason I couldn't
* submit this without adding a space to
* the functionnames. Please remove it :)
****/

if ($fh = f open("file.txt", 'w')) {
f write($fh, "Some text\n");
f close($fh);
}
DiskEdit
php PHP 5
<?php
file_put_contents('file.txt', 'a string');
?>
DiskEdit
python
open('test.txt', 'wt').write('Hello World!')
DiskEdit
ruby
File.new("a_file", "w") << "some text"
ExpandDiskEdit
scala org.apache.commons
FileUtils.writeStringToFile(new File("test.txt"), "This line overwites file contents!")
ExpandDiskEdit
scala
val fw = new FileWriter("test.txt") ; fw.write("This line overwites file contents!") ; fw.close()

Submit a new solution for clojure, cpp, csharp, erlang ...