为什么下面代码不能识别汉字?(关于输入输出流)

zhshjin 2003-10-19 05:06:12
import java.util.*;
import java.io.*;

public class Concatenate {
public static void main(String[] args) throws IOException {
ListOfFiles mylist = new ListOfFiles(args);
File outputFile = new File("result.txt");

SequenceInputStream s = new SequenceInputStream(mylist);
FileWriter out = new FileWriter(outputFile);
int c;

while ((c = s.read()) != -1)
out.write(c);

s.close();
out.close();
}
}


class ListOfFiles implements Enumeration {

private String[] listOfFiles;
private int current = 0;

public ListOfFiles(String[] listOfFiles) {
this.listOfFiles = listOfFiles;
}

public boolean hasMoreElements() {
if (current < listOfFiles.length)
return true;
else
return false;
}

public Object nextElement() {
InputStream in = null;

if (!hasMoreElements())
throw new NoSuchElementException("No more files.");
else {
String nextElement = listOfFiles[current];
current++;
try {
in = new FileInputStream(nextElement);
} catch (FileNotFoundException e) {
System.err.println("ListOfFiles: Can't open " + nextElement);
}
}
return in;
}
}

程序是把两个文本的内容连接到一个新建的文本里面
执行的时候
java Concatenate 第一个文件 第二个文件
如果是英文还可以,但是如果是汉字,它就在生成的result.txt中显示乱码
还有下面另外一个几乎一样的代码(两个不同的地方,都已经标出),却能够在系统中显示汉字,这是为什么?是java本身的问题吗?还是编码方式变了?请高手帮忙解答,小弟不胜感激!
import java.util.*;
import java.io.*;

public class Concatenate {
public static void main(String[] args) throws IOException {
ListOfFiles mylist = new ListOfFiles(args);
File outputFile = new File("result.txt");

SequenceInputStream s = new SequenceInputStream(mylist);
FileWriter out = new FileWriter(outputFile);
int c;

while ((c = s.read()) != -1)
System.out.write(c); // 不同之处

s.close();
//out.close(); // 不同之处
}
}


class ListOfFiles implements Enumeration {

private String[] listOfFiles;
private int current = 0;

public ListOfFiles(String[] listOfFiles) {
this.listOfFiles = listOfFiles;
}

public boolean hasMoreElements() {
if (current < listOfFiles.length)
return true;
else
return false;
}

public Object nextElement() {
InputStream in = null;

if (!hasMoreElements())
throw new NoSuchElementException("No more files.");
else {
String nextElement = listOfFiles[current];
current++;
try {
in = new FileInputStream(nextElement);
} catch (FileNotFoundException e) {
System.err.println("ListOfFiles: Can't open " + nextElement);
}
}
return in;
}
}


...全文
98 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
prosong 2003-10-19
  • 打赏
  • 举报
回复
InputStream 的read读取到的是 字节
Write的write方法写入的是 字符
在上面的例子中两者不一致所以出现乱码,
下面的Sytem.out是OutputStream写入的是字节,所以不出现乱码
更改源程序的之一使其一致即可

示例更改如下:
import java.util.*;
import java.io.*;

public class Concatenate {
public static void main(String[] args) throws IOException {
ListOfFiles mylist = new ListOfFiles(args);
File outputFile = new File("result.txt");

SequenceInputStream s = new SequenceInputStream(mylist);
//FileWriter out = new FileWriter(outputFile);
FileOutputStream out = new FileOutputStream(outputFile); // Modified, using OutputStream to write byte
int c;

while ((c = s.read()) != -1)
out.write((byte)c); // Modified, write a byte

s.close();
out.close();
}
}


class ListOfFiles implements Enumeration {

private String[] listOfFiles;
private int current = 0;

public ListOfFiles(String[] listOfFiles) {
this.listOfFiles = listOfFiles;
}

public boolean hasMoreElements() {
if (current < listOfFiles.length)
return true;
else
return false;
}

public Object nextElement() {
InputStream in = null;

if (!hasMoreElements())
throw new NoSuchElementException("No more files.");
else {
String nextElement = listOfFiles[current];
current++;
try {
in = new FileInputStream(nextElement);
} catch (FileNotFoundException e) {
System.err.println("ListOfFiles: Can't open " + nextElement);
}
}
return in;
}
}
imagex 2003-10-19
  • 打赏
  • 举报
回复
File f = new File("E:\\eclipse\\workspace\\box\\Data\\2003-3-7-100.inc");

InputStreamReader read = new InputStreamReader (new FileInputStream(f),"UTF-8");

BufferedReader reader=new BufferedReader(read);

String line;

while ((line = reader.readLine()) != null) {

System.out.println(line);

}
这样可以读取有中文的文件
loveyousomuch 2003-10-19
  • 打赏
  • 举报
回复
inputStream和outputStream是用来处理字节码的流,不能用来处理汉字,
Reader和Writer是用来处理unicede码的,可以处理汉字
    本课程分享对由中国开发者提供的OpenCV条形码识别模块的原理和代码精讲。该模块借鉴“目标识别”领域先进理念,采用“定位-识别”二段模式,有效提高了自然环境下条码识别的准确率并保持了C++代码的高速度,相比较常用的zxing和zbar在准确率和识别速度上均有较大优势。更难得可贵的是在代码的实现过程中能够注意细节,在诸如“循环测试确定参数数值”“积分图的使用”“倾斜矩形纠偏”等处,均提供了思路清晰、弹性高的优质代码。作为一套通过了OpenCV官方的代码检验、解决一个常用领域内具体问题的模块,对于图像处理学习来说是难得可贵的。    逐条进行代码解读繁琐且低效,个人认为,在图像处理领域,能够运行和修改观察的代码对于学习研究至关重要。在理论剖析部分,也是由应用引导原理。同时做好知识的迁移和代码的复用工作。在这个过程中,创建针对性的实验非常重要。 课程内容分为4个部分:一是基本配置,包括· 条形码识别模块的安装使用 (cmake配置和OpenCV编译);· 构建用于测试和代码阅读的环境;· 模块对官方数据集的测试;二是条码定位,将详细讲解思路、原理和实现知识迁移部分将简单说一下在毛发识别上的迁移:三是条码识别,该部分内容会首先梳理框架,而后具体进行分析讲解    此外,我们将结合例子,将OpenCV的基础功能,包括积分图像、形态学变化、联通区域、透视变化等进行复习,加深理解。 

62,612

社区成员

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

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