Javaday21 字符流出现的原因 转换流
一 字符流出现的原因及编码表概况
由于字节流操作中文不是特别方便,所以java提供了字符流。
1.编码: 就是把字符串转换成字节数组
把一个字符串转换成一个字节数组
public byte[] getBytes();使用平台的默认字符集将此 String编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
public byte[] getBytes(String charsetName) 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
2.解码: 把字节数组转换成字符串
public String(byte[] bytes): 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
public String(byte[] bytes, String charsetName) 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。
public class MyTest {
public static void main(String[] args) throws UnsupportedEncodingException {
//编码:把字符串转换成字节数组,把看的懂的变成看不懂的
String str="今天晚上老地方见";
byte[] bytes = str.getBytes(); //采用默认的码表进行编码
//解码;把字节数组转换成字符串 把看不懂的,变成看的懂的
String s = new String(bytes);//采用 默认的码表进行解码
System.out.println(s);
//可以指定码表进行编解码
String str2="你家我家不如如家";
byte[] bytes1 = str2.getBytes("utf-8");
//解码
String s1 = new String(bytes1,"utf-8");
System.out.println(s1);
//乱码的原因:编解码 用的不是同一个码表
}
}
二 字符流的5种写数据的方式
1.方法概述
public void write(int c) 写一个字符
public void write(char[] cbuf) 写一个字符数组
public void write(char[] cbuf,int off,int len) 写一个字符数组的 一部分
public void write(String str) 写一个字符串
public void write(String str,int off,int len) 写一个字符串的一部分
OutputStreamWriter out = out=new OutputStreamWriter(new FileOutputStream("a.txt",true),"utf-8");
//往所关联的文件中写数据
out.write('你'); //一次写一个字符
out.write("\r\n");
out.write("今天是大暑,注意放暑假降温");//一次写入一个字符串
out.write("\r\n");
out.write("今天是大暑,注意放暑假降温",0,5);//一次写入字符串的一部分
out.write("\r\n");
out.write(new char[]{'a','你','好'});//一次写入一个字符数组
out.write("\r\n");
out.write(new char[]{'a', '你', '好'},1,2);//一次写入一个字符数组的一部分
//释放资源
out.close();
}
三 字符流的2种读取数据的方式
方法概述
public int read() 一次读取一个字符
public int read(char[] cbuf) 一次读取一个字符数组 如果没有读到 返回-1
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
char[] chars=new char[1024];//字符数组,充当缓冲区
int len = in.read(chars); //返回值,是实际读取到的字符个数
System.out.println(len);
//把字符数组转换成字符串
//String trim = new String(chars,0,len);
//System.out.println(trim);
String s = String.valueOf(chars);
System.out.println(s);
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
char[] chars = new char[1024];//字符数组,充当缓冲区
int len = in.read(chars, 0, 3); //一次读取3个字符,装入缓冲区中
System.out.println(len);
String s = String.valueOf(chars, 0, len);
System.out.println(s);
System.out.println("--------------------------------");
in.close();
}
四 字符流复制文本文件
public static void main(String[] args) throws IOException {
//采用字符流,来复制,文本文件
InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("MyTest2.java"));
//读一个字符,写一个字符来复制
int len=0;//用来记录你读取到的那个字符
while ((len=in.read())!=-1){
out.write(len);
out.flush();//字符流记得刷新一下
}
public static void main(String[] args) throws IOException {
//一次读取一个字符数组,写出一个字符数组 推荐 使用
InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("MyTest2.java"));
char[] chars = new char[1000];
int len=0;//读取到有效字符个数
while ((len=in.read(chars))!=-1){
out.write(chars,0,len);
out.flush();
}
in.close();
out.close();
}
五 FileWriter和FileReader复制文本文件
public static void main(String[] args) throws IOException {
FileReader in = new FileReader("src/MyTest.java");
FileWriter out = new FileWriter("MyTest3.java");
char[] chars=new char[1000];
int len=0;
while ((len=in.read(chars))!=-1){
//System.out.println("读取的次数"+len);
out.write(chars,0,len);
out.flush();
}
in.close();
out.close();
六 字符缓冲流的特殊功能
- 字符缓冲流的特殊功能
BufferedWriter: public void newLine():根据系统来决定换行符 具有系统兼容性的换行符
BufferedReader: public String readLine():一次读取一行数据 是以换行符为标记的 读到换行符就 换行 没读到数据返回null
public static void main(String[] args) throws IOException {
//高效的字符流
//BufferedReader
// BufferedWriter
// BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
//
// 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。
BufferedReader bfr = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bfw = new BufferedWriter(new FileWriter("aa.txt"));
char[] chars = new char[1024];
int len = 0;
while ((len=bfr.read(chars)) != -1) {
bfw.write(chars, 0, len);
bfw.flush();
}
bfr.close();
bfw.close();
}
}
public static void main(String[] args) throws IOException {
BufferedReader bfr = new BufferedReader(new FileReader("回顾.java"));
BufferedWriter bfw = new BufferedWriter(new FileWriter("aaa.java"));
//BufferedReader 他有一个特有的方法,readLine() 一次读取一行内容
//bfr.readLine()
// bfw.write("\r\n");
//bfw.newLine(); //写一个换行符
//读取一行,写出一行来复制
String line=null;
while ((line=bfr.readLine())!=null){
bfw.write(line);
bfw.newLine(); //写出一个换行符,具有平台兼容性
bfw.flush();
}
bfr.close();
bfw.close();
}
}
七 把集合中的数据存储到文本文件当中
public static void main(String[] args) throws IOException {
//需求:把ArrayList集合中的字符串数据存储到文本文件
ArrayList<String> list = new ArrayList<>();
list.add("张飞");
list.add("赵云");
list.add("马超");
list.add("黄忠");
list.add("关羽");
FileWriter writer = new FileWriter("username.txt");
for (String s : list) {
writer.write(s);
writer.write("\r\n");
writer.flush();
}
writer.close();
八 把文本文件中的数据存储到集合当中
public static void main(String[] args) throws IOException {
//需求:从文本文件中读取数据(每一行为一个字符串数据) 到集合中,并遍历集合
//分析:
ArrayList<String> list = new ArrayList<>();
BufferedReader bfr = new BufferedReader(new FileReader("username.txt"));
String line = null;
while ((line = bfr.readLine()) != null) {
list.add(line);
}
for (String s : list) {
System.out.println(s);
}
}
}
九 随机获取文本文件中的姓名
ublic static void main(String[] args) throws IOException {
//点名器
ArrayList<String> list = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("username.txt")));
//读取一行数据,往集合里面添加一个
String line=null;
while ((line=reader.readLine())!=null){
list.add(line);
}
//随机从集合里面抽取一个人
Random random = new Random();
int index = random.nextInt(list.size());
String s = list.get(index);
System.out.println(s);
//数据跟程序解耦
}
}
十 复制单级文件夹
public class Work {
public static void main(String[] args) throws IOException {
File file = new File("C:\\text");
File file1 = new File("D:\\text");
if (file1.mkdirs()){
System.out.println("目标文档创建成功");
}
else{
System.out.println("目标文档已存在");
}
File[] files = file.listFiles();
for(File f:files){
if (f.isFile()){
File file2 = new File(file1,f.getName());
CopyFile(f,file2);
System.out.println(f.getName()+"复制成功");
}else{
File file2 = new File("f");
}
}
}
private static void CopyFile(File f,File f2) throws IOException {
BufferedInputStream bin = new BufferedInputStream(new FileInputStream("f"));
BufferedOutputStream bot = new BufferedOutputStream(new FileOutputStream("f2"));
byte[] b = new byte[1024];
int len = 0;
while ((len=bin.read(b))!=-1){
bot.write(b,0,len);
bot.flush();
}
bin.close();
bot.close();
}
}