BufferedOutputStream产生了文件,都是怪字符?

samyp1234 2010-03-03 11:11:41
如下的代码片段,执行后,产生了D:/aa.dat 文件

try{
FileOutputStream fo= new FileOutputStream("D:/aa.dat");
BufferedOutputStream bo = new BufferedOutputStream(fo);
byte[] b = new byte[20];
for(int i=0;i<b.length;i++){
b[i] = (byte)i;
out.println(b[i]);
}
bo.write(b);
bo.flush();
fo.close();
}catch(IOException ex){
out.println("没有正确");
ex.printStackTrace();
}

为什么当我用记事本打开 生成的 D:/aa.dat 文件后,看到的却是 Ā̂Ԅ܆ईଊഌ༎ᄐጒ ?

为什么出现了怪字符?
非常谢谢大家啊。
...全文
355 11 打赏 收藏 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
JavaAlpha 2010-03-03
  • 打赏
  • 举报
回复
搂主 可以使用 editplus 打开。

或者是使用其他的二进制编辑器打开。
Z_FEI 2010-03-03
  • 打赏
  • 举报
回复
try{
FileOutputStream fo= new FileOutputStream("D:/aa.dat");
OutputStreamWriter osw = new OutputStreamWriter(fo);
BufferedWriter bfw = new BufferedWriter(osw);
byte[] b = new byte[20];
for(int i=0;i <b.length;i++){
b[i] = (byte)i;
System.out.println(b[i]);
bfw.write(Integer.toString(b[i]));
}
bfw.close();
osw.close();
fo.close();
}catch(IOException ex){
System.out.println("没有正确");
ex.printStackTrace();
}
wang461137703 2010-03-03
  • 打赏
  • 举报
回复
你用的是字节流,是二进制的,用记事本打开当然是乱码。。。。
YangMacgrady 2010-03-03
  • 打赏
  • 举报
回复
楼上某高人解答得太详细了。。。。。
myylmyas1 2010-03-03
  • 打赏
  • 举报
回复
用记事本查看也没有乱码的。
myylmyas1 2010-03-03
  • 打赏
  • 举报
回复
public static void writer(){
try{
FileOutputStream fo= new FileOutputStream("D:/aa.dat");
BufferedWriter bo = new BufferedWriter (new OutputStreamWriter(fo,"UTF-8"));
StringBuffer sb=new StringBuffer();
byte[] b = new byte[20];
for(int i=0;i <b.length;i++){
b[i] = (byte)i;
sb.append(b[i]);

System. out.println(b[i]);
}
bo.write(sb.toString());
bo.flush();
fo.close();
}catch(IOException ex){
System.out.println("没有正确");
ex.printStackTrace();
}
}
看看这个是否是你想要的
冰思雨 2010-03-03
  • 打赏
  • 举报
回复
当然这样也可以:
try{ 
FileOutputStream fo= new FileOutputStream("D:/aa.txt");
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int max = 20;
for(int i=0;i < max;i++){
bo.write((i+"").getBytes());
out.println(i);
}
fo.write(bo.toByteArray());
fo.flush();
fo.close();
}catch(IOException ex){
out.println("没有正确");
ex.printStackTrace();
}
冰思雨 2010-03-03
  • 打赏
  • 举报
回复
或者这样写也行:

try{
FileOutputStream fo= new FileOutputStream("D:/aa.txt");
StringBuilder buff = new StringBuilder();
int max = 20;
for(int i=0;i < max ;i++){
buff.append(i);
out.println(i);
}
fo.write(buff.toString().getBytes());
fo.flush();
fo.close();
}catch(IOException ex){
out.println("没有正确");
ex.printStackTrace();
}
冰思雨 2010-03-03
  • 打赏
  • 举报
回复
应该是忽略了数据类型之间的转换。
基本数据类型 int , byte , char 之间的区别没有弄清楚。

记事本看的是字符不是字节,所以在写入的时候要将数字转换成字符。

