相关疑难解决方法(0)

设置默认的Java字符编码?

如何以编程方式正确设置JVM(1.5.x)使用​​的默认字符编码?

我已经读过-Dfile.encoding=whatever以前用于旧JVM的方法......由于我不会进入的原因,我没有那么奢侈.

我试过了:

System.setProperty("file.encoding", "UTF-8");
Run Code Online (Sandbox Code Playgroud)

并且属性已设置,但它似乎不会导致下面的最终getBytes调用使用UTF8:

    System.setProperty("file.encoding", "UTF-8");

    byte inbytes[] = new byte[1024];

    FileInputStream fis = new FileInputStream("response.txt");
    fis.read(inbytes);
    FileOutputStream fos = new FileOutputStream("response-2.txt");
    String in = new String(inbytes, "UTF8");
    fos.write(in.getBytes());
Run Code Online (Sandbox Code Playgroud)

java utf-8 character-encoding

342
推荐指数
10
解决办法
62万
查看次数

将编码设置为FileWriter的UTF-8

下面是我的代码,它打算带两个.ckl文件,比较两个,添加新项目并创建一个新的合并文件.程序在Netbeans中运行时正确执行,但是,当执行.jar时,程序似乎没有以UTF-8编码文件.我对编程很陌生,想知道我可能需要在哪里或如何强制执行这种编码?

**我已经删除了Swing代码和其他行,因此只显示了我的方法,即执行所有比较和合并的方法.

public void mergeFiles(File[] files, File mergedFile) {

    ArrayList<String> list = new ArrayList<String>();

    FileWriter fstream = null;
    BufferedWriter out = null;
    try {
        fstream = new FileWriter(mergedFile, false);
        out = new BufferedWriter(fstream);
      } catch (IOException e1) {
        e1.printStackTrace();
    }
    // Going in a different direction. We are using a couple booleans to tell us when we want to copy or not. So at the beginning since we start
    // with our source file we set copy to true, we …
Run Code Online (Sandbox Code Playgroud)

java encoding utf-8

15
推荐指数
3
解决办法
5万
查看次数

标签 统计

java ×2

utf-8 ×2

character-encoding ×1

encoding ×1