11
\$\begingroup\$

Challenge

Create a new file and write the string Hello World to it.

Restrictions

  • Your challenge must write to a file on disk, in the file system.

  • The file may not be a log file generated during normal operation of the interpreter.

  • The file must contain only the string Hello World. It is allowed to contain a trailing newline or minimal whitespace. No other content.

  • No command-line flags/pipes (etc) allowed, except when necessary to run the program. (e.g. perl -p)

Notes

  • This is , so shortest program in bytes wins.

  • Follow the spirit, not the letter, of the rules.

\$\endgroup\$
5
  • \$\begingroup\$ Is trailing newline okay? \$\endgroup\$
    – Winny
    Aug 1 '16 at 9:03
  • \$\begingroup\$ @Winny yes, it is ok \$\endgroup\$
    – anna328p
    Aug 1 '16 at 20:23
  • \$\begingroup\$ Is a filename part of the contents of a file? \$\endgroup\$ Aug 12 '16 at 20:36
  • \$\begingroup\$ Could I also write to a magnetic tape, or must it be a disk? If so, are you including 3.5" and 5.25" disks? Or do you mean a "hard disk" or solid state disk? \$\endgroup\$ Nov 26 at 16:06
  • \$\begingroup\$ @ShaunBebbers Those are acceptable too \$\endgroup\$
    – anna328p
    Nov 27 at 7:00

53 Answers 53

1
2
1
\$\begingroup\$

Nim, 28 26 bytes

"o".writeFile"Hello World"

Yes, Nim has a function in the system module for opening a file, writing a string to it, then closing it again.

\$\endgroup\$
1
\$\begingroup\$

JavaScript, 89 bytes

This code was tested in Chrome. In other browsers, the element must be in the document for the click method to work.

a=document.createElement("a");a.href="data:text/plain,Hello World";a.download=1;a.click()
\$\endgroup\$
1
\$\begingroup\$

HTML, 83 bytes

This code was tested in Chrome. It relies on the image failing to load, then it clicks itself, triggering the anchor tag which is a download link.

<a href="data:text/plain,Hello World" download><img src onerror="this.click()"></a>
\$\endgroup\$
1
\$\begingroup\$

C#, 118 112 98 bytes


Golfed

void M(){var f=new FileInfo("_");var s=f.CreateText();s.Write("Hello World");s.Flush();s.Close();}

Ungolfed

public void M() {
    var f = new FileInfo( "_" );
    var s = f.CreateText();

    s.Write( "Hello World" );
    s.Flush();
    s.Close();
}

Full explicit code

using System.IO;

namespace N {
    class C {
        public void M() {
            // A file name is required
            FileInfo f = new FileInfo( "_" );
            
            // Obtain the Stream to later write on it
            StreamWriter s = f.CreateText();

            // Write 'Hello World' to the Stream
            s.Write( "Hello World" );
            
            // Flush it to the file
            s.Flush();
            
            // Close the Stream, for security and integrity reasons.
            s.Close();
        }
    }
}

Releases

  • v1.2 - -14 bytes - Changed to vars. Shoutout to Jean Lourenço.
  • v1.1 -  -6 bytes - Simplified names.
  • v1.0 - 118 bytes - Initial solution.



I'll leave a version down below that only uses the Console to write on th file. Just because I can.


C# Console Version, 39 bytes


Golfed

void M(){Console.Write("Hello World");}

Ungolfed

public void M() {
    Console.Write("Hello World");
}

Full code

using System;

namespace N {
    class C {
        public void M() {
            // Write 'Hello World' to the output handler
            Console.Write("Hello World");
        }
    }
}

Usage

<Exe. file name>.exe > FileToOutput.txt

Releases

  • v1.0 - 39 bytes - Initial solution.
\$\endgroup\$
2
  • \$\begingroup\$ You can use 'var' instead of FileInfo and StreamWriter to save some bytes \$\endgroup\$ Aug 2 '16 at 16:37
  • \$\begingroup\$ Right! Completely forgot the vars thing... Thanks @JeanLourenço \$\endgroup\$
    – auhmaan
    Aug 2 '16 at 16:56
1
\$\begingroup\$

Java

public class H
{
    public static void main(String[] a)
        throws IOException
    {
        FileWriter o = new FileWriter("x");
        o.write("Hello World");
        o.close();
    }
}

Simple I/O with no exception handling.

Corrected

Corrected the original code to write the correct string (Hello World).

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Your answer would have been shorter if you had actually wrote the correct string.. \$\endgroup\$
    – Shaun Wild
    Aug 2 '16 at 9:11
  • \$\begingroup\$ @SeanBean: Huh, force of habit, I guess, that I wrote Hello, world. I corrected the code. \$\endgroup\$ Aug 4 '16 at 23:07
  • \$\begingroup\$ @DavidRTribble You can change public class H to class H and IOException to Exception. This will save you 9 bytes. Also, you should include the import java.io.*; \$\endgroup\$ Aug 5 '16 at 15:12
1
\$\begingroup\$

Tcl, 28 bytes

puts [open T w] Hello\ World
\$\endgroup\$
1
\$\begingroup\$

Awk, 29 27 characters

BEGIN{print"Hello World">1}

(No, not another output redirection. Just Awk's syntax is similar.)

Thanks to:

  • Pedro Maimere for suggesting to use integer for file name (-2 characters)

Sample run:

bash-4.3$ awk 'BEGIN{print"Hello World">1}'

bash-4.3$ cat 1
Hello World
\$\endgroup\$
2
  • \$\begingroup\$ Substituting "o" for a lone number like 1 saves 2 bytes. \$\endgroup\$ Nov 27 at 11:01
  • \$\begingroup\$ Thank you, @PedroMaimere. \$\endgroup\$
    – manatwork
    Nov 27 at 14:32
1
\$\begingroup\$

R, 20 bytes

write("Hello World")

This creates a file named "data" in the current directory and write "Hello World" in it.

New contributor
Maël is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Welcome to Code Golf! Nice first answer! \$\endgroup\$
    – Fmbalbuena
    Nov 27 at 16:14
  • \$\begingroup\$ @Fmbalbuena thanks! \$\endgroup\$
    – Maël
    Nov 27 at 16:54
1
\$\begingroup\$

JavaScript (SpiderMonkey), 48 37 30 29 bytes

open`data:text/t,Hello World`

Try it online!

\$\endgroup\$
0
\$\begingroup\$

AutoIT, 43 40 bytes

FileWrite(FileOpen("a",1),"Hello World")


Explanation

FileOpen takes two parameters and returns the handle of the file. Mode must be 1 for writing (appending) text.

FileOpen ( "filename" [, mode = 0] )

FileWrite takes two parameters, a handle and the text to write

FileWrite ( "filehandle/filename", "text/data" )

If a file does not exist, AutoIT will create it. Note that in my example it will not have any file extension associated (filename: a), but can still be opened with any text editor.

AutoIT is a Windows only scripting language.

Script-Example.com has an online AutoIT compiler that can verify the solution.

\$\endgroup\$
0
\$\begingroup\$

Php, 38 bytes

file_put_contents('a', 'Hello World');

\$\endgroup\$
4
  • 1
    \$\begingroup\$ You can save 15 bytes by calling the execution operator. `echo Hello\ World > a` \$\endgroup\$
    – ʰᵈˑ
    Aug 1 '16 at 13:26
  • 2
    \$\begingroup\$ Heh heh that's not really PHP anymore. \$\endgroup\$ Aug 1 '16 at 16:24
  • 1
    \$\begingroup\$ 36 bytes: fputs(fopen('a','c'),'Hello World'); \$\endgroup\$ Aug 1 '16 at 16:28
  • \$\begingroup\$ @AlexHowansky You can do fputs(fopen(a,c),'Hello World'); and save 4 bytes. \$\endgroup\$ Aug 2 '16 at 13:32
0
\$\begingroup\$

Mathematica, 27 bytes

".txt"~Export~"Hello World"
\$\endgroup\$
3
  • \$\begingroup\$ Like in the answer using R... isn't a single character enough for the file name? \$\endgroup\$
    – PEAR
    Aug 4 '16 at 10:06
  • \$\begingroup\$ @PEAR Mathematica can't infer the file format without a file extension. \$\endgroup\$
    – alephalpha
    Aug 4 '16 at 10:20
  • \$\begingroup\$ Oh, ok... maybe something shorter encoded in the same way?... .c for example? \$\endgroup\$
    – PEAR
    Aug 4 '16 at 10:22
0
\$\begingroup\$

Perl, 20 bytes

`echo Hello World>o`

Perl will execute anything in backticks (`) as a system command.

\$\endgroup\$
0
\$\begingroup\$

ListSharp, 37 bytes

OUTP="Hello World" HERE[<here>+"\\a"]

simply writes Hello World to a file called "a"(not even a text file) in the script execution environment,

\$\endgroup\$
0
\$\begingroup\$

PHP, 34 bytes

fputs(fopen(7,'c'),'Hello World');
\$\endgroup\$
1
  • \$\begingroup\$ @Lukabot I reverted your edit because it should have been a comment, and it is completely different enough to be its own answer (the same language can be used multiple times if the approach is different) \$\endgroup\$ Aug 2 '16 at 2:09
0
\$\begingroup\$

PHP, 27 bytes

exec('echo Hello World>o');

PHP, 20 bytes

`echo Hello World>o`
\$\endgroup\$
0
\$\begingroup\$

C#, 50 bytes

s=>System.IO.File.WriteAllText("f","Hello World");

An anonymous function that takes in an unused argument and outputs the file.

Alternatively and cheating slightly this one:

s=>System.IO.File.WriteAllText("f",s);

That takes in a string and works assuming the string contains the text: Hello World, for 38 bytes.

\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 69 bytes

Lauded for being incredibly expressive, it's not the best golfing language...

(with-open-file (f "f" :direction :output) (format f "Hello, world"))

See it work, with permission denied.

\$\endgroup\$
0
\$\begingroup\$

Bash script, 20 18 bytes

echo Hello World>o

Save this as a file write.sh and run it: `bash o.sh

\$\endgroup\$
7
  • 5
    \$\begingroup\$ Why did you repost a deleted answer? \$\endgroup\$
    – Dennis
    Aug 1 '16 at 6:17
  • 2
    \$\begingroup\$ Surely the quotes are unnecessary? \$\endgroup\$
    – Neil
    Aug 1 '16 at 9:19
  • 1
    \$\begingroup\$ You can save a byte by escaping the space instead of using quotes: Hello\ World \$\endgroup\$
    – MTCoster
    Aug 1 '16 at 14:13
  • \$\begingroup\$ @MTCoster, why? help echo says: “Display the ARGs, separated by a single space character and followed by a newline, on the standard output.” \$\endgroup\$
    – manatwork
    Aug 1 '16 at 16:52
  • \$\begingroup\$ @manatwork You're absolutely right, I was unaware of this! \$\endgroup\$
    – MTCoster
    Aug 1 '16 at 16:53
0
\$\begingroup\$

Groovy, 28 characters

new File("o")<<"Hello World"

Sample run:

bash-4.3$ groovy -e 'new File("o")<<"Hello World"'

bash-4.3$ cat o
Hello World
\$\endgroup\$
0
\$\begingroup\$

Sed, 20 characters

(19 characters code + 1 character dummy input.)

s/^/Hello World/
wo

Seems the side effect of also displaying the string to STDOUT after the file was created is not prohibited.

Requires dummy input to work as per Are languages like sed exempt from “no input” rules?

Sample run:

bash-4.3$ sed $'s/^/Hello World/;wo' <<< ''
Hello World

bash-4.3$ cat o
Hello World
\$\endgroup\$
0
\$\begingroup\$

QBasic, 39 bytes

OPEN"f"FOR OUTPUT AS#1
?#1,"Hello World

Opens a file named f (creating it if it doesn't exist, wiping it if it does) and gives it the file handle #1. Then prints (?) to file #1 the string Hello World (with a trailing newline).

\$\endgroup\$
0
\$\begingroup\$

Jelly, 25 bytes

“ṠȷŀƤoĊịSAĊhJİⱮa¤XPṢḞɦ»ŒV

Try it online!

Executes the Python code open(*'aw').write('Hello World').

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.