Java IO流总结 博客分类: Java IO和NIO javaio
程序员文章站
2024-03-16 14:55:04
...
本篇主要对Java基础中BIO文件输入输出流做一个总结。
首先来看一下IO流的分类,图片来自网络:
直接上代码吧:
1.字节输入流FileInputStream,字节输出流FileOutputStream:
/** * FileInputStream 读取原始字节流 **/ public class TestFileInputStream{ public static void main(String []args){ FileInputStream fis = null; try{ fis = new FileInputStream("d:\\java\\io\\TestFileInputStream.java");//"d:/java/io/TestFileInputStream.java" //read() int num=0; int b; while((b=fis.read())!=-1){ System.out.print((char)b); num++; } System.out.println("共有字节数:"+num); //read(byte []b) /*byte []buffer = new byte[1024]; int n; int num=0; int count=0; while((n=fis.read(buffer))!=-1){ for(int i=0;i<buffer.length;i++){ System.out.print((char)buffer[i]); num++; } count++; } System.out.println("共有字节数:"+num+",共读了"+count+"次");*/ //read(byte []b,int off,int len) /*byte []buffer = new byte[200]; int n=0; int num=0; int count=0; while((n=fis.read(buffer,0,buffer.length))!=-1){ for(int i=0;i<buffer.length;i++){ System.out.print((char)buffer[i]); num++; } count++; } System.out.println("共有字节数:"+num+",共读了"+count+"次");*/ fis.close(); }catch(Exception e){ System.out.println("读取原始字节流失败!"); } } }
/** * FileOutputStream 写入原始字节流 **/ public class TestFileOutputStream{ public static void main(String []args){ FileInputStream fis=null; FileOutputStream fos=null; int b; try{ fis = new FileInputStream("d:/java.jpg"); fos = new FileOutputStream("e:/java.jpg"); //read(),write() while((b=fis.read())!=-1){ System.out.print((char)b); fos.write(b); } //read(byte []b),write(byte []b) /*byte []buffer = new byte[1024]; while((b=fis.read(buffer))!=-1){ fos.write(buffer); }*/ fos.flush(); fos.close(); fis.close(); System.out.println("文件复制成功!"); }catch(Exception e){ System.out.println("写入原始字节流失败!"); } } }
2.字符输入流FileReader,字符输出流FileWriter:
/** * FileReader 读取字符流 **/ public class TestFileReader{ public static void main(String []args){ FileReader fr=null; int b=0; try{ fr = new FileReader("d:/java/io/TestFileReader.java"); while((b=fr.read())!=-1){ System.out.print((char)b); } fr.close(); }catch(Exception e){ System.out.println("读取字符流失败!"+e); } } }
/** * FileWriter 写入字符流 **/ public class TestFileWriter{ public static void main(String []args){ FileReader fr = null; FileWriter fw = null; int b=0; try{ fr = new FileReader("d:/java/io/TestFileWriter.java"); fw = new FileWriter("d:/java/io/TestFileWriter1.java"); while((b=fr.read())!=-1){ fw.write(b); } fw.flush(); fw.close(); fr.close(); System.out.println("文件复制成功!"); }catch(Exception e){ System.out.println("写入字符流失败!"+e); } } }
3.带缓冲Buffered的输入输出流:
/** * 缓冲流 * BufferedInputStream 带缓冲区地读取原始字节流 * BufferedOutputStream 带缓冲区地写入原始字节流 **/ public class TestBufferedStream1{ public static void main(String []args){ BufferedInputStream bis = null; BufferedOutputStream bos = null; int c=0; try{ bis = new BufferedInputStream(new FileInputStream("d:/java/io/TestBufferedStream1.java")); bos = new BufferedOutputStream(new FileOutputStream("d:/Test1.java")); System.out.println((char)bis.read()); System.out.println((char)bis.read()); bis.mark(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); } while((c=bis.read())!=-1){ bos.write(c); } System.out.println("文件复制成功!"); bos.flush(); bos.close(); bis.close(); }catch(Exception e){ System.out.println("带缓冲区地读取、写入原始字节流失败!"+e); } } }
/** * 缓冲流 * BufferedReader 带缓冲区地读取字符流 * BufferedWriter 带缓冲区地写入字符流 **/ public class TestBufferedStream2{ public static void main(String []args){ BufferedReader br = null; BufferedWriter bw = null; String s=null; try{ bw = new BufferedWriter(new FileWriter("d:/dbk.txt")); br = new BufferedReader(new FileReader("d:/dbk.txt")); for(int i=0;i<100;i++){ bw.write(String.valueOf(Math.random())); bw.newLine(); } bw.flush(); while((s=br.readLine())!=null){ System.out.println(s); } bw.close(); br.close(); }catch(Exception e){ System.out.println("带缓冲区地读取、写入原始字符流失败!"+e); } } }
4.数据流DataStream:
/** * 数据流 * ByteArrayOutputStream byte数组输出流 * ByteArrayInputStream byte数组输入流 * DataOutputStream 数据输出流 * DataInputStream 数据输入流 **/ public class TestDataStream{ public static void main(String []args){ ByteArrayOutputStream baos = null; DataOutputStream dos = null; ByteArrayInputStream bais = null; DataInputStream dis = null; try{ baos = new ByteArrayOutputStream(); dos = new DataOutputStream(baos); dos.writeDouble(Math.random()); dos.writeBoolean(true); dos.flush(); bais = new ByteArrayInputStream(baos.toByteArray()); System.out.println(bais.available()); dis = new DataInputStream(bais); System.out.println(dis.readDouble()); System.out.println(dis.readBoolean()); dos.close(); dis.close(); }catch(Exception e){ System.out.println("数据流失败!"+e); } } }
5.打印流PrintStream:
/* * 打印流 * PrintStream 打印字节流 **/ public class TestPrintStream1{ public static void main(String []args){ PrintStream ps = null; try{ FileOutputStream fos = new FileOutputStream("d:/bak.dat"); ps = new PrintStream(fos); if(ps != null){ System.setOut(ps); } int n=0; for(int i=0;i<60000;i++){ System.out.print((char)i); if(n++==100){ System.out.println(); n=0; } } }catch(Exception e){ System.out.println("打印字节流失败!"+e); } } }
/* * 打印流 * PrintWriter 打印字符流 **/ public class TestPrintStream3{ public static void main(String []args){ PrintWriter pw = null; try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new FileWriter("d:/bak.log",true)); String s = null; while((s=br.readLine())!=null){ if(s.equalsIgnoreCase("exit"))break; System.out.println(s.toUpperCase()); pw.println(s); pw.flush(); } pw.println("------------"); pw.println("===="+new Date()+"===="); pw.flush(); pw.close(); br.close(); }catch(Exception e){ System.out.println("打印字符流失败!"+e); } } }
6.转换流:
/** * 转换流 * InputStreamReader 将InputStream转换为Reader * 可以读一行 **/ public class TestTransform2{ public static void main(String []args){ InputStreamReader isr = null; try{ isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = null; while((s=br.readLine())!=null){ if(s.equalsIgnoreCase("exit")){ System.exit(1); } System.out.println(s.toUpperCase()); } br.close(); isr.close(); }catch(Exception e){ System.out.println("转换输出流失败!"+e); } } }
/** * 转换流 * OutputStreamWriter 将OutputStream转换为Writer * 可以写一个字符串 **/ public class TestTransform1{ public static void main(String []args){ OutputStreamWriter osw = null; try{ osw = new OutputStreamWriter(new FileOutputStream("d:/bak.txt")); osw.write("microsoftibmgoogle"); System.out.println(osw.getEncoding()); osw.close(); osw = new OutputStreamWriter(new FileOutputStream("d:/bak.txt",true),"ISO-8859-1"); osw.write("microsoftibmgoogle"); System.out.println(osw.getEncoding()); osw.close(); }catch(Exception e){ System.out.println("转换输出流失败!"+e); } } }
7.Object流,Java对象序列化和反序列化,transient关键字:
/* * Object流 * Serializable 标记类可被系列化 * ObjectInputStream * ObjectOutputSteam **/ public class TestObjectIo{ public static void main(String []args){ T t = new T(); t.n=15; try{ FileOutputStream fos = new FileOutputStream("d:/objectio.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(t); oos.flush(); oos.close(); FileInputStream fis = new FileInputStream("d:/objectio.dat"); ObjectInputStream ois = new ObjectInputStream(fis); T oist = (T)ois.readObject(); System.out.println(oist.i+" "+oist.j+" "+oist.k+" "+oist.n); ois.close(); }catch(Exception e){ System.out.println("Object流失败!"+e); } } } class T implements Serializable{ int i=1; int j=10; double k=2.231; transient int n=12;//transient 透明的,在系列化时不予考滤 }
先总结如上,后续持续更新。。。
<audio controls="controls" style="display: none;"></audio>
推荐阅读
-
Java IO流总结 博客分类: Java IO和NIO javaio
-
java流的总结——从原理分析 博客分类: java IO javaio数据源字节字符
-
Java NIO框架Mina、Netty、Grizzly介绍与对比 博客分类: IO
-
Java NIO 学习资料 博客分类: java io
-
Java NIO 学习资料 博客分类: java io
-
J03-Java IO流总结三 《 FileInoutStream和FileOutputStream 》
-
J04-Java IO流总结四 《 FileReader和FileWriter 》
-
J05-Java IO流总结五 《 BufferedInputStream和BufferedOutputStream 》
-
J07-Java IO流总结七 《 InputStreamReader和OutputStreamWriter 》
-
J04-Java IO流总结四 《 FileReader和FileWriter 》