java中基础字符流使用
java中基础字符流使用
java中编码表编码解码
- 中文的编码都是负数
- UTF-8中每个中文字符占3个字节
- GBK中每个中文字符占2个字节
- 编码解码要使用统一的编码表
- 默认使用UTF-8
字符串中编码解码
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class Demo1 {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "中国";
// 使用字符串的getBytes()方法编码为UTF-8字节码
byte[] bytes = s.getBytes("UTF-8");
// UTF-8中每个中文字符占3个字节
// [-28, -72, -83, -27, -101, -67]
System.out.println(Arrays.toString(bytes));
// 使用字符串的getBytes()方法编码为GBK节码
byte[] bytes1 = s.getBytes("GBK");
// GBK中每个中文字符占2个字节
// [-42, -48, -71, -6]
System.out.println(Arrays.toString(bytes1));
// 解码:此时bytes为UTF-8编码,bytes1为GBK编码
// 使用String的构造函数进行解码
String s1 = new String(bytes,"UTF-8");
String s2 = new String(bytes1,"GBK");
System.out.println(s1);
System.out.println(s2);
}
}
字符流中编码解码的基本使用
/*
* 字符流中编码解码
* 从字符流到字节流使用的类:
* InputStreamReader和OutputStreamWriter
* */
import java.io.*;
public class Demo2 {
public static void main(String[] args) throws IOException {
/*
* API:
* OutputStreamWriter (OutputStream out,String charsetName)
* InputStreamReader (InputStream out,String charsetName)
* */
// FileOutputStream fileOutputStream = new FileOutputStream("java基础\\src\\com\\io\\encoding\\test.txt");
// OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "GBK");
// 合并为一步
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("java基础\\src\\com\\io\\encoding\\test.txt"),"GBK");
// 使用该对象的write()写入
osw.write("中国");
// 关闭资源
osw.close();
// 读取
InputStreamReader isr = new InputStreamReader(new FileInputStream("java基础\\src\\com\\io\\encoding\\test.txt"), "GBK");
// 一次读取一个字符数据
int read;
while ((read = isr.read()) != -1){
System.out.print((char)read);
}
// 关闭资源
isr.close();
}
}
字符流写数据的5种方式
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Write {
public static void main(String[] args) throws IOException{
// 1.一次写一个字符
// method1();
// 2.一次写一个字符数组
// method2();
// 3.一次写一个字符数组的一部分
// method3();
// 4.一次写一个字符串
// method4();
// 5.一次写一个字符串的一部分
method5();
}
/*
* 构造函数源码:
* public void write(int c) throws IOException {
se.write(c);
}
/*
* Writes a portion of an array of characters.
*
* @param cbuf Buffer of characters
* @param off Offset from which to start writing characters
* @param len Number of characters to write
*
* @exception IOException If an I/O error occurs
*
public void write(char cbuf[], int off, int len) throws IOException {
se.write(cbuf, off, len);
}
/**
* Writes a portion of a string.
*
* @param str A String
* @param off Offset from which to start writing characters
* @param len Number of characters to write
*
* @exception IOException If an I/O error occurs
*
public void write(String str, int off, int len) throws IOException {
se.write(str, off, len);
}
/**
* Flushes the stream.
*
* @exception IOException If an I/O error occurs
**/
public static void method1() throws IOException{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("java基础\\src\\com\\io\\encoding\\test.txt"));
osw.write(97);
osw.write(98);
osw.write(99);
// 因为字符流写数据会保存到缓冲区中,需要调用刷新流刷新
// 调用刷新流
// osw.flush();
// 使用close会先进行自动刷新
// 但是调用close()后就不能对此流进任何操作了
osw.close();
}
public static void method2() throws IOException{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("java基础\\src\\com\\io\\encoding\\test.txt"));
char[] chars = {'a','b','c','e','f','g'};
osw.write(chars);
osw.close();
}
public static void method3() throws IOException{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("java基础\\src\\com\\io\\encoding\\test.txt"));
char[] chars = {'a','b','c','e','f','g'};
// 从第1个开始,一共写5个
osw.write(chars,0,4);
osw.close();
}
public static void method4() throws IOException{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("java基础\\src\\com\\io\\encoding\\test.txt"));
osw.write("你好");
osw.close();
}
public static void method5() throws IOException{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("java基础\\src\\com\\io\\encoding\\test.txt"));
osw.write("你好",0,1);
osw.close();
}
}
字符流读数据的2种方式
/*
* 字符流读数据的2种方式
* */
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Read {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("java基础\\src\\com\\io\\encoding\\test.txt"));
// 一次读取一个字符数据
// method1(isr);
// 一次读取一个字符数组
method2(isr);
isr.close();
}
// 一次读取一个字符数据
public static void method1(InputStreamReader isr) throws IOException {
int read;
while ((read = isr.read()) != -1){
System.out.print((char) read);
}
}
// 一次读取一个字符数组
public static void method2(InputStreamReader isr) throws IOException{
char[] chars = new char[1024];
int read;
while ((read = isr.read(chars)) != -1){
System.out.print(new String(chars,0,read));
}
}
}
实训:复制java文件
/*
* 将Before.java复制到Copy.java
* */
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
// 数据源
InputStreamReader isr = new InputStreamReader(new FileInputStream("java基础\\src\\com\\io\\encoding\\copy\\Before.java"));
// 目标
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("java基础\\src\\com\\io\\encoding\\copy\\Copy.java"));
// 读取数据源
char[] chars = new char[1024];
int readLength;
while ((readLength = isr.read(chars)) != -1){
// 此时chars为读取的数据
// 将其转换成字符串
String read = new String(chars, 0, readLength);
// 将读取的数据写入目标
osw.write(read);
}
isr.close();
osw.close();
}
}
转换流的名字过于长,我们决定使用他们的子类对上述实训进行重构并学习
缺点是不能编码解码
/*
* 使用子类FileReader和FileWriter
* 将Before.java复制到Copy.java
* 缺点是不能编码解码
* */
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
/*
* 构造函数源码:
* public FileReader(String fileName) throws FileNotFoundException {
super(new FileInputStream(fileName));
}
*
* public FileWriter(String fileName) throws IOException {
super(new FileOutputStream(fileName));
}
* */
// 数据源
FileReader fileReader = new FileReader("java基础\\src\\com\\io\\encoding\\newCopy\\Before.java");
// 目标
FileWriter fileWriter = new FileWriter("java基础\\src\\com\\io\\encoding\\newCopy\\Copy.java");
// 读取数据源
char[] chars = new char[1024];
int readLength;
while ((readLength = fileReader.read(chars)) != -1){
fileWriter.write(chars,0,readLength);
}
// 关闭资源
fileWriter.close();
fileReader.close();
}
}