OutputStream这个类无论是否有缓存都是针对字节进行操作的。
但楼主要写入的是字符,所以,推荐使用Writer类,因为它针对字符操作。

我手头上没Java编译环境,但对楼主的代码做了一点更改,楼主看看是否有用。

try{ 
FileWriter fo= new FileWriter("D:/aa.txt",false);
BufferedWriter bo = new BufferedWriter(fo);
int max = 20;
for(int i=0;i < max;i++){
bo.write(i+"");
out.println(i);
}
bo.flush();
fo.close();
}catch(IOException ex){
out.println("没有正确");
ex.printStackTrace();
}
哈特中尉 2010-03-03
  • 打赏
  • 举报
回复
(1)字节流:
package IO;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* test.getBytes("iso8859-1"))
* 用OutputStream和InputStream实现文件的写入和读取(字节的写入和读取)
*
*/
public class InAndOut {
String path="e:\\abc.txt";
public static void main(String[] args) {
InAndOut io=new InAndOut ();
io.write("我是邪恶少年!");
io.read();
}
public void write(String content)
{
File f=new File (path);
boolean add=true;//是否写入
try {
if (f.exists()==false) {
f.createNewFile();
FileOutputStream os=new FileOutputStream (f);//不用判断是否写入
//FileOutputStream os=new FileOutputStream (f,add);//要判断是否写入
os.write(content.getBytes());
os.flush();
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void read()
{
try {
InputStream is=new FileInputStream(path);
//BufferedInputStream bis=new BufferedInputStream(new FileInputStream(path));
int temp=0;
String test="";
try {
while ((temp=is.read())!=-1) {
test+=(char)temp;
}
System.out.println(new String (test.getBytes("iso8859-1")));//会出现乱码
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

(2)字符流:
package IO;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
/**
*
*用read和write实现文件的写入和读取(字符的写入和读取)
*
*/
public class ReadAndWrite {
//定义文件的路径,一边写入和读取使用
String path="e:\\abc.txt";

public static void main(String[] args) {
ReadAndWrite r=new ReadAndWrite ();
r.writeFile("我乃邪恶少年是也!");
r.readFile();
}
private void readFile() {
// TODO Auto-generated method stub
try {
Reader r=new BufferedReader (new FileReader(path));
String test="";
int temp=0;
try {
while ((temp=r.read())!=-1) {
test+=(char)temp;
}
System.out.println(test);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void writeFile(String content) {
// TODO Auto-generated method stub
File f=new File (path);
if (f.exists()==false) {
try {
f.createNewFile();
FileWriter fw=new FileWriter (f);
fw.write(content);
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}else {
System.out.println(path+"已存在,写入失败,源文件是");
}
}
}
orangemike 2010-03-03
  • 打赏
  • 举报
回复
我的理解是,你要在aa.dat中输出123456...数列.
你混淆了字节和数字的概念.
实际上数字1在存为文本时,对应的字节值应该为48.
比如你把你的代码改成:

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IO{

public static void main(String[] args){
try{
FileOutputStream fo= new FileOutputStream("D:/aa.dat");
BufferedOutputStream bo = new BufferedOutputStream(fo);
byte[] b = new byte[20];
for(int i=0;i <b.length;i++){
b[i] = (byte)(i+48);
System.out.println(b[i]);
}
bo.write(b);
bo.flush();
fo.close();
}catch(IOException ex){
System.out.println("没有正确");
ex.printStackTrace();
}
}
}

可以看到输出 "0123456789:;<=>?@ABC"
此时0-9是正常输出的,但是10以后的出问题了,因为10在按文本存储的时候实际是两个字符.
所以你最好可以先把数字转为字符串,比如

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class IO{

public static void main(String[] args){
try{
Writer fo= new FileWriter("D:/aa.dat");
BufferedWriter bw = new BufferedWriter(fo);

for(int i=0;i < 20;i++){
bw.append(i+"");
}

bw.flush();
fo.close();
}catch(IOException ex){
System.out.println("没有正确");
ex.printStackTrace();
}
}
}

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