java流
输入流
InputStream类
- int read()
- void close()
- int available()
- skip(long n)
- boolean markSupported(),void mark(int readLimit),void reset()
FileStream类(InputStream的子类)
读文件
package tupnimaerts;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Read {
public static void main(String[] args) throws FileNotFoundException {
String fileName = "D:\\aa\\dd.txt"; // 文件名
FileInputStream fStream = new FileInputStream(fileName); // 获得文件输入对象
try {
int ch = fStream.read();
while (ch != -1){
System.out.print((char)ch);
ch = fStream.read();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出流
OutStream类
复制一个文件(字节流)
package tupnimaerts;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy {
public static void main(String[] args) {
String fileNameIn= "D:\\aa\\dd.txt";
String fileNameOut= "D:\\aa\\ddCopy.txt";
try {
FileInputStream fin = new FileInputStream(fileNameIn);
FileOutputStream fout = new FileOutputStream(fileNameOut);
int ch = fin.read();
while(ch != -1){
fout.write(ch);
ch = fin.read();
}
System.out.println("写出成功");
fout.close();
fin.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
复制一个文件(缓冲区)
package tupnimaerts;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy {
public static void main(String[] args) {
String fileNameIn= "D:\\aa\\ee.ppt"; // 打开的文件
String fileNameOut= "D:\\aa\\eeCopy.ppt"; // 写出的文件
try {
FileInputStream fin = new FileInputStream(fileNameIn);
FileOutputStream fout = new FileOutputStream(fileNameOut);
byte[] buff = new byte[1024]; // 缓冲区
int len = fin.read(buff);
while(len != -1){
fout.write(buff,0,len);
len = fin.read(buff);
}
System.out.println("写出成功");
fout.close();
fin.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
拷贝较大的文件的时候尽量避免一个个字节读入,操作磁盘的过程是很慢的,直接读入一个扇区可以很大程度上加快拷贝的速度。
序列化和反序列化
package tupnimaerts;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
class St implements Serializable{
// 需要实现序列化接口
// 但是实际上什么也没干
private String xm;
private int nl;
String getXm() {
return xm;
}
int getNl() {
return nl;
}
St(String xm, int nl) {
this.xm = xm;
this.nl = nl;
}
}
public class ObjSave {
public static void main(String[] args) {
// 序列化对象 把对象写入到文件中
// 反序列化对象 把文件中的对象读到文件中
St st1 = new St("zs",19);
St st2 = new St("ww",20);
String fileName = "D:\\aa\\st.dat";
List<St> stList = new ArrayList<>();
try {
FileOutputStream fout = new FileOutputStream(fileName);
ObjectOutputStream oout = new ObjectOutputStream(fout);
oout.writeObject(st1);
oout.writeObject(st2);
oout.close();// 先关闭处理流
fout.close();// 再关闭节点流
FileInputStream fin = new FileInputStream(fileName);
ObjectInputStream oin = new ObjectInputStream(fin);
while(true){
try{
St t = (St)oin.readObject();
stList.add(t);
}catch (java.io.EOFException e){
break;
}
// 系统并没有提供判断文件尾的函数,所以遇到文件尾之后会抛出文件IO异常
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
for (St x : stList){
System.out.println(x.getXm() + " " + x.getNl());
}
}
}