Login
|
Signup
langref.org
-
perl
,
java
, and
clojure
add..
all
cpp
csharp
erlang
fantom
fsharp
go
groovy
haskell
ocaml
php
python
ruby
scala
Home
All
Solved
Unsolved
Strings
Numbers
Regex
Lists
Maps
Structure
Files
Dates
OOP
Networking
XML
Algorithms
Misc
Parallel
View Problem
Files
Writing
Write a string to a file
perl
open(my $fh, '>', $path) or die "can't open $path: $!";
print $fh "This line overwites file contents!";
close $fh;
open(my $fh, '>', $path) or die "can't open $path: $!";
print $fh "This line overwites file contents!";
close $fh;
java
FileWriter fw = null;
try
{
fw = new FileWriter("test.txt");
fw.write("This line overwites file contents!");
}
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
public class SolutionXX {
public static void main(String[] args) throws IOException {
FileWriter fw = null;
try
{
fw = new FileWriter("test.txt");
fw.write("This line overwites file contents!");
}
catch (FileNotFoundException e) { System.out.println("FNF"); }
catch (IOException e) { System.out.println("IOE"); }
catch (Exception e) { System.out.println("E"); }
finally { if (fw != null) fw.close(); }
}
}
java
PrintWriter pw = null;
try
{
pw = new PrintWriter(new BufferedWriter(new FileWriter("test.txt")));
pw.print("This line overwites file contents!");
}
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.IOException
public class SolutionXX {
public static void main(String[] args) throws IOException {
PrintWriter pw = null;
try
{
pw = new PrintWriter(new BufferedWriter(new FileWriter("test.txt")));
pw.print("This line overwites file contents!");
}
catch (FileNotFoundException e) { System.out.println("FNF"); }
catch (IOException e) { System.out.println("IOE"); }
catch (Exception e) { System.out.println("E"); }
finally { if (pw != null) pw.close(); }
}
}
clojure
(with-out-writer "output.txt" (println "Hello file!"))
(use '[clojure.contrib.duck-streams :only (with-out-writer)])
(with-out-writer "output.txt" (println "Hello file!"))
Submit a new solution for
perl
,
java
, or
clojure
There are 15 other solutions in
additional
languages (
cpp
,
csharp
,
erlang
,
fantom
...)