简体   繁体   English

使用 Gradle 进行转码

[英]Transcoding with Gradle

My build.gradle file generates UNIX and Windows launcher scripts for my Java project from templates.我的 build.gradle 文件从项目模板生成 UNIX 和 Windows 启动器脚本。 Templates are UTF-8 encoded and the generated scripts are UTF-8 too.模板是 UTF-8 编码的,生成的脚本也是 UTF-8。 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. 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. 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.)但是我不知道如何将 UTF-8 文件转换为其他代码页(查看了副本,但没有找到指定 output 编码的方法。)

Simply use FileUtils from Apache Commons IO in your build file.只需在构建文件中使用Apache Commons IO中的 FileUtils。

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM