*01-IO流(输入流&输出流)
输入输出——设备之间的数据传输。
分类:字节流 和 字符流
分类:输入流 和 输出流
正常的程序运行,会将硬盘的数据,读入到内存当中。这个就叫做输入。
将内存中的数据,存储在硬盘上就叫做输出。
看起来数据是在流动,所以就叫做数据流。
操作流的对象都在IO包里面。
输入——读
输出——写
外围设备:u盘、打印机。
*02-IO流(字节流&字符流)
所有在设备上存储的数据都是以字节形式存储,用于处理字节这种流对象,称作字节流(输入字节流、输出流),它能处理所有的数据。
字符流:是后期出现的,为什么呢?
N种编码表——>国际标准码表Unicode[不论什么字符都用两个字节来表示]
问题:比如中文汉字“中”,如果是在GBK里面存,而拿Unicode码表去取,就会出现错误。
字节流 + 编码表 = 字符流
字符流的由来:
其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表。获取对应的文字。
在对这个文字进行操作。简单说:字节流+编码表
输入流和输出流相对于内存设备而言.
将外设中的数据读取到内存中:输入
将内存的数写入到外设中:输出。
*
*03-IO流(字符流-FileWriter)
字节流——抽象基类,字节流的两个顶层父类:
Inputstream outputstream
字符流——抽象基类,字符流的两个顶层父类:
Reader Writer
就从熟悉的文字开始字符流,
//需求:将一些文字存储到硬盘一个文件中。
记住;如果要操作文字数据,建议优先考虑字符流。
而且要将数据从内存写到硬盘上,要使用字符流中的输出流。Writer
硬盘的数据基本体现是文件。 希望找到一个可以操作文件的Writer.
找到了FileWriter
//需求:将一些文字存储到硬盘一个文件中。
记住;如果要操作文字数据,建议优先考虑字符流。
而且要将数据从内存写到硬盘上,要使用字符流中的输出流。Writer
硬盘的数据基本体现是文件。 希望找到一个可以操作文件的Writer.
找到了FileWriter
如果写到硬盘上,因为硬盘是以文件来存储的,所以找Writer的子类发现
writer
|——outputstreamwriter
|——Filewriter:没有空参的构造函数,参数放置文件的存放路径
如果文件存在,会覆盖。
* 调用Writer对象中的write(string)方法,写入数据。
* 其实数据写入到临时存储缓冲区中。
* 其实数据写入到临时存储缓冲区中。
// fw.write("xixi");
* 进行刷新,将数据直接写到目的地中。
*/
// fw.flush();
*/
// fw.flush();
这样在运行程序就会出现写入的数据了。
这是使用windows的资源,所以用完之后,还要关闭他。
// fw.flush();
/*
* 关闭流,关闭资源。在关闭前会先调用flush刷新缓冲中的数据到目的地。
*/
fw.close();
* 关闭流,关闭资源。在关闭前会先调用flush刷新缓冲中的数据到目的地。
*/
fw.close();
*04-IO流(字符流-FileWriter-细节(换行和续写))
原因,windows的记事本对换行的解析是/r/n
这样的好处是,会调用系统的换行,无论是Linux还是Windows。
如果现在已有文件的末端续写,
FileWriter fw = new FileWriter("demo.txt",true);
fw.write("abcde"+LINE_SEPARATOR+"hahaha");
fw.write("xixi");
package cn.itcast.p2.io.filewriter;
import java.io.FileWriter;
import java.io.IOException;
//需求:将一些文字存储到硬盘一个文件中。
public class FileWriterDemo {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
fw.write("abcde"+LINE_SEPARATOR+"hahaha");
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//创建一个可以往文件中写入字符数据的字符输出流对象。
/*
* 既然是往一个文件中写入文字数据,那么在创建对象时,就必须明确该文件(用于存储数据的目的地)。
*
* 如果文件不存在,则会自动创建。
* 如果文件存在,则会被覆盖。
*
* 如果构造函数中加入true,可以实现对文件进行续写!
*/
FileWriter fw = new FileWriter("demo.txt",true);
/*
* 调用Writer对象中的write(string)方法,写入数据。
*
* 其实数据写入到临时存储缓冲区中。
*
*/
fw.write("abcde"+LINE_SEPARATOR+"hahaha");
// fw.write("xixi");
/*
* 进行刷新,将数据直接写到目的地中。
*/
// fw.flush();
/*
* 关闭流,关闭资源。在关闭前会先调用flush刷新缓冲中的数据到目的地。
*/
fw.close();
// fw.write("haha");// java.io.IOException: Stream closed
}
}
*05-IO流(字符流-FileWriter-IO异常处理)
记住:只要IO操作都要异常处理。
修正:
解释两个异常。
继续修正:
package cn.itcast.p2.io.filewriter;
import java.io.FileWriter;
import java.io.IOException;
public class IOExceptionDemo {
private static final String LINE_SEPARATOR = System
.getProperty("line.separator");
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("k:\\demo.txt");
fw.write("abcde" + LINE_SEPARATOR + "hahaha");
} catch (IOException e) {
System.out.println(e.toString());
} finally {
if (fw != null)
try {
fw.close();
} catch (IOException e) {
// code....
throw new RuntimeException("关闭失败");
}
}
}
}
*06-IO流(字符流-FileReader-读取方式一)
//需求:读取一个文本文件。将读取到的字符打印到控制台.
同上,找到了FileReader
同上,找到了FileReader
package cn.itcast.p3.io.filereader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
//需求:读取一个文本文件。将读取到的字符打印到控制台.
public class FileReaderDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//1,创建读取字符数据的流对象。
/*
* 在创建读取流对象时,必须要明确被读取的文件。一定要确定该文件是存在的。
*
* 用一个读取流关联一个已存在文件。
*/
FileReader fr = new FileReader("demo.txt");
int ch = 0;
while((ch=fr.read())!=-1){
System.out.println((char)ch);
}
/*
//用Reader中的read方法读取字符。
int ch = fr.read();
System.out.println((char)ch);
int ch1 = fr.read();
System.out.println(ch1);
int ch2 = fr.read();
System.out.println(ch2);
*/
fr.close();
}
}
*07-IO流(字符流-FileReader-读取方式二)
第二种:效率高
package cn.itcast.p3.io.filereader;
import java.io.FileReader;
import java.io.IOException;
//需求:读取一个文本文件。将读取到的字符打印到控制台.
public class FileReaderDemo2 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("demo.txt");
/*
* 使用read(char[])读取文本文件数据。
*
* 先创建字符数组。
*/
char[] buf = new char[1024];
int len = 0;
while((len=fr.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
/*
int num = fr.read(buf);//将读取到的字符存储到数组中。
System.out.println(num+":"+new String(buf,0,num));
int num1 = fr.read(buf);//将读取到的字符存储到数组中。
System.out.println(num1+":"+new String(buf,0,num1));
int num2 = fr.read(buf);//将读取到的字符存储到数组中。
System.out.println(num2+":"+new String(buf));
*/
fr.close();
}
}
*08-IO流(字符流-练习-复制文本文件_1)
作业:将c盘的一个文本文件复制到d盘。
分析:
复制原理:
读取c盘文件中的数据,
将这些数据写入到d盘当中。
连读带写。
复制原理:
读取c盘文件中的数据,
将这些数据写入到d盘当中。
连读带写。
使用一次读一个字符的方法:
package cn.itcast.p1.io.charstream.test;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
* 需求:作业:将c盘的一个文本文件复制到d盘。
*
* 思路:
* 1,需要读取源,
* 2,将读到的源数据写入到目的地。
* 3,既然是操作文本数据,使用字符流。
*
*/
public class CopyTextTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//1,读取一个已有的文本文件,使用字符读取流和文件相关联。
FileReader fr = new FileReader("IO流_2.txt");
//2,创建一个目的,用于存储读到数据。
FileWriter fw = new FileWriter("copytext_1.txt");
//3,频繁的读写操作。
int ch = 0;
while((ch=fr.read())!=-1){
fw.write(ch);
}
//4,关闭流资源。
fw.close();
fr.close();
}
}
*09-IO流(字符流-练习-复制文本文件_2)
第二种读取方法:
package cn.itcast.p1.io.charstream.test;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyTextTest_2 {
private static final int BUFFER_SIZE = 1024;
/**
* @param args
*/
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("IO流_2.txt");
fw = new FileWriter("copytest_2.txt");
//创建一个临时容器,用于缓存读取到的字符。
char[] buf = new char[BUFFER_SIZE];//这就是缓冲区。
//定义一个变量记录读取到的字符数,(其实就是往数组里装的字符个数 )
int len = 0;
while((len=fr.read(buf))!=-1){
fw.write(buf, 0, len);
}
} catch (Exception e) {
// System.out.println("读写失败");
throw new RuntimeException("读写失败");
}finally{
if(fw!=null)
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
if(fr!=null)
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
*10-IO流(字符流-练习-复制文本文件_图解)