例如:
File f = new File("bin\\my.vbs");

if (!f.exists()){
f.createNewFile();
}

String hold = "CreateObject(\"SAPI.SpVoice\").Speak \"" + content
+ "\" ";

FileOutputStream o = new FileOutputStream("bin\\my.vbs");
    o.write(hold.getBytes("GBK"));
    o.close();其中content是中文,我尝试了各种格式,utf-8,GBK,US-ASCLL,都不行。。我肯定文件内容是正确的,应该是编码的问题,求教各位。这个程序该怎么写,才能让生成的程序可以运行?例如:生成一个vbs文件,内容如下:
CreateObject("SAPI.SpVoice").Speak "很差"程序生成的就不能执行,
而自己新建文本文件,然后输入内容,再重命名的就可以执行。

解决方案 »

  1.   

    因为你的文件本身不是相应的编码,默认可能是ascii编码的
    试试看用带编码方式打开文件
    for example
    OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream("test.txt"), "UTF-8");
    String s = "这是测试";
    ow.write(new String(s.getBytes("UTF-8"), "UTF-8"));
    ow.close();