Java字节流和字符流

时间:2023-03-09 02:38:30
Java字节流和字符流

file.txt文本中存储的内容:

好abc

1.字符流处理:

package com.wjy.java;

import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException; class Test {
public static void main(String[] args){
int byteEx;
try{
FileReader inputStream=new FileReader("./file/file.txt");
while((byteEx=inputStream.read())!=-){
System.out.println(byteEx);
} }catch(IOException e){
e.printStackTrace();
}
}
}

结果:

22909                      //汉字"好"的结果

2.字节流处理:

package com.wjy.java;

import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException; class Test {
public static void main(String[] args){
int byteEx;
try{
FileInputStream inputStream=new FileInputStream("./file/file.txt");
while((byteEx=inputStream.read())!=-){
System.out.println(byteEx);
} }catch(IOException e){
e.printStackTrace();
}
}
}

结果:

186                                  //汉字"好"的第一部分
195 //汉字”好“的第二部分