【求助】将字节数组写入文件后用记事本打开是乱码 将一个字节数组通过FileInputStream写到文件中,然后再用记事本打开和直接用String str = new String(byte[]);显示得不一样呢?求助啊!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void main(String[] args) throws IOException { byte[] msg = {13,14,15,10,13}; System.out.println(new String(msg)); File file = new File("D:\\wlt\\tmp", "file1.txt"); if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } FileOutputStream stream = new FileOutputStream(file); stream.write(msg); stream.flush(); stream.close(); } 你写的msg数组里面的信息就是一些特殊的符号,如果换成其他的值就OK,比如下面这个显示 abc ,97对应a的ASCII编码值,你先看看public static void main(String[] args) throws IOException { byte[] msg = {97,98,99}; System.out.println(new String(msg)); File file = new File("D:\\tmp", "file1.txt"); if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } FileOutputStream stream = new FileOutputStream(file); stream.write(msg); stream.flush(); stream.close();} 下面这个例子是写入中文字符串的例子,不知道你还有问题么,你之前数组取的值无法形成正常编码的字符public static void main(String[] args) throws IOException { byte[] a="程序".getBytes(); //for(byte b:a){ //System.out.println("["+b+"]"); //}// byte[] msg = {13,14,15,10,13};// byte[] msg = {97,98,99}; System.out.println(new String(a)); File file = new File("D:\\tmp", "file1.txt"); if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } FileOutputStream stream = new FileOutputStream(file); stream.write(a); stream.flush(); stream.close(); } public static void main(String[] args) throws IOException { String msg = "13,14,15,10,13"; System.out.println(new String(msg)); File file = new File("D:\\wlt\\tmp", "file1.txt"); if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } FileOutputStream stream = new FileOutputStream(file); stream.write(msg.getBytes()); stream.flush(); stream.close(); } stream.write(msg.getBytes("utf-8"),"gbk");如果还是乱码 就变这句试试 为什么不要继承一个实现好的类,而要继承抽象类或接口 Java 数据采集 上传图片求思路例子!!! 请高手说说数组和集合的特点和区别 在java中怎么实现鼠标点哪,图片移向哪????? jar可以转换成exe吗?? tomcate中servlet显示中文乱码,如何解决? 怎样用Java开发opentools 插件 急!分不够在加。。。。 如何在JSP中获取客户端提交文本中的空格 大神帮帮忙 解决不了 关于初始化引用的疑问 求救大虾们,写的复制文件的程序问题到底出在哪里???
public static void main(String[] args) throws IOException {
byte[] msg = {13,14,15,10,13};
System.out.println(new String(msg));
File file = new File("D:\\wlt\\tmp", "file1.txt");
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
FileOutputStream stream = new FileOutputStream(file);
stream.write(msg);
stream.flush();
stream.close();
}
byte[] msg = {97,98,99};
System.out.println(new String(msg));
File file = new File("D:\\tmp", "file1.txt");
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
FileOutputStream stream = new FileOutputStream(file);
stream.write(msg);
stream.flush();
stream.close();
}
byte[] a="程序".getBytes();
//for(byte b:a){
//System.out.println("["+b+"]");
//}
// byte[] msg = {13,14,15,10,13};
// byte[] msg = {97,98,99};
System.out.println(new String(a));
File file = new File("D:\\tmp", "file1.txt");
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
FileOutputStream stream = new FileOutputStream(file);
stream.write(a);
stream.flush();
stream.close();
}
String msg = "13,14,15,10,13";
System.out.println(new String(msg));
File file = new File("D:\\wlt\\tmp", "file1.txt");
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
FileOutputStream stream = new FileOutputStream(file);
stream.write(msg.getBytes());
stream.flush();
stream.close();
}
stream.write(msg.getBytes("utf-8"),"gbk");
如果还是乱码 就变这句试试