ariya/phantomjs — pull request #199 by execjosh (2012-02-02 16:22:31+00:00)
Title: Charset option for `fs.{open,read,write}`

This is a fix for [Issue 367](http://code.google.com/p/phantomjs/issues/detail?id=367). The charset to be used by `fs.{open,read,write}` can now be passed as an option. Additionally, instead of using the system default, will use _UTF-8_ when unspecified. This allows for minimal effort across platforms, and is consistent with the command-line options `--{output,script}-encoding`.

Here's an example of converting _EUC-JP_ to _Shift-JIS_ (adapted from [comment 6](http://code.google.com/p/phantomjs/issues/detail?id=367#c6)).

Assuming a file, _test1.txt_, with _eucjp_-encoded text (`日本語\n`) in it, like so:

```bash
$ od -An -tx1 test1.txt
c6 fc cb dc b8 ec 0a
$ cat test1.txt | nkf -Ew
日本語
```

And a script, `test.coffee`, like so:

```coffeescript
fs = require 'fs'
str = fs.read 'test1.txt', charset: 'eucjp'
console.log "Read: #{str}"
fs.write 'test2.txt', str, mode: 'w', charset: 'sjis'
phantom.exit 0
```

We would get a file, _text2.txt_, with _sjis_-encoded text, like so:

```bash
$ echo $LANG
ja_JP.UTF8
$ phantomjs test.coffee
Read: 日本語

$ od -An -tx1 test2.txt
93 fa 96 7b 8c ea 0a
$ cat test2.txt | nkf -Sw
日本語
```