怎么使用InputStreamReader
来源:互联网 发布:js怎么设置div的大小 编辑:程序博客网 时间:2024/05/19 23:00
网上介绍:重要的类是InputStreamReader,它是字节转换为字符的桥梁
字节流是什么, 字符流是什么,字节转换为字符不是用char() 强制转换就可以了, 为什么要引进这个类呢?
下面这个程序用字节流处理,也能处理文件
java StreamRecoder UTF-8 UTF-8 D://kk.txt D://kk2.txt
package
import java.io.*;
public class StreamRecoder {
public static void main(String[] args) {
if (args.length < 2) {
System.err.println(
"Usage: java StreamRecoder "
+ "infile_encoding outfile_encoding infile outfile");
return;
}
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
File infile = new File(args[2]);
File outfile = new File(args[3]);
if (outfile.exists( )
&& infile.getCanonicalPath().equals(outfile.getCanonicalPath( ))) {
System.err.println("Can't convert file in place");
return;
}
FileInputStream fin = new FileInputStream(infile);
FileOutputStream fout = new FileOutputStream(outfile);
// isr = new InputStreamReader(fin, args[0]);
// osw = new OutputStreamWriter(fout, args[1]);
while (true) {
int c = fin.read( );
if (c == -1) break; // end of stream
fout.write(c);
}
fout.close( );
fin.close( );
}
catch (IOException ex) {
System.err.println(ex);
ex.printStackTrace( );
}
finally {
if (isr != null) {
try {
isr.close( );
} catch (IOException ex) {
ex.printStackTrace( );
}
}
if (osw != null) {
try {
osw.close( );
}
catch (IOException ex) {
ex.printStackTrace( );
}
}
}
}
}
我如果改成这样,用字符流来处理, 如果kk.txt是中文, 就会出现乱码,
那用字符流处理还有什么用呢
import java.io.*;
public class StreamRecoder {
public static void main(String[] args) {
if (args.length < 2) {
System.err.println(
"Usage: java StreamRecoder "
+ "infile_encoding outfile_encoding infile outfile");
return;
}
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
File infile = new File(args[2]);
File outfile = new File(args[3]);
if (outfile.exists( )
&& infile.getCanonicalPath().equals(outfile.getCanonicalPath( ))) {
System.err.println("Can't convert file in place");
return;
}
FileInputStream fin = new FileInputStream(infile);
FileOutputStream fout = new FileOutputStream(outfile);
isr = new InputStreamReader(fin, args[0]);
osw = new OutputStreamWriter(fout, args[1]);
while (true) {
int c = isr.read( );
if (c == -1) break; // end of stream
osw.write(c);
}
osw.close( );
isr.close( );
}
catch (IOException ex) {
System.err.println(ex);
ex.printStackTrace( );
}
finally {
if (isr != null) {
try {
isr.close( );
} catch (IOException ex) {
ex.printStackTrace( );
}
}
if (osw != null) {
try {
osw.close( );
}
catch (IOException ex) {
ex.printStackTrace( );
}
}
}
}
}
- 怎么使用InputStreamReader
- java 怎么确保输入整型 InputStreamReader BufferedReader
- inputstreamreader
- InputStreamReader
- InputStreamReader
- InputStreamReader
- InputStreamReader
- InputStreamReader
- InputStreamReader
- InputStreamReader
- InputStreamReader
- InputStreamReader
- IO:转换流的使用InputStreamReader
- 转换流OutputStreamWriter和InputStreamReader简单使用
- 转换流的使用(InputStreamReader,OutputStreamWriter)
- 【第二十一颗】流---InputStreamReader和PrintStream使用
- File,FileInputStream,FileReader,InputStreamReader,BufferedReader 的使用和区别
- File,FileInputStream,FileReader,InputStreamReader,BufferedReader 的使用和区别
- 写给毕业之际的自己
- VMware6.5 安装fedora11过程详解与若干问题的解决
- ZedGraph给LineChart添加数值 (转帖)
- Hello cygwin
- Java/JSP中文乱码问题解决心得
- 怎么使用InputStreamReader
- javascript印象
- javascript 浏览器对象结构
- 如何让Oracle表字段自动增长
- ORACLE默认字符集是al32utf8的Q群讨论摘录
- MSN2009登陆时程序崩溃自动退出的解决方法。
- [DirectShow] 010 - Overview of Graph Building
- 关于程序调用Sqlldr不出错但数据导入不成功的问题
- SQL与XML交互