黑马程序员_java_IO流总结(上)

来源:互联网 发布:阿里云人工在线客服 编辑:程序博客网 时间:2024/05/22 05:11
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------


IO流知识点总结
一、IO流
不同的输入输出设备之间的数据传输抽象表述为“流”,IO流用来处理设备之间的数据传输。Java对数据的操作是通过流的方式。Java用于操作流的对象都在IO包中。


流按操作数据分为两种:字节流与字符流。
字节流又分为输入字节流InputStream、输出字节流OutputStream。
字符流又分为输入字符流Reader、输出字符流Writer。




二、字节流
字节流的概念:IO流中针对字节输入输出提供的一系列的流,统称为字节流。


字节流:
输入流:
InputStream(抽象类)
int read()
int read(byte[] bys)


FileInputStream(常用基本流)


BufferedInputStream
输出流:
OutputStream(抽象类)
write(int by)
write(byte[] bys,int index,int len)


FileOutputStream(常用基本流)


BufferedOutputStream


案例一:
/*
 * 字节输入流操作步骤:
 * A:创建字节输入流对象
 * B:调用读取数据的方式,并显示
 * C:释放资源
 */
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
// 创建字节输入流对象
FileInputStream fis = new FileInputStream("b.txt");


// 调用读取数据的方式,并显示
// 方式1
// int by = 0;
// while ((by = fis.read()) != -1) {
// System.out.println(by);
// }


// 方式2
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
System.out.print(new String(bys, 0, len));
}


// 释放资源
fis.close();
}
}




案例二:
/*
 * 通过字节流往文件中写数据。
 * 
 * 字节输出流操作步骤:
 * A:创建字节输出流对象
 * B:调用写数据的方法
 * C:释放资源
 */
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
// 创建字节输出流对象
// FileOutputStream fos = new FileOutputStream("a.txt",true);
FileOutputStream fos = new FileOutputStream("a.txt");


// 调用写数据的方法
// 写入一个字节
// fos.write(97);
// fos.write(98);
// fos.write(99);
// fos.flush();
// 写一个字节数组
// byte[] bys = { 97, 98, 99, 100, 101 };
byte[] bys = "abcde".getBytes();
// fos.write(bys);
// 写一个字节数组的一部分
fos.write(bys, 0, 2);


// 释放资源
fos.close();
}
}


案例三:
/*
 * 复制MP3,加入异常处理标准格式。
 * 
 * 数据源:
 * d:\\Pink.mp3
 * 目的地:
 * copy.mp3
 */
public class CopyMP3 {
public static void main(String[] args) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream("d:\\Pink.mp3"));
bos = new BufferedOutputStream(new FileOutputStream("copy.mp3"));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}


案例四:
/*
 * BufferedWriter:
 * public void newLine():根据系统平台写入行分隔符
 *
 * BufferedReader:
 * public String readLine():一次读取一行的数据。但是不包含换行符。
 */
public class BufferedStreamDemo {
public static void main(String[] args) throws IOException {
// 写数据
// BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));
// for (int x = 0; x < 5; x++) {
// bw.write("hello" + x);
// // bw.write("\r\n");
// bw.newLine();
// bw.flush();
// }
//
// bw.close();


// 读取数据
BufferedReader br = new BufferedReader(new FileReader("bw.txt"));


/*
String line = br.readLine();
System.out.println(line);
line = br.readLine();
System.out.println(line);
line = br.readLine();
System.out.println(line);
line = br.readLine();
System.out.println(line);
*/

String line = null;
while((line=br.readLine())!=null){
System.out.println(line);
}


br.close();
}
}


三、字符流
字符流的概念:IO流中针对字符输入输出提供的一系列的流,统称为字符流。




字符流:
输入流:
Reader(抽象类)
int read()
int read(char[] chs)




FileReader(常用基本流)




BufferedReader
String readLine()
输出流:
Writer(抽象类)
write(int ch)
write(char[] chs,int index,int len)


FileWriter(常用基本流)


BufferedWriter
write(String line)
void newLine()


案例一:
/*
 * 需求:我要把一句话:"hello,io,我来了。"写入一个test.txt中。
 * 
 * 分析:
 * A:由于我们是写数据,所以使用OutputStream或者Writer。
 * B:由于我们要写入到一个文本文件中,所以最终选择了Writer。
 * 
 * 通过查看API,我们发现,Writer是一个抽象类。那么,怎么办呢?
 * 只能找子类使用。又由于我们是对文件操作,我们猜测了这个子类的名称是:FileWriter
 * 
 * 去看FileWriter的构造方法:
 * A:FileWriter(File file) 
 * B:FileWriter(String fileName)
 */
