JavaIO
程序员文章站
2022-06-13 21:20:35
...
一、IO 流的结构
二、IO流的划分
1 按照流的流向的不同:输入流 输出流 (站位于程序的角度)
2 按照流中的数据单位的不同:字节流 字符流 (纯文本文件使用字符流 ,除此之外使用字节流)
3 按照流的角色的不同:节点流 处理流 (流直接作用于文件上是节点流(4个),除此之外都是处理流)
三、实现
1、字节流文件复制
/**
* 实现字节文件的复制
*
* @param src源文件路径
* @param dest目标文件路径
*/
public void copyFile(String src, String dest) {
File file1 = new File(src);// 源文件
File file2 = new File(dest);// 目标文件
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int len;
while ((len = bis.read(b)) != -1) {
bos.write(b, 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();
}
}
}
}
2、字符文件复制
public void copyFile2(String src, String dest) {
File file1 = new File(src);
File file2 = new File(dest);
BufferedReader br = null;
BufferedWriter bw = null;
try {
FileReader fr = new FileReader(file1);
FileWriter fw = new FileWriter(file2);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
// 按字符读
// char[] c = new char[1024];
// int len;
// while((len=br.read(c)) != -1) {
// bw.write(c, 0, len);
// }
// 按行读
String str = null;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3、转换流文件复制
/**
* 转换流输入输出
* 编码 字符串---->字节数组
* 解码 字节数组---->字符串
*
* @param src
* @param dest
*/
public void copyFile(String src, String dest) {
File file1 = new File(src);
File file2 = new File(dest);
BufferedReader br = null;
BufferedWriter bw = null;
try {
FileInputStream fis = new FileInputStream(file1);
InputStreamReader isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
FileOutputStream fos = new FileOutputStream(file2);
OutputStreamWriter osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
String str;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4、基于转换流的控制台输入小案例
public class MyInput {
public String nextString() {
String str = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
return Integer.parseInt(nextString());
}
public boolean nextBoolean() {
return Boolean.parseBoolean(nextString());
}
public static void main(String[] args) {
MyInput in = new MyInput();
String nextString = in.nextString();
System.out.println(nextString);
int nextInt = in.nextInt();
System.out.println(nextInt);
boolean nextBoolean = in.nextBoolean();
System.out.println(nextBoolean);
}
}
5、打印输出流
@Test
public void testPrintStreamWriter() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("text.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} // 创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
PrintStream ps = new PrintStream(fos, true);
if (ps != null) { // 把标准输出流(控制台输出)改成文件
System.setOut(ps);
}
for (int i = 0; i <= 255; i++) { // 输出ASCII字符
System.out.print((char) i);
if (i % 50 == 0) { // 每50个数据一行
System.out.println(); // 换行
}
}
ps.close();
}
6、DataWrite&DataRead
@Test
public void testDataWrite() {
DataOutputStream dos = null;
try {
FileOutputStream fos = new FileOutputStream(new File("data.txt"));
dos = new DataOutputStream(fos);
dos.writeInt(3);
dos.writeUTF("你好");
dos.writeBoolean(true);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testDataRead(){
DataInputStream dis = null;
try {
FileInputStream fis = new FileInputStream(new File("data.txt"));
dis = new DataInputStream(fis);
int readInt = dis.readInt();
String readUTF = dis.readUTF();
boolean readBoolean = dis.readBoolean();
System.out.println(readInt);
System.out.println(readUTF);
System.out.println(readBoolean);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(dis != null) {
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
7、ObjectInputStream&ObjectOutputStream(序列化)
public class TestObjectInputOutputStream {
@Test
public void testObejctInputStream() {
ObjectInputStream ois =null;
try {
ois = new ObjectInputStream(new FileInputStream(new File("person.txt")));
Person p1 = (Person) ois.readObject();
System.out.println(p1);
Person p2 = (Person) ois.readObject();
System.out.println(p2);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if(ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testObjectOutputStream() {
Person p1 = new Person("小红", 33);
Person p2 = new Person("小明", 25);
ObjectOutputStream oos = null;;
try {
oos = new ObjectOutputStream(new FileOutputStream(new File("person.txt")));
oos.writeObject(p1);
oos.writeObject(p2);;
} catch (IOException e) {
e.printStackTrace();
} finally {
if(oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class Person implements Serializable{
private static final long serialVersionUID = -2614025952162968968L;
String name;
Integer age;
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
8、RandomAccessFile
// 实现插入
@Test
public void test4() {
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File("hello5.txt"), "rw");
raf.seek(4);//插入位置4
byte[] b = new byte[10];
int len;
StringBuffer sb = new StringBuffer();
while ((len = raf.read(b)) != -1) {
sb.append(new String(b, 0, len));
}
raf.seek(4);
raf.write("xyz".getBytes());
raf.write(sb.toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (raf != null) {
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
推荐阅读
-
[十三]JavaIO之PushBackInputStream
-
[十]JavaIO之FilterInputStream FilterOutputStream
-
JavaIO流总结
-
[十]JavaIO之FilterInputStream FilterOutputStream
-
[十三]JavaIO之PushBackInputStream
-
[五] JavaIO之InputStream OutputStream简介 方法列表说明
-
[二] JavaIO之File详解 以及FileSystem WinNTFileSystem简介
-
JavaIO流基本操作
-
【JavaIO流】JavaIO流基础知识
-
javaIO流(一)