所谓IO流,顾名思义,I就是input,O就是output。
IO流的分类:
按照流的流向分,可以分为输入流和输出流。
输入流: 只能从中读取数据,而不能向其写入数据。
输出流:只能向其写入数据,而不能向其读取数据。
按照操作单元划分,可以划分为字节流和字符流。
字节流和字符流的用法几乎完成全一样,区别在于字节流和字符流所操作的数据单元不同,字节流操作的单元是数据单元是8位的字节,字符流操作的是数据单元为16位的字符。
按照流的角色划分为节点流和处理流。
IO流的一些简单运用:
文件的读取
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class Read {
public static void main(String[] args) throws IOException {
// TODO 自动生成的方法存根
String path = "F:" + File.separator + "A" + File.separator + "b.txt";
InputStream input = new FileInputStream(path);
byte[] b = new byte[1024];
System.out.println(input);
int num;
while ((num = input.read(b)) != -1) {
System.out.println("每次读取字节数 " + num);
String str = new String(b, 0, num);
System.out.println("读取数据 " + str);
}
input.close();
System.out.println("操作完毕!");
}
}
文件的删除:
public static void DeleteFile(File file) {
File[] b = file.listFiles();
for (int i = 0; i < b.length; i++) {
if (b[i].isFile()) {
b[i].delete();
} else {
DeleteFile(b[i]);
}
}
file.delete();
}
文件的复制:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Copy {
public static void main(String[] args) {
String src = "F:" + File.separator + "A" + File.separator + "d.png";
String dest = "F:" + File.separator + "B" + File.separator + "copy.png";
InputStream insrc = null;
OutputStream outdest = null;
try {
insrc = new FileInputStream(src);
outdest = new FileOutputStream(dest);
int num;
byte[] b = new byte[1024];
while ((num = insrc.read(b)) != -1) {
outdest.write(b, 0, num);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (outdest != null)
outdest.close();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (insrc != null) {
insrc.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
System.out.println("复制完成!");
}
}
}
文件的合并:
public static void He() throws IOException {
String path1 = “F:” + File.separator + “A” + File.separator + “b.txt”;
String path2 = “F:” + File.separator + “A” + File.separator + “c.txt”;
String path3 = “F:” + File.separator + “A” + File.separator + “d.txt”;
OutputStream c = new FileOutputStream(path3);
InputStream a = new FileInputStream(path1);
InputStream b = new FileInputStream(path2);
ArrayList streams = new ArrayList();
streams.add(a);
streams.add(b);
Enumeration e = Collections.enumeration(streams);
SequenceInputStream s = new SequenceInputStream(e);
byte[] buf = new byte[1024];
int num;
while ((num = s.read(buf)) != -1) {
c.write(buf, 0, num);
}
System.out.println("合并完成!");
c.close();
a.close();
b.close();
}
文件的分割:
public static void Fen() throws IOException {
String src = “F:” + File.separator + “A” + File.separator + “d.png”;
InputStream in = new FileInputStream(src);
String dest = “F:” + File.separator + “A”;
File parent = new File(dest);
OutputStream out = null;
int count = 1;
byte[] buf = new byte[1024];
int num;
while ((num = in.read(buf)) != -1) {
File son = new File(parent, “a” + (count++) + “.png”);
out = new FileOutputStream(son);
out.write(buf, 0, num);
out.close();
}
in.close();
System.out.println(“分割完成!”);
}
}
以上都是一些运用File的函数完成的一些简易功能。