1.概述
IO流是用来处理设备之间的数据传输,Java对数据的操作是通过流的方式,Java用于操作流的对象都在IO包中
分类:
a:按照数据流向
输入流 读入数据
输出流 写出数据
b:按照数据类型
字节流 可以读写任何类型的文件
字符流 只能读写文本文件
注意:如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流;如果在不知情的情况下,就采用用字节流
2.IO流基类概述
A:IO流基类概述
a:字节流的抽象基类:
InputStream ,OutputStream
b:字符流的抽象基类:
Reader , Writer
注:由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。
如:InputStream的子类FileInputStream
如:Reader的子类FileReader
B:FileOutputStream的构造方法
由一个案例引出使用IO流写数据,由于字节流先出现就先学习字节输出流基类OutputStream,
使用具体子类FileOutputStream
Io流的分类:
(1): 按照流向进行划分
输入流
输出流
(2): 按照操作的数据类型进行划分
字节流
字节输入流 InputStream 读
字节输出流 OutputStream 写
字符流
字符输入流 Reader 读
字符输出流 Writer 写
2.FileOutputStream概述
构造方法
FileOutputStream(File file)
FileOutputStream(String name)
FileOutputStream的三个write()方法
public void write(int b):写一个字节 超过一个字节 砍掉前面的字节
public void write(byte[] b):写一个字节数组
public void write(byte[] b,int off,int len):写一个字节数组的一部分
//基本字节流 字节数组写入
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("C:\\Users\\ChioaR\\Desktop\\a.txt");
FileOutputStream out = new FileOutputStream("C:\\Users\\ChioaR\\Desktop\\b.txt");
byte[] bytes = new byte[1024 * 8];
int len=0;
while ((len=in.read(bytes))!=-1){
out.write(bytes);
System.out.println("正在写入");
out.flush();
}
in.close();
out.close();
}
}
public class CopyFileTest2 {
//基本字节流 单个字节写入
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("C:\\Users\\ChioaR\\Desktop\\a.txt");
FileOutputStream out = new FileOutputStream("C:\\Users\\ChioaR\\Desktop\\b.txt");
int len=0;
while ((len=in.read())!=-1){
out.write(len);
out.flush();
System.out.println(len);
}
in.close();
out.flush();
}
}
public class CopyFileTest3 {
//高效字节流 单个字节写入
public static void main(String[] args) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream("C:\\Users\\ChioaR\\Desktop\\a.txt"));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("C:\\Users\\ChioaR\\Desktop\\b.txt"));
int len=0;
while ((len=in.read())!=-1){
out.write(len);
out.flush();
System.out.println(len);
}
in.close();
out.close();
}
}
public class CopyFileTest4 {
//高效字节流 字节数组写入
public static void main(String[] args) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream("C:\\Users\\ChioaR\\Desktop\\a.txt"));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("C:\\Users\\ChioaR\\Desktop\\b.txt"));
int len=0;
byte[] bytes = new byte[1024 * 8];
while ((len=in.read(bytes))!=-1){
out.write(bytes);
out.flush();
}
in.close();
out.close();
}
}
public class CopyFile1 {
// 基本字符流 单个字符
public static void main(String[] args) throws IOException {
FileReader r = new FileReader("C:\\Users\\ChioaR\\Desktop\\day19.txt");
FileWriter w = new FileWriter("C:\\Users\\ChioaR\\Desktop\\copyday19.txt");
int len=0;
while ((len=r.read())!=-1){
w.write(len);
w.flush();
}
w.close();
r.close();
}
}public class CopyFile2 {
//b) 基本字符流 字符数组
public static void main(String[] args) throws IOException {
FileReader r = new FileReader("C:\\Users\\ChioaR\\Desktop\\day19.txt");
FileWriter w = new FileWriter("C:\\Users\\ChioaR\\Desktop\\copyday19.txt");
int len=0;
char[] chars = new char[1024 * 8];
while ((len=r.read(chars))!=-1){
w.write(chars,0,len);
w.flush();
}
r.close();
w.close();
}
}
public class CopyFile3 {
//高效字符流 单个字符
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader(""));
BufferedWriter w = new BufferedWriter(new FileWriter(""));
int len=0;
while ((len=r.read())!=-1){
w.write(len);
w.flush();
}
w.close();
r.close();
}
}
public class CopyFile4 {
//高效字符流 字符数组
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader(""));
BufferedWriter w = new BufferedWriter(new FileWriter(""));
int len=0;
char[] chars = new char[1024 * 8];
while ((len=r.read(chars))!=-1){
w.write(chars,0,len);
w.flush();
}
r.close();
w.close();
}
}
复制文件夹:
public static void main(String[] args) throws IOException{
File srcfile = new File("C:\\Users\\ChioaR\\Desktop\\新建文件夹");
File desfile = new File("C:\\Users\\ChioaR\\Desktop\\新建文件夹1");
desfile.mkdirs();
File[] files = srcfile.listFiles();
for (File file : files) {
String name = file.getName();
File file1 = new File(desfile, name);
copyfile(file,file1);
}
}
private static void copyfile(File srcfile, File desfile) throws IOException {
FileInputStream in = new FileInputStream(srcfile);
FileOutputStream out = new FileOutputStream(desfile);
byte[] bytes = new byte[1024 * 8];
int len=0;
while ((len=in.read(bytes))!=-1){
out.write(bytes);
out.flush();
}
in.close();
out.close();
}
}