Java流
文件通常是由一连串的字节或字符构成,组成文件的字节序列称为字节流,组成文件的字符序列称为字符流。 Java 中根据流的方向可
以分为输入流和输出流。
输入流是将文件或其它输入设备的数据加载到内存的过程; 输出流恰恰相反, 是将内存中的数据保存到文件或其他输出设备。
见图:
文件是由字符或字节构成, 那么将文件加载到内存或再将文件输出到文件, 需要有输入和输出流的支持,那么在
Java 语言中又把输入和输出流分为了两个,字节输入和输出流,字符输入和输出流
InputStream 是字节输入流:
InputStream 是一个抽象类, 所有继承了 InputStream 的类都是字节输入流
OutputStream(字节输出流):
继承了 OutputStream 都是字节输出流
Reader(字符输入流):
继承了 Reader 都是字符输如流
Writer(字符输出流):
继承了 Writer 都是字符输出流
文件流主要分为:文件字节输入流、文件字节输出流、文件字符输入流、文件字符输出流
FileInputStream(文件字节输入流): 主要按照字节方式读取文件
FileOutputStream(文件字节输出流): 主要按照字节方式写文件
FileReader(文件字符输入流): FileReader 是一字符为单位读取文件,也就是一次读取两个字节
FileWriter(文件字符输出流)
/*
java.io.InputStream 字节输入流
java.io.FileInputStream 文件字节输入流
按照字节方式读取文件
*/
import java.io.*;
public class FileInputStreamTest01{
public static void main(String[] args){
FileInputStream fis = null;
try{
fis = new FileInputStream("C:\work\Java\course\vip\JavaSE-Course-Arry\chapter25\Test01.java");
int i = fis.read(); // 以字节的方式读取
System.out.println("i = " + i);//读出为-1
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
} finally{
// 关闭文件输入流
if(fis != null){
try{
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
/*
int read(byte[] b);
从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
*/
import java.io.*;
public class FileInputStreamTest02{
public static void main(String[] args){
try{
FileInputStream fis = new FileInputStream("C:\work\Java\course\vip\JavaSE-Course-Arry\chapter25\Test01.java");
// 准备一个byte数组
byte[] bytes = new byte[3]; // 每一次最多读取三个字节
// int read(byte[] b);
int i1 = fis.read(bytes);
System.out.println("i = " + i1);
System.out.println(new String(bytes)); //输出乱码
/*
乱码,文件正常读取,但是我们的汉字乱码了,原因在于使用了字节输入流,他是一个
一个字节读取的,所以读取一个字节就打印,那么汉字是不完整的 所以就乱码
*/
} catch(Exception e){
e.printStackTrace();
}
}
}
/*
文件的备份(复制粘贴)
*/
import java.io.*;
public class FileOutputStreamTest01{
public static void main(String[] args){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("C:\work\keke.jpg");
fos = new FileOutputStream("C:\work\Java\keke.jpg");
System.out.println("文件复制开始进行中 ........");
// 一边读,一边写
int temp = 0; // 临时变量
while((temp = fis.read()) != -1){
fos.write(temp);
}
System.out.println("文件复制完毕 !");
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} finally{
if(fis != null){
try{
fis.close();
} catch(Exception e){
e.printStackTrace();
}
}
if(fos != null){
try{
fos.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
}
}
/*
FileReader 文件的字符输入流
是以字符为单位读取文件,也就是一次读取两个字节
*/
import java.io.*;
public class FileReaderTest01{
public static void main(String[] args){
FileReader fr = null;
try{
fr = new FileReader("C:\work\ming.txt");
int temp = 0;
while((temp = fr.read()) != -1){
// 输出字符
System.out.println(temp);
}
// 为什么不乱码? 因为采用了字符输入流 读取文本文件
// 所以汉字就不乱码了(一次读取两个字节<即一个字符>)
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} finally{
try{
if(fr != null){
fr.close();
}
} catch(IOException e){
e.printStackTrace();
}
}
}
}
/*
FileWriter 文件字符输出流
java.lang.Object
继承者 java.io.Writer
继承者 java.io.OutputStreamWriter
继承者 java.io.FileWriter
*/
import java.io.*;
public class FileWriterTest01{
public static void main(String[] args){
FileWriter fw = null;
try{
// true表示 在文件后面追加
fw = new FileWriter("C:\work\ming.txt",true);
// 换行(
)
fw.write("
太棒了!电子科技大学中山学院");
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} finally{
try{
if(fw != null){
fw.close();
}
} catch(IOException e){
e.printStackTrace();
}
}
}
}
/*
采用字节缓冲流改造文件复制代码
*/
import java.io.*;
public class BufferedStreamTest01{
public static void main(String[] args){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
// FileInputStream fis = new FileInputStream("C:\work\arry.txt");
bis = new BufferedInputStream(new FileInputStream("C:\work\keke.jpg"));
// FileOutputStream fos = new FileOutputStream("C:\work\Java\course\arry.txt.bak");
bos = new BufferedOutputStream(new FileOutputStream("C:\work\Java\course\keke.jpg"));
System.out.println("文件复制开始进行中 .....");
int temp = 0;
while((temp = bis.read()) != -1){
bos.write(temp);
}
// 刷新
bos.flush();
System.out.println("文件复制成功 !");
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} finally{
try{
if(bis != null){
bis.close();
}
if(bos != null){
bos.close();
}
} catch(IOException e){
e.printStackTrace();
}
}
}
}