黑马程序员--Java基础学习之IO流之字节流、字符流、读取写入文件、Copy文件、键盘输入输出、流操作的基本规律

时间:2023-02-25 19:04:44
--------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! -------------------

一、IO流

字节流抽象类: InputStream 、OutputSteam

黑马程序员--Java基础学习之IO流之字节流、字符流、读取写入文件、Copy文件、键盘输入输出、流操作的基本规律

黑马程序员--Java基础学习之IO流之字节流、字符流、读取写入文件、Copy文件、键盘输入输出、流操作的基本规律

字符流抽象类:Reader、Writer

黑马程序员--Java基础学习之IO流之字节流、字符流、读取写入文件、Copy文件、键盘输入输出、流操作的基本规律

黑马程序员--Java基础学习之IO流之字节流、字符流、读取写入文件、Copy文件、键盘输入输出、流操作的基本规律

二、写入或者读取文件(未使用缓冲区)

1、写入文件:

FileWriter fw = new FileWriter("D://test.txt");//在指定目录下创建指定文件,如果已存在,则覆盖
fw.write("asdfa\r\nxiexienihao");//调用write()方法将字符串写入流中
fw.flush();//刷新流对象中的数据,将数据刷新到目的文件中
fw.close();//刷新,并关闭流资源
如果文件存在,不覆盖,并且在源文件后面添加

FileWriter fw = new FileWriter("test.txt",true);

2、读取文件:

FileReader fr = new FileReader("test.txt");
char[] buf = new char[3];//三个三个的读出数据。char[1024] 一次读2k 一个字符是2个字节
int num = fr.read(buf);//读取之后存在buf中,并返回此次读取的个数;没有了返回-1
int num = 0;
while((num=fr.read(buf))!=-1){
SOP(new String(buf,0,num));//有几个就打印几个;此处注意尽量不要使用println 而要使用print
}

三、IO异常处理方式

FileWriter fw = null;
try{
fw = new FileWriter("demo.txt");//如果有异常失败,则对象创建失败
fw.write("asdfd");
}catch(IOException e){
try{
if(fw != null){ //此处应该判断,
fw.close();
}
}catch(IOException e){

}
}
四、copy文件
1、不适用缓冲区技术:

public void copy(){
FileWriter fw = null;
FileReader fr = null;
try{
fw = FileWriter("copy.txt");//要写入的文件
fr = FileReader("yuan.txt");//copy的文件
char ch = new char[1024];//一次copy 2k
int lenth = 0;
while((lenth = fr.read(buf)) != -1){
fw.write(buf,0,lenth);
}
}catch(IOException e){
throw new RuntimeException("读写失败");
}fanally{
if(fr!=null){
try{
fr.close();
}catch(IOException e){

}
}
if(fw!=null){
try{
fw.close();
}catch(IOException e){

}
}
}
}
2、使用缓冲区技术

public void copy(){
BufferedWriter bufw = null;
BufferedReader bufr = null;
try{
bufw = BufferedWriter(new FileWriter("copy.txt"));//要写入的文件
bufr = BufferedReader(new FileReader("yuan.txt"));//copy的文件
String line = null;
while((line = bufr.read(buf)) != null){
bufw.write(line);
bufw.newLine();//自己换行
bufw.flush();
}
}catch(IOException e){
throw new RuntimeException("读写失败");
}fanally{
if(bufr!=null){
try{
bufr.close();
}catch(IOException e){
throw new RuntimeException("读关闭失败");
}
}
if(bufw!=null){
try{
bufw.close();
}catch(IOException e){
throw new RuntimeException("写关闭失败");
}
}
}
}
五、接收从键盘输入的示例
读取键盘输入
System.in;//对应的是标准的输入设备,键盘

System.out;//对应的而是标准输出设备,控制台

System.setIn();//改变标准输入设备

System.setOut();

InputStreamReader//字节通向字符的桥梁

OutputStreamReader//字符转字节

//InputStream in = System.in;//获取键盘输入
//InputStreamReader isr = new InputStreamReader(in);//转换流(字节流转字符流),并且通过BufferedReader装饰可以使用字符流的readLine()
//BufferedReader bufr = new BufferedReader(isr);
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));//一句
String line = null;
while((line=bufr.readLine()) != null){
if(line.isEmpty()){
continue;
}
if("over".equals(line)){
break;
}
SOP(line.toUpperCase());//输出也可以使用输出流表示,如下
}
bufr.close();
六、流操作的基本规律

1、明确源和目的
源:输入流 InputStream Reader
目的:输出流.Outputstream Writer
2、明确错做的数据是否是纯文本
是:字符流
不是:字节流
3、体系明确后,再明确要使用的那个对象
通过设备来区分:
源设备:内存,硬盘,键盘
目的设备:内存,硬盘,键盘


--------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! -------------------