流的分类
- 按数据流的方向不同可以分为输入流和输出流
- 按处理数据单位不同可以分为字节流和字符流
- 按照功能不同可以分为节点流和处理流
节点流和处理流
FileInputStream & FileOutputStream
//字节流
public class TestFileInputStream {
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
try {
in = new FileInputStream("F:/DB/abc.txt");
} catch (FileNotFoundException e) {
System.out.println("系统找不到指定文件!");
System.exit(-1);
}
try {
long num = 0;
while((b = in.read()) != -1) {
System.out.println((char)b);
num++;
}
in.close();
System.out.println();
System.out.println("共读取了" + num + "个字节");
} catch (IOException e) {
System.out.println("文件读取错误");
System.exit(-1);
}
}
}
//字节流[文件复制]
public class TestFileOutputStream {
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("F:/DB/abc.txt");
out = new FileOutputStream("F:/DB/def.txt");
while((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
System.exit(-1);
} catch (IOException e) {
System.out.println("文件复制错误");
System.exit(-1);
}
System.out.println("文件已复制");
}
}
FileReader & FileWriter
//字符流
public class TestFileReader {
public static void main(String[] args) {
int c = 0;
FileReader in = null;
try {
in = new FileReader("F:/DB/abc.txt");
} catch (FileNotFoundException e) {
System.out.println("系统找不到指定文件!");
System.exit(-1);
}
try {
long num = 0;
while((c = in.read()) != -1) {
System.out.println((char)c);
num++;
}
in.close();
System.out.println();
System.out.println("共读取了" + num + "个字节");
} catch (IOException e) {
System.out.println("文件读取错误");
System.exit(-1);
}
}
}
//字符流
public class TestFileWriter {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("F:/DB/abc.dat");
for(int c = 0; c <=50; c++) {
fw.write(c);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件写入错误");
System.exit(-1);
}
}
}
BufferedInputStream & BufferedOutputStream
//字节缓冲流
public class TestBufferStream1 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("F:/DB/abc.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int c = 0;
System.out.println((char)bis.read());
System.out.println((char)bis.read());
bis.mark(100); //将指针指到100下标处
for(int i = 0; i < 10 && (c = bis.read()) != -1; i++) {
System.out.print((char)c + " ");
}
System.out.println();
bis.reset(); //回到标记处
for(int i = 0; i < 10 && (c = bis.read()) != -1; i++) {
System.out.print((char)c + " ");
}
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedReader & BufferedWriter
//字符缓冲流
public class TestBufferStream2 {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("F:/DB/random.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("F:/DB/random.txt"));
String s = null;
for(int i = 0; i < 100; i++) {
s = String.valueOf(Math.random());
bw.write(s);
bw.newLine();
}
bw.flush();
while((s = br.readLine()) != null) {
System.out.println(s);
}
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
DataInputStream & DataOutputStream & ByteArrayInputStream & ByteArrayOutputStream
//对基本的数据类型进行读写
public class TestDataStream {
public static void main(String[] args) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeDouble(Math.random());
dos.writeBoolean(true);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
DataInputStream dis = new DataInputStream(bais);
System.out.println(dis.available());
System.out.println(dis.readDouble()); //先写到字节数组中的数据先读
System.out.println(dis.readBoolean());
dos.close(); dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
InputStreamReader & OutputStreamWriter
//转换流[将字节流转换成字符流]
public class TestTransform2 {
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = null;
try {
s = br.readLine();
while(s != null) {
if(s.equalsIgnoreCase("exit")) break;
System.out.println(s.toUpperCase());
s = br.readLine();
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//转换流[将字节流转换成字符流]
public class TestTransform1 {
public static void main(String[] args) {
try {
//OutputStreamWriter将字节流转换成字符流输出
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("F:/DB/transform.txt"));
osw.write("amazine1");
System.out.println(osw.getEncoding());
osw.close();
//FileOutputStream第二个参数为true表示追加
osw = new OutputStreamWriter(new FileOutputStream("F:/DB/transform.txt",true),"ISO8859_1");
osw.write("amazine2");
System.out.println(osw.getEncoding());
osw.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
PrintStream & PrintWriter
//打印流
public class TestPrintStream1 {
public static void main(String[] args) {
try {
PrintStream ps = null;
FileOutputStream fos = new FileOutputStream("F:/DB/log.dat");
ps = new PrintStream(fos);
int in = 0;
//打印到Console窗口
for(char c = 0; c < 60000; c++) {
System.out.print(c + " ");
if(in++ >= 100) {//每行打印100个数
System.out.println();
in = 0;
}
}
if(ps != null) {//输出重定向
System.setOut(ps);
}
//打印到文件中去
for(char c = 0; c < 60000; c++) {
System.out.print(c + " ");
if(in++ >= 100) {//每行打印100个数
System.out.println();
in = 0;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
//打印流
public class TestPrintStream2 {
public static void main(String[] args) {
String fileName = args[0];
if(fileName != null) {
list(fileName, System.out);
}
}
public static void list(String fileName, PrintStream fs) {
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String s = null;
while((s = br.readLine()) != null) {
fs.println(s);
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
fs.println("无法读取文件!");
}
}
}
//模拟日志
public class TestPrintStream3 {
public static void main(String[] args) {
String s = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
FileWriter fw = new FileWriter("F:/DB/logtxt.txt");
PrintWriter log = new PrintWriter(fw);
while((s = br.readLine()) != null) {
if(s.equalsIgnoreCase("exit")) {
break;
}
System.out.println(s.toUpperCase());
log.println("------");
log.println(s.toUpperCase());
log.flush();
}
log.println("======" + new Date() + "======");
log.flush();
log.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ObjectInputStream & ObjectOutputStream
//Object流
public class TestObjectIO {
public static void main(String[] args) {
T t = new T();
t.k = 90;
try {
FileOutputStream fos = new FileOutputStream("F:/DB/objectio.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(t);
oos.flush();
oos.close();
FileInputStream fis = new FileInputStream("F:/DB/objectio.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
T tRead = (T) ois.readObject();
System.out.println(tRead.i + " " + tRead.j + " " + tRead.d + " " + tRead.k);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
class T implements Serializable{
int i = 10;
int j = 9;
double d = 3.4;
transient int k = 40; //序列化的时候不序列化该变量
}