Skip to content

Instantly share code, notes, and snippets.

@retronym
Created January 6, 2010 10:34
Show Gist options
  • Select an option

  • Save retronym/270191 to your computer and use it in GitHub Desktop.

Select an option

Save retronym/270191 to your computer and use it in GitHub Desktop.
Change File Encodings
import collection.immutable.List
import java.io.{OutputStreamWriter, FileWriter, FileOutputStream, File}
import scala.io.{Source, Codec}
import scalaz._
import Scalaz._
val base = new File("""E:\code\raptor""")
val tree: Tree[Stream[File]] = base.unfoldTree[Stream[File]] {(f: File) =>
val (files, dirs) = f.listFiles.toStream.partition(_.isFile)
(files, () => dirs)
}
val files = tree.stream.μ
implicit val fileShow = showA[File]
def convert(f: File): Unit = {
val buffer = new collection.mutable.ArrayBuffer[Char]
val source: Source = Source.fromFile(f)("windows-1252")
try {
source.copyToBuffer(buffer)
} finally {
source.close
}
val fos: FileOutputStream = new FileOutputStream(f)
try {
fos.write(Codec.fromUTF8(buffer.toArray))
} finally {
fos.close
}
}
for (f <- files.filterNot(_.getAbsolutePath.matches(".*(.idea|.svn|=).*")).filter((_.getAbsolutePath.matches(".*\\.(java|xsd|xsl)$")))) {
try {
convert(f)
}
catch {
case e => ("error:" ⊹ f.shows ⊹ e.getMessage).println
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment