IO流之字符流
1、OutputStreamWriter利用字符输出转换流,向文件写入数据
1、不处理异常的情况----抛异常
package com.xx.IO;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Test01 {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("xx.txt"));
char[] cs={'1','2','a','b'};
osw.write(cs);
osw.write(cs, 1, 2);
String str="1234";
osw.write(str);
osw.write(str, 1, 2);
osw.close();
}
}
2、默认编码格式,和自定义编码格式
package com.xx.IO;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Test02 {
public static void main(String[] args) throws IOException {
//1。创建流对象时+末尾追加+默认编码,不设置编码就是默认编码
//OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("xx.txt",true));
//1.创建流对象时+末尾追加+自定义utf-8编码
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("xx.txt",true),"UTF-8");
//2写入数据
osw.write("12345");
//3.关闭资源
osw.close();
}
}
3、处理异常的情况
package com.xx.IO;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class Test03 {
public static void main(String[] args) {
OutputStreamWriter osw =null;
try {
osw = new OutputStreamWriter(new FileOutputStream("xx.txt",true),"GBK");
osw.write("qqweq");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(osw!=null){
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2、InputStreamReader利用字符输入转换流,向文件写入数据
1、不处理异常读取数据----复杂版
package com.xx.IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test04 {
public static void main(String[] args) throws IOException {
//1、创建流对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("xx.txt"));
//2、读取数据,当读到末尾时返回负一,不强转的话读取的时uncode码值,需要强转为字符串
int read = isr.read();
System.out.println((char)read);
read = isr.read();
System.out.println((char)read);
read = isr.read();
System.out.println((char)read);
read = isr.read();
System.out.println((char)read);
read = isr.read();
System.out.println((char)read);
read = isr.read();
System.out.println((char)read);
//关闭资源
isr.close();
}
}
2、读取数据简易版
package com.xx.IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test05 {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("xx.txt"));
int read;
while((read=isr.read())!=-1){
System.out.println((char)read);
}
isr.close();
}
}
3、默认编码格式,和自定义编码格式
package com.xx.IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test06 {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("xx.txt"),"GBK");
char[] cs=new char[1024];
int len;
while((len=isr.read())!=-1){
System.out.println(new String(cs,0,len));
}
isr.close();
}
}
4、处理异常的情况
package com.xx.IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test07 {
public static void main(String[] args) {
InputStreamReader isr=null;
try {
isr = new InputStreamReader(new FileInputStream("xx.txt"));
char[] cs =new char[1024];
int len;
while((len=isr.read())!=-1){
System.out.println(new String(cs, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(isr!=null){
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3、FileWriter利用 文件字符输出流 向文件写入数据
1、案例一
package com.xx.IO;
import java.io.FileWriter;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("hhy.txt",true);
fw.write("123abc木头人");
fw.close();
}
}
2、案例二
package com.xx.IO;
import java.io.FileReader;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("hhy.txt");
char[] cs = new char[1024];
int len;
while((len = fr.read(cs)) != -1){
System.out.println(new String(cs, 0, len));
}
fr.close();
}
}
4、BufferedWriter利用带缓冲区的字符输出流 向文件写入数据
package com.xx.io03;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("hhy.txt",true));
bw.write("123abc木头人");
bw.close();
}
}
5、BufferedReader利用 带缓冲区的字符输入流 读取文件里的数据
package com.xx.io03;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("hhy.txt"));
char[] cs = new char[1024];
int len;
while((len = br.read(cs)) != -1){
System.out.println(new String(cs, 0, len));
}
br.close();
}
}
6、文件的复制
1、FileReader和FileWriter的文件复制
package com.xx.io02;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Copy {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("С˵.txt");
FileWriter fw = new FileWriter("copy.txt");
char[] cs = new char[1024];
int len;
while((len = fr.read(cs)) != -1){
fw.write(cs, 0, len);
}
fr.close();
fw.close();
}
}
2、BufferedReader和BufferedWriter的文件复制
package com.qf.io03;
import java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Copy01 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("С˵.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
char[] cs = new char[1024];
int len;
while((len = br.read(cs)) != -1){
bw.write(cs, 0, len);
}
br.close();
bw.close();
}
}