Write file
From Programmer's Wiki
This article contains examples of how to write data to a file in different languages
Contents |
[edit] C
#include <stdio.h>
int main() {
FILE *f;
f = fopen("somefile.txt", "w");
if(f != NULL) {
fprintf("some text\n");
fclose(f);
}
return 0;
}
[edit] Java
try {
BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
out.write("aString");
out.close();
} catch (IOException e) {
}
[edit] Groovy
#!/usr/bin/env groovy
def writer=new File("FileWrite.out").newWriter()
writer.writeLine("contents")
writer.close()
[edit] Perl
#!/usr/local/bin/perl open (MYFILE, '>>data.txt'); print MYFILE "Bob\n"; close (MYFILE);
[edit] Python
f = open(filename, 'w') f.write(data) f.close()
[edit] Ruby
out = File.new(filename, "w+") out << "wrote into file" << "\n" out.close
