黑马程序员_JAVA之IO流的(字节流与字符流)
来源:互联网 发布:tb复刻比较好的淘宝店 编辑:程序博客网 时间:2024/05/21 22:40
------- android培训、java培训、期待与您交流!----------
IO流(字节流与字符流)
一.IO流概述:
1)IO流是用来处理设备之间的数据传输
两种方式:上传文件和下载文件
Java对数据的操作是通过流的方式
Java用于操作流的对象都在IO流包中
2)IO按照流向分:
输出流:Input
输入流:Output
3)IO按照数据类型分:
字节流:可以传输任意类型的数据
字符流:只可以传输字符(字符串,一切文本,我们能看懂的)
4)IO流包下的字节流与字符流
字节输出流抽象基类,用于写文件
java.io.OutputStream
实现子类:FileOutputStream
字节输入流抽象基类,用于读取文件
java.io.InputStream
实现子类:FileInputStream
字符输出流抽象基类,用于写文本文件
java.io.Writer(FileWrtter)
字符输入流抽象基类,用于读取文本文件
java.io.Reader(FileReader)
二.FileOutputStream:
OutputStream:此抽象类是表示输出字节流的所有类的超类
FileOutputStream: 向文件输出的OutputStream
构造方法:
public FileOutputStream(File file) throws FileNotFoundException 指定文件创建文件输出流对象
public FileOutputStream(String name) throws FileNotFoundException 指定文件创建文件输出流对象
普通方法:
public void write(int b) throws IOException 将指定字节写入此文件输出流, 即 一次输出一个字节
public void close() throws IOException 关闭此文件输出流并释放与此流有关的所有系统资源.此文件输出流不能再用于写入字节.
注意:
每次的IO资源一定关闭,用于回收
代码演示:
[java] view plaincopy
package cn.itcast2;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo2 {
public static void main(String[] args) {
//创建IO对象
FileOutputStream fos = null;
try {
fos = new FileOutputStream("a.txt");
//写出数据
fos.write(97);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos!=null) {
//关闭流
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// fos.write(98);
}
}
FileOutputStream:向文件输出的OutputStream
其他输出方法:
public void write(byte[] b) throws IOException 一次输出一个字节数组
public void write(byte[] b,int off,int len) throws IOException 一次输出一个字节数组的一部分
b:要输出的字节数组
off:偏移量:从哪开始 len:长度,多少个字节
输出回车就是输出:\r\n (windows当中的回车换行)
代码演示:
[java] view plaincopy
package cn.itcast2;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo3 {
public static void main(String[] args) throws IOException {
//创建IO流对象
FileOutputStream fos = new FileOutputStream("a.txt");
//写出或者读入
//准备数据
// byte[] bytes = new byte[]{97,98,99};
byte[] bytes = new byte[]{'a','b','c'};
fos.write(bytes);
fos.write('\r');
fos.write('\n');
fos.write(bytes,0,2);
//关闭流
fos.close();
}
}
三.输入流FileInputStream:
InputStream此抽象类是表示字节输入流的所有类的超类
FileInputStream:文件输入流
构造方法:
public FileInputStream(String name) throws FileNotFoundException
读取方法:
public int read() throws IOException 一次读取一个字节
public int read(byte[] b) throws IOException 一次读取一个字节数组
b:读到的内容存放的地方 返回值:读取到的字节个数,如果到达文件结尾返回-1
代码演示:
[java] view plaincopy
package cn.itcast2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demo4 {
public static void main(String[] args) throws IOException {
// method();
method2();
}
//一次一个字节
public static void method() throws FileNotFoundException, IOException {
//创建流对象
FileInputStream fis = new FileInputStream("a.txt");
//输入数据
//定义变量,临时记录读取到的这个字节
int c;
while((c=fis.read())!=-1) {
System.out.println((char)c);
}
//关闭流
fis.close();
}
//一次一个字节数组
public static void method2() throws IOException {
//创建流对象
FileInputStream fis = new FileInputStream("a.txt");
//输入数据
byte[] bytes = new byte[1024];
//定义变量,临时记录读取到的字节的个数
int len;
while((len=fis.read(bytes))!=-1) {
String s = new String(bytes,0,len);
// String s = new String(bytes);
System.out.print(s);
}
//关闭流
fis.close();
}
}
注意事项:
文件名可以没有扩展名,文件夹可以有扩展名.文件与文件夹互不能同名
使用(File file)与(String name)参数的构造方法,写出是覆盖操作
使用(File file,boolean append)与(String name,boolean append)参数的构造方法,写出是追加操作
public FileOutputStream(File file, boolean append) throws FileNotFoundException
append:是否追加数据的开关
使用一次一个字节的方式,无法直接输出中文.可以使用一次一个字节数组的方式
如果写出目标的这个文件不存在,则在写出时,直接创建一个文件,再写出
代码演示:
[java] view plaincopy
package cn.itcast2;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo5 {
public static void main(String[] args) throws IOException {
//输出流如何追加内容
FileOutputStream fos = new FileOutputStream("a.txt",true);
//写出数据
fos.write('中'); //一次输出一个字节无法输出中文,只能输出一半
//准备数据
byte[] bytes = "中".getBytes();
fos.write(bytes);
//关闭流
fos.close();
}
}
高效流:
BufferedOutputStream 高效字节输出流
BufferedInputStream 高效字节输入流
高效流的构造方法:
高效流的构造方法参数,不是文件,而是对应的普通流
BufferedOutputStream(OutputStream out)
BufferedInputStream(InputStream in)
高效字节流的普通方法与普通字节流的普通方法基本相同
代码演示:
[java] view plaincopy
package cn.itcast2;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Demo7 {
public static void main(String[] args) throws IOException {
//创建普通流对象
InputStream is = new FileInputStream("a.txt");
OutputStream os = new FileOutputStream("b.txt");
//使用普通流对象创建高效流对象
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os);
//读
byte[] bytes = new byte[1024];
int len;
while((len=bis.read(bytes))!=-1) {
//写
bos.write(bytes, 0, len);
}
//只需关闭高效流即可
bos.close();
bis.close();
}
}
四.字符流FileWriter:
Writer:写入字符流的抽象类
FileWriter:用来写入字符文件的便捷类
构造方法:
public FileWriter(String fileName) throws IOException
普通方法:
public void write(int c) throws IOException 一次写出一个字符
public void write(char[] cbuf, int off,int len) throws IOException 一次写出一个字符数组的一部分
public void write(String str) throws IOException 一次写出一个字符串
public void write(String str, int off, int len) throws IOException 一次写出一个字符串的一部分
代码演示:
[java] view plaincopy
package cn.itcast2;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Demo {
public static void main(String[] args) throws IOException {
//创建IO流对象
Writer writer = new FileWriter("a.mp3");
//写出
char c = 'a';
writer.write(c);
char[] arr = new char[]{'a','c','z'};
writer.write(arr, 0, 2);
String s = "i love java";
writer.write(s);
//关闭流
writer.close();
}
}
刷新缓冲区:
public void flush() throws IOException 刷新此输出流并强制写出所有缓冲的输出数据
带缓冲区的输出流,在写到缓冲区数据之后,还需要刷新缓冲区,将内容从缓冲区刷新到文件中
高效流与字符流均有缓冲区
普通字节流没有缓冲区,不用flush
五.字符输入流FileReader:
Reader用于读取字符流的抽象类
FileReader:文件字符输出流
普通方法:
public int read() throws IOException 一次读取一个字符
public int read(char[] chars) throws IOException 一次读取一个字符数组
代码演示:
[java] view plaincopy
<strong>package cn.itcast2;
import java.io.FileReader;
import java.io.IOException;
public class Demo3 {
public static void main(String[] args) throws IOException {
// method();
method2();
}
public static void method2() throws IOException {
FileReader fr = new FileReader("b.txt");
char[] chars = new char[1024];
int len;
while((len=fr.read(chars))!=-1) {
//char类型的数组比较特殊,会将该数组中的所有内容打印出来
// System.out.println(chars);
System.out.println(new String(chars, 0, len));
}
fr.close();
}
public static void method() throws IOException {
FileReader fr = new FileReader("b.txt");
int c;
while((c=fr.read())!=-1) {
System.out.print((char)c);
}
fr.close();
}
}</strong>
高效字符流:
BufferedWriter:
public BufferedWriter(Writer out)
public void newLine() throws IOException 写入一个行分隔符
BufferedReader:
public BufferedReader(Reader in)
public String readLine() throws IOException 读取一个文本行
返回的字符串不包括回车换行,但是回车换行是返回一行的标志
高效流:
构造方法传入的不是文件,而是对应的流对象
代码演示:
[java] view plaincopy
<strong>package cn.itcast2;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo4 {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("f.txt"));
String ls = System.getProperty("line.separator");
bw.write('a');
// bw.write("\r\n");
// bw.write(ls);
bw.newLine();
bw.write("i love java".toCharArray());
// bw.write("\r\n");
// bw.write(ls);
bw.newLine();
bw.write("i like java");
bw.close();
BufferedReader br = new BufferedReader(new FileReader("f.txt"));
//定义字符串,临时记录每次读到的每行的字符串
String line;
while((line=br.readLine())!=null) {
System.out.println(line);
}
br.close();
File srcFile = new File("f.txt");
File destFile = new File("g.txt");
method(srcFile,destFile);
}
//使用高效字符流一次复制一行
public static void method(File src,File dest) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(src));
BufferedWriter bw = new BufferedWriter(new FileWriter(dest));
String line;
while((line=br.readLine())!=null) {
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
}
}
</strong>
版权声明:本文为博主原创文章,未经博主允许不得转载。
IO流(字节流与字符流)
一.IO流概述:
1)IO流是用来处理设备之间的数据传输
两种方式:上传文件和下载文件
Java对数据的操作是通过流的方式
Java用于操作流的对象都在IO流包中
2)IO按照流向分:
输出流:Input
输入流:Output
3)IO按照数据类型分:
字节流:可以传输任意类型的数据
字符流:只可以传输字符(字符串,一切文本,我们能看懂的)
4)IO流包下的字节流与字符流
字节输出流抽象基类,用于写文件
java.io.OutputStream
实现子类:FileOutputStream
字节输入流抽象基类,用于读取文件
java.io.InputStream
实现子类:FileInputStream
字符输出流抽象基类,用于写文本文件
java.io.Writer(FileWrtter)
字符输入流抽象基类,用于读取文本文件
java.io.Reader(FileReader)
二.FileOutputStream:
OutputStream:此抽象类是表示输出字节流的所有类的超类
FileOutputStream: 向文件输出的OutputStream
构造方法:
public FileOutputStream(File file) throws FileNotFoundException 指定文件创建文件输出流对象
public FileOutputStream(String name) throws FileNotFoundException 指定文件创建文件输出流对象
普通方法:
public void write(int b) throws IOException 将指定字节写入此文件输出流, 即 一次输出一个字节
public void close() throws IOException 关闭此文件输出流并释放与此流有关的所有系统资源.此文件输出流不能再用于写入字节.
注意:
每次的IO资源一定关闭,用于回收
代码演示:
[java] view plaincopy
package cn.itcast2;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo2 {
public static void main(String[] args) {
//创建IO对象
FileOutputStream fos = null;
try {
fos = new FileOutputStream("a.txt");
//写出数据
fos.write(97);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos!=null) {
//关闭流
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// fos.write(98);
}
}
FileOutputStream:向文件输出的OutputStream
其他输出方法:
public void write(byte[] b) throws IOException 一次输出一个字节数组
public void write(byte[] b,int off,int len) throws IOException 一次输出一个字节数组的一部分
b:要输出的字节数组
off:偏移量:从哪开始 len:长度,多少个字节
输出回车就是输出:\r\n (windows当中的回车换行)
代码演示:
[java] view plaincopy
package cn.itcast2;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo3 {
public static void main(String[] args) throws IOException {
//创建IO流对象
FileOutputStream fos = new FileOutputStream("a.txt");
//写出或者读入
//准备数据
// byte[] bytes = new byte[]{97,98,99};
byte[] bytes = new byte[]{'a','b','c'};
fos.write(bytes);
fos.write('\r');
fos.write('\n');
fos.write(bytes,0,2);
//关闭流
fos.close();
}
}
三.输入流FileInputStream:
InputStream此抽象类是表示字节输入流的所有类的超类
FileInputStream:文件输入流
构造方法:
public FileInputStream(String name) throws FileNotFoundException
读取方法:
public int read() throws IOException 一次读取一个字节
public int read(byte[] b) throws IOException 一次读取一个字节数组
b:读到的内容存放的地方 返回值:读取到的字节个数,如果到达文件结尾返回-1
代码演示:
[java] view plaincopy
package cn.itcast2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demo4 {
public static void main(String[] args) throws IOException {
// method();
method2();
}
//一次一个字节
public static void method() throws FileNotFoundException, IOException {
//创建流对象
FileInputStream fis = new FileInputStream("a.txt");
//输入数据
//定义变量,临时记录读取到的这个字节
int c;
while((c=fis.read())!=-1) {
System.out.println((char)c);
}
//关闭流
fis.close();
}
//一次一个字节数组
public static void method2() throws IOException {
//创建流对象
FileInputStream fis = new FileInputStream("a.txt");
//输入数据
byte[] bytes = new byte[1024];
//定义变量,临时记录读取到的字节的个数
int len;
while((len=fis.read(bytes))!=-1) {
String s = new String(bytes,0,len);
// String s = new String(bytes);
System.out.print(s);
}
//关闭流
fis.close();
}
}
注意事项:
文件名可以没有扩展名,文件夹可以有扩展名.文件与文件夹互不能同名
使用(File file)与(String name)参数的构造方法,写出是覆盖操作
使用(File file,boolean append)与(String name,boolean append)参数的构造方法,写出是追加操作
public FileOutputStream(File file, boolean append) throws FileNotFoundException
append:是否追加数据的开关
使用一次一个字节的方式,无法直接输出中文.可以使用一次一个字节数组的方式
如果写出目标的这个文件不存在,则在写出时,直接创建一个文件,再写出
代码演示:
[java] view plaincopy
package cn.itcast2;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo5 {
public static void main(String[] args) throws IOException {
//输出流如何追加内容
FileOutputStream fos = new FileOutputStream("a.txt",true);
//写出数据
fos.write('中'); //一次输出一个字节无法输出中文,只能输出一半
//准备数据
byte[] bytes = "中".getBytes();
fos.write(bytes);
//关闭流
fos.close();
}
}
高效流:
BufferedOutputStream 高效字节输出流
BufferedInputStream 高效字节输入流
高效流的构造方法:
高效流的构造方法参数,不是文件,而是对应的普通流
BufferedOutputStream(OutputStream out)
BufferedInputStream(InputStream in)
高效字节流的普通方法与普通字节流的普通方法基本相同
代码演示:
[java] view plaincopy
package cn.itcast2;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Demo7 {
public static void main(String[] args) throws IOException {
//创建普通流对象
InputStream is = new FileInputStream("a.txt");
OutputStream os = new FileOutputStream("b.txt");
//使用普通流对象创建高效流对象
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os);
//读
byte[] bytes = new byte[1024];
int len;
while((len=bis.read(bytes))!=-1) {
//写
bos.write(bytes, 0, len);
}
//只需关闭高效流即可
bos.close();
bis.close();
}
}
四.字符流FileWriter:
Writer:写入字符流的抽象类
FileWriter:用来写入字符文件的便捷类
构造方法:
public FileWriter(String fileName) throws IOException
普通方法:
public void write(int c) throws IOException 一次写出一个字符
public void write(char[] cbuf, int off,int len) throws IOException 一次写出一个字符数组的一部分
public void write(String str) throws IOException 一次写出一个字符串
public void write(String str, int off, int len) throws IOException 一次写出一个字符串的一部分
代码演示:
[java] view plaincopy
package cn.itcast2;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Demo {
public static void main(String[] args) throws IOException {
//创建IO流对象
Writer writer = new FileWriter("a.mp3");
//写出
char c = 'a';
writer.write(c);
char[] arr = new char[]{'a','c','z'};
writer.write(arr, 0, 2);
String s = "i love java";
writer.write(s);
//关闭流
writer.close();
}
}
刷新缓冲区:
public void flush() throws IOException 刷新此输出流并强制写出所有缓冲的输出数据
带缓冲区的输出流,在写到缓冲区数据之后,还需要刷新缓冲区,将内容从缓冲区刷新到文件中
高效流与字符流均有缓冲区
普通字节流没有缓冲区,不用flush
五.字符输入流FileReader:
Reader用于读取字符流的抽象类
FileReader:文件字符输出流
普通方法:
public int read() throws IOException 一次读取一个字符
public int read(char[] chars) throws IOException 一次读取一个字符数组
代码演示:
[java] view plaincopy
<strong>package cn.itcast2;
import java.io.FileReader;
import java.io.IOException;
public class Demo3 {
public static void main(String[] args) throws IOException {
// method();
method2();
}
public static void method2() throws IOException {
FileReader fr = new FileReader("b.txt");
char[] chars = new char[1024];
int len;
while((len=fr.read(chars))!=-1) {
//char类型的数组比较特殊,会将该数组中的所有内容打印出来
// System.out.println(chars);
System.out.println(new String(chars, 0, len));
}
fr.close();
}
public static void method() throws IOException {
FileReader fr = new FileReader("b.txt");
int c;
while((c=fr.read())!=-1) {
System.out.print((char)c);
}
fr.close();
}
}</strong>
高效字符流:
BufferedWriter:
public BufferedWriter(Writer out)
public void newLine() throws IOException 写入一个行分隔符
BufferedReader:
public BufferedReader(Reader in)
public String readLine() throws IOException 读取一个文本行
返回的字符串不包括回车换行,但是回车换行是返回一行的标志
高效流:
构造方法传入的不是文件,而是对应的流对象
代码演示:
[java] view plaincopy
<strong>package cn.itcast2;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo4 {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("f.txt"));
String ls = System.getProperty("line.separator");
bw.write('a');
// bw.write("\r\n");
// bw.write(ls);
bw.newLine();
bw.write("i love java".toCharArray());
// bw.write("\r\n");
// bw.write(ls);
bw.newLine();
bw.write("i like java");
bw.close();
BufferedReader br = new BufferedReader(new FileReader("f.txt"));
//定义字符串,临时记录每次读到的每行的字符串
String line;
while((line=br.readLine())!=null) {
System.out.println(line);
}
br.close();
File srcFile = new File("f.txt");
File destFile = new File("g.txt");
method(srcFile,destFile);
}
//使用高效字符流一次复制一行
public static void method(File src,File dest) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(src));
BufferedWriter bw = new BufferedWriter(new FileWriter(dest));
String line;
while((line=br.readLine())!=null) {
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
}
}
</strong>
版权声明:本文为博主原创文章,未经博主允许不得转载。
0 0
- 黑马程序员_JAVA之IO流的(字节流与字符流)
- 黑马程序员_JAVA之IO流(字节流与字符流)
- 黑马程序员_JAVA中IO流-字节流与字符流比较与转换
- 黑马程序员-IO流 字符流与字节流
- 黑马程序员—IO流(字符流与字节流)
- 黑马程序员——IO字符流与字节流
- 黑马程序员_java之IO流
- 黑马程序员_java之IO流
- 黑马程序员_java基础之IO流
- 黑马程序员_JAVA 之IO流
- 黑马程序员_java中IO流--字节流
- 黑马程序员_java输入与输出的IO流技术
- 黑马程序员_java IO流
- 黑马程序员_java IO流
- 黑马程序员_Java IO流
- 黑马程序员_java IO流
- 黑马程序员_java IO流
- 黑马程序员_java IO流
- C++中windwos数据类型和类型转换
- 数据结构-链表
- Prim实现最小生成树
- 笔试题——数据库索引的作用
- 关于java中有符号数转换成无符号数的相关问题
- 黑马程序员_JAVA之IO流的(字节流与字符流)
- 数据结构基础4_双链表的实现
- C#日期时间
- lintcode-木材加工-183
- C++primer学习:文本查询程序
- UFT的工作原理--我自己的理解
- 指令缓存
- 内存分配策略
- PostgreSQL性能优化综合案例讲解 - 1