IO:

  IO流按操作分为:字节流、字符流。

  所有的数据都是以字节体现的,后期产生了字符流。因为字符数据涉及到了编码问题。所以在字符流对象中加入的编码机制。如果处理的数据都是字符数据,那么可以使用字符流对象来完成。

  IO流按流分为:输入流、输出流。

字符流:

  字符流操作文件数据,读取/写入时需要匹配编码表。

  字符流的抽象基类:Reader、Writer

//简单实例:字符串写入文件
FileWriter fw=new FileWriter(path);//替换指定文件 new FileWriter(path,true);续写指定文件
fw.write(内容);//将数据写入缓存
fw.flush();//刷新缓存中的数据到指定文件
fw.close();//先刷新缓存中的数据到文件,然后释放资源

  写入数据换行符:\r\n(windows);\n(LINUX)

  IO异常(IOException)处理方式

try{ }catch(IOException ex){ }finally{ }

实例:FileReader、FileWriter

//读取文件
FileReader fr=new FileReader("path");

int ch=0;
while((ch=fr.read())!=-1){
  char c=(char)ch;
}
fr.close();
//
char[] buf=new char[1024];//缓冲区的长度;
int len=0;
while((len=fr.read(buf))!=-1){
  new String(buf,0,len);
}

//复制文本文件:先读后写
FileReader fr=new FileReader(path);
FileWriter fw=new FileWriter(corpy-path);

char[] buf=new char[1024];
int len=0;
while((len=fr.read(buf))!=-1){
  fw.write(buf,0,len);
}
fw.close();
fr.close();
View Code

相关文章:

  • 2021-12-23
  • 2021-07-02
  • 2021-12-23
  • 2021-09-27
  • 2021-12-12
  • 2021-09-27
  • 2021-07-27
  • 2021-07-31
猜你喜欢
  • 2021-07-10
  • 2021-11-23
  • 2021-10-16
  • 2021-06-04
  • 2021-11-13
  • 2022-03-09
  • 2021-08-04
相关资源
相似解决方案