public class FileWriterDemo {
public static void main(String[] args) throws IOException {
// 创建字符输出流对象
// Writer w = new FileWriter(); // 多态
// File file = new File("a.txt");
// if(!file.exists()){
// file.createNewFile();
// }
// FileWriter fw = new FileWriter(file);


FileWriter fw = new FileWriter("a.txt");
/*
* 创建字符输出流对象做了几件事情? A:调用系统功能创建了文件 B:创建字符输出流对象 C:把字符输出流对象指向创建的文件
*/


// 调用写数据的功能
// public void write(String str)
fw.write("hello,io,我来了。");
/*
* 字符流 1字符 = 2字节 文件数据底层单位是字节,而我们现在是字符,所以它不能直接把数据写入文件。 把字符输出存储到缓冲区里面。
*/
// 刷新缓冲区
// public void flush()
fw.flush();


// 释放资源
// public void close()
fw.close();
/*
* 为什么要close()呢?
* 1:让流对象变成垃圾。
  * 2:通知系统去释放和文件相关的资源。
*/
//code
}
}
案例二:
/*
 * 1:写入数据的方式,有5种。
 * write(int ch)
 * write(char[] chs,int index,int len)
 * write(char[] chs)
 * write(String str,int index,int len)
 * write(String str)
 * 
 * 2:为什么连续两次没有换号呢?如果想换行,怎么办呢?
 * 因为你没有让他换行,所以没有换号。
 * 那就给出换行符不就可以了吗。\n
 * 
 * 不同的操作系统,对换行符的要求不一样:
 * windows:\r\n
 * linux:\n
 * mac:\r
 * 而有些软件,在制作的时候,为了通用,对任何换行符都是可以识别的。
 * 
 * 3:数据没次都把以前的给覆盖了,如果我想追加写入,怎么办?
 * 构造的时候,用带两个参数的。
 * public FileWriter(String fileName,boolean append)
 */
public class FileWriterDemo3 {
public static void main(String[] args) throws IOException {
// 创建字符输出流对象
FileWriter fw = new FileWriter("c.txt",true);


// 写数据
// 一次写一个字符
// fw.write('a');
// fw.write(98);
// 一次写一个字符数组
// char[] chs = { 'a', 'b', 'c', 'd', 'e' };
// fw.write(chs);
// 一次写一个字符数组的一部分
// fw.write(chs, 0, 2);
// 一次写一个字符串
// fw.write("abcde");
// // 一次写一个字符串的一部分
// fw.write("abcde", 0, 3);
// fw.flush();


// 写数据
// fw.write("hello\r\n");
// fw.write("world\r\n");
// fw.write("java\r\n");
// fw.flush();


// 追加数据
fw.write("hello\r\n");
fw.write("world\r\n");
fw.write("java\r\n");
fw.flush();


// 释放资源
fw.close();
}
}
案例三:
/*
 * 加入异常处理的代码
 */
public class FileWriterDemo4 {
public static void main(String[] args) {
// FileWriter fw = null;
// try {
// // System.out.println(10/0);
// fw = new FileWriter("d.txt");
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// try {
// fw.write("d.txt");
// } catch (IOException e) {
// e.printStackTrace();
// }
// try {
// fw.flush();
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// try {
// fw.close();
// } catch (IOException e) {
// e.printStackTrace();
// }


FileWriter fw = null;
try {
fw = new FileWriter("d.txt");
fw.write("hello");
fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
案例四:
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
//封装数据源
FileReader fr = new FileReader("d.txt");
//封装目的地
//要复制到具体的文件
FileWriter fw = new FileWriter("e.txt");
//读取数据
// int i = fr.read();
// System.out.println((char)(i));
// i = fr.read();
// System.out.println((char)(i));
// i = fr.read();
// System.out.println((char)(i));
// i = fr.read();
// System.out.println((char)(i));



char[] chs = new char[3];
int len = 0;
while ((len = fr.read(chs)) != -1) {
//写入数据
System.out.println(len);
System.out.println(new String(chs,0,len));
// fw.write(chs, 0, len);
}
//释放资源
fw.close();
fr.close();
}
}
0 0
原创粉丝点击