为什么会出现字符流


汉字都是负数
编码表

String a=“中国”;
String转byte字节数组用,
byte[] byt=a.getBytes();
byte字节数组转String用,
String s=new String(byt);
字符输出流和字符输入流
字符流写数据的5种方式

OutputStreamWriter osw=
new OutputStreamWriter(new FileOutputStream("C:\\libeauty\\libeauty.txt"));
osw.write('中');
osw.flush();//刷新流
osw.close();//先刷新后释放资源关闭流
也可以写字符数组


可以写字符串


字符流写数据
其实与字节流写数据一样
代码如下

案例字符流复制文件
public class mianji {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("D:\\MyWork\\MyRepository\\libeauty.txt"));
InputStreamReader isr=new InputStreamReader(new FileInputStream("C:\\libeauty\\libeauty.txt"));
int b;
char [] iss=new char[1024];
while((b=isr.read(iss,0,iss.length))!=-1){
osw.write(new String(iss));
// System.out.println(new String(iss));
}
osw.close();
isr.close();
}
}
优化复制文件案例

其实就只是改变了比较长的类名,将OutputStreamWriter换成了FileWriter
将InputStreamReader换成了FileReader
当然后面括号里的new FileOutputStream(字节输出流)也被省略了

字符缓冲流



字符缓冲流复制文件

也可以这样写

字符缓冲流特有功能



字符缓冲输出流的readLine()方法每次只读取一行数据,并不读任何换行符等转义符,他的返回值是String类型的

改为如下即可

总结


集合到文件

结果如下

文件到集合

案例点名器

集合到文件升级
import java.io.*;
import java.util.ArrayList;
public class threeToNine2 {
public static void main(String[] args) throws IOException {
ArrayList<Student> array=new ArrayList<Student>();
Student s1=new Student("001","林青霞",21,"西安");
Student s2 = new Student("002", "张飞", 33, "河南");
Student s3 = new Student("003", "宋江", 40, "河北");
Student s4 = new Student("004", "张飞", 33, "河南");
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
BufferedWriter br=new BufferedWriter(new FileWriter("C:\\libeauty\\student.txt"));
for(Student a:array){
StringBuilder str=new StringBuilder();
str.append(a.getId()).append(',').append(a.getName()).append(',').append(a.getAge()).append(',').append(a.getAddress());
br.write(str.toString());
br.newLine();
}
br.close();
}
}

文件到集合改进版

复制单级文件
import java.io.*;
public class threeToTen2 {
public static void main(String[] args) throws IOException {
File file = new File("C:\\libeauty");
String Srcname = file.getName();
File SrcTofile=new File( "D:\\javaproject",Srcname);
if(!SrcTofile.exists()){
SrcTofile.mkdir();
}
File[] fil = file.listFiles();
for(File os:fil){
System.out.println(os);
String Muname1 = os.getName();
File Mufile= new File(SrcTofile,Muname1);
Copy(Mufile,os);
}
}
private static void Copy(File Mufile,File srcfile) throws IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcfile));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(Mufile));
int len;
byte[] byt = new byte[1024];
while((len=bis.read(byt))!=-1){
bos.write(byt,0,len);
}
bis.close();
bos.close();
}
}
复制多级文件夹
import java.io.*;
public class threeToEleven {
public static void main(String[] args) throws IOException {
File SrcFile = new File("D:\\Mywork");
File ToFile=new File("C:\\libeauty");
CopyFist(SrcFile,ToFile);
}
private static void CopyFist(File srcfile,File tofile ) throws IOException {
//判断数据源是否是目录
if(srcfile.isDirectory()){
//在目的地下创建和数据源名称一样的目录
String srcName = srcfile.getName();
File newToFile=new File(tofile,srcName);
if (!newToFile.exists()){
newToFile.mkdir();
}
//获取数据源下所有文件或目录的File数组
File[] files = srcfile.listFiles();
//遍历数组,得到每一个file对象
for(File f1:files){
//把f1作为数据源file对象,递归调用复制文件夹的方法
CopyFist(f1,newToFile);
}
}else {//如果是文档,直接进行Copy()方法。
File fils=new File(tofile, srcfile.getName());
Copy(fils,srcfile);
}
}
private static void Copy(File Mufile, File srcfile) throws IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcfile));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(Mufile));
int len;
byte[] byt = new byte[1024];
while((len=bis.read(byt))!=-1){
bos.write(byt,0,len);
}
bis.close();
bos.close();
}
}
本文详细介绍了字符流的原理、使用方法,包括String转byte和回溯、字符缓冲流、文件复制以及集合到文件的实例。重点讲解了字符流写数据的不同方式和优化案例,涵盖了字符流复制文件、缓冲流特性和文件到集合操作。
1576

被折叠的 条评论
为什么被折叠?



