Save a string to a file

From CodeCodex

Revision as of 09:44, 26 November 2009 by 78.43.62.210 (Talk)

It's quite common to have a string in memory that needs to be saved as a text file. Below are implementations to store a string (or similar) object as a file on the local file system.

Implementations

Java

Use this source code to save a string to a file in Java:

<HIGHLIGHTSYNTAX language="java"> import java.io.IOException; import java.io.PrintWriter; import java.io.FileWriter; ... public static void saveTextFile(String contents, File file) throws IOException {

 PrintWriter out = new PrintWriter(new FileWriter(file));
 out.print(contents);
 out.close();

} </HIGHLIGHTSYNTAX>

C

Use this source code to save a string to a file in C: <HIGHLIGHTSYNTAX language="c">

  FILE *f;
  f = fopen ("path/to/file.ext", "w");
  fprintf (f, "%s", buff);
  fclose (f);

</HIGHLIGHTSYNTAX>


Common Lisp

 (with-open-file (stream "filename.txt" :direction :output)
   (format stream "Your string here."))

Haskell

<HIGHLIGHTSYNTAX language="haskell"> main = writeFile "filename.txt" "Your string here." </HIGHLIGHTSYNTAX>

OCaml

<HIGHLIGHTSYNTAX language="ocaml">

# let save file string =
    let channel = open_out file in
    output_string channel string;
    close_out channel;;
val save : string -> string -> unit = <fun>

</HIGHLIGHTSYNTAX> For example:

# save "foo" "bar\n";;
- : unit = ()
$ cat foo
bar

The Micmatch library provides a function which does this directly (Micmatch.Text.save):

# open Micmatch;;
# Text.save "foo" "bar\n";;
- : unit = ()

Perl

Use this source code to save a string to a file in Perl: <HIGHLIGHTSYNTAX language="perl"> use File::Slurp qw(write_file); write_file('filename', $string); </HIGHLIGHTSYNTAX>

PHP

Use this source code to save a string to a file in PHP 5.x and above:

<HIGHLIGHTSYNTAX language="php"> file_put_contents('file.name', 'file contents'); </HIGHLIGHTSYNTAX>

PHP 4.x and below:

<HIGHLIGHTSYNTAX language="php"> $fh = fopen('file.name', 'w'); fputs($fh, 'file contents'); fclose($fh); </HIGHLIGHTSYNTAX>

Python

Use this source code to save a string to a file in Python: <HIGHLIGHTSYNTAX language="python"> def save(filename, contents):

   fh = open(filename, 'w')
   fh.write(contents)
   fh.close()

save('file.name', 'some stuff') </HIGHLIGHTSYNTAX>

Seed7

The "getf.s7i" library contains the function putf to write a string to a file:

putf("filename", string);

The definition of putf in the "getf.s7i" library is:

const proc: putf (in string: file_name, in string: stri) is func
  local
    var file: work_file is STD_NULL;
  begin
    if stri <> "" then
      work_file := open(file_name, "w");
      if work_file <> STD_NULL then
        write(work_file, stri);
        close(work_file);
      end if;
    end if;
  end func;

Tcl

proc text_save {filename string} {
  set f [open $filename w]
  fconfigure $f -encoding utf-8 -eofchar {} -translation lf
  puts -nonewline $f $string
  close $f
}

text_save foo bar\n

Visual Basic

Use this source code to save a string to a file in Visual Basic (VB): <HIGHLIGHTSYNTAX language="vb"> sub SaveTextToFile(sText As String, sFileName As String)

 Dim IFnum As Long
 IFnum = FreeFile                      ' Get the next available file number
 Open sFileName For Output As IFnum    ' Open file for write & create if it does not exist
 Print #IFnum, sText                   ' Write string to file number
 Close IFnum                           ' Close the file

end sub

SaveTextToFile("Some text I want to save", "c:\test.txt") </HIGHLIGHTSYNTAX>

See also

Category:String