如何用程序创建一个utf-8格式的文本文件
如题,在线等。
------解决方案--------------------我也很迷惑,一起等
------解决方案-------------------- String utf8str = new String(s.getBytes( "gbk "), "uft8 ");
------解决方案--------------------import java.io.*;
import java.util.*;
public class Test66 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FileOutputStream fos = new FileOutputStream( "d:\\2.txt ");
String s = "12345上山打老虎 ";
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(s.getBytes( "UTF-8 "));
bos.close();
Scanner sc = new Scanner(new FileInputStream( "d:\\2.txt "), "UTF-8 ");
while(sc.hasNextLine())
System.out.println(sc.nextLine());
sc.close();
}
}
------解决方案--------------------没什么用~就是试了试,必须用UTF-8读回来才不乱码~,你改改别的编码试试
------解决方案--------------------import java.io.*;
import java.util.*;
public class Test66 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FileOutputStream fos = new FileOutputStream( "d:\\2.txt ");
String s = "12345上山打老虎 ";
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(s.getBytes( "UTF-8 "));
bos.close();
Scanner sc = new Scanner(new FileInputStream( "d:\\2.txt "), "UTF-8 ");
while(sc.hasNextLine())
System.out.println(sc.nextLine());
sc.close();
}
}
------解决方案--------------------request.setCharacterEncoding( "UTF-8 ");
------解决方案--------------------你用utf-8的编码写进去就是。不过如果打开的时候不指定utf-8编码读出,在系统当前默认非utf-8的情况下读就是乱码