0

My build.gradle file generates UNIX and Windows launcher scripts for my Java project from templates. Templates are UTF-8 encoded and the generated scripts are UTF-8 too. It's not a problem on Linux where UTF-8 support is ubiquitous, but Windows has some issues displaying non Latin-1 characters in cmd.exe terminal window. After reading Using UTF-8 Encoding (CHCP 65001) in Command Prompt / Windows Powershell (Windows 10) I come to a conclusion that converting the generated UTF-8 script to cp1250 (in my case) would save me lots of trouble when displaying hungarian text. However I couldn't figure out how to convert a UTF-8 file to other code page (looked at copy, but didn't find a way to specify output encoding.)

ToYonos
  • 16,469
  • 2
  • 54
  • 70
  • I believe Windows Terminal supports better encodings, that is, as a replacement for `cmd`. – loa_in_ Mar 02 '21 at 17:38
  • @loa_in_ Installing additional software just for displaying accented characters is overkill I cannot sell to customer. cmd can do this for non unicode charsets. I can convert the bat file by hand and it works, but I want to automate this step inside my Gradle build. – Benjámin Budai Mar 02 '21 at 18:48
  • I believe calling the issue "just displaying accented characters" is your problem. You're in effect fighting internationalization and localization. Sell the solution to that instead. Also Windows Terminal is an official Microsoft product so that should make it easier. – loa_in_ Mar 03 '21 at 12:32

1 Answers1

0

Simply use FileUtils from Apache Commons IO in your build file.

import org.apache.commons.io.FileUtils

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath("commons-io:commons-io:2.8.0")
    }
}

And then, in the relevant part of the script, where launcher scripts are generated :

File f =  file('/path/to/windows-launcher')
// Reading the content as UTF-8
String content = FileUtils.readFileToString(f, 'UTF-8')
// Rewriting the file as cp1250
FileUtils.write(f, content, "cp1250")
ToYonos
  • 16,469
  • 2
  • 54
  • 70