常用java中I/O流
来源:互联网 发布:社交网络信息 综述 编辑:程序博客网 时间:2024/05/23 15:23
1、字节流
/** * 字节文件输入流 */ private static void fileInputStreamMethod2() { try { InputStream inputStream=new FileInputStream("E://TestDemoFolder//A.txt"); byte[] b=new byte[2]; int i2=0; while ((i2=inputStream.read(b))!=-1){ System.out.println(new String(b,0,i2)); } inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 文件输入流 */ private static void fileInputStreamMethod() { try { InputStream inputStream=new FileInputStream("E://TestDemoFolder//A.txt"); int i=0; while ((i=inputStream.read())!=-1){ System.out.println(i); } inputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** * 字节文件输出流 */ private static void fileOutPutStreamMethod() { try { OutputStream outPutStream=new FileOutputStream("E://TestDemoFolder//A.txt"); outPutStream.write("\r\n".getBytes()); outPutStream.write("helloWorld".getBytes()); outPutStream.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * 字节缓冲输入流 */ private static void bufferedInputStreamMethod() { try { InputStream in=new FileInputStream("E://TestDemoFolder//A.txt"); BufferedInputStream bis=new BufferedInputStream(in); byte[] b=new byte[2]; int i=0; while ((i=bis.read(b))!=-1){ System.out.println(new String(b,0,i)); } bis.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * 字节缓冲输出流 */ private static void bufferedOutputStreamMethod() { try { OutputStream os=new FileOutputStream("E://TestDemoFolder//A.txt",true); BufferedOutputStream bos=new BufferedOutputStream(os); bos.write("\r\n".getBytes()); bos.write("hello android".getBytes()); bos.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * 缓冲高效输入输出流 */ private static void bufferedReaderAndWriterMethod() { try { InputStream in=new FileInputStream("E://TestDemoFolder//test.mp3"); BufferedInputStream bis=new BufferedInputStream(in); OutputStream out=new FileOutputStream("E://TestDemoFolder//test2.mp3"); BufferedOutputStream bos=new BufferedOutputStream(out); int i=0; byte[] b=new byte[1024]; long begin=System.currentTimeMillis(); while ((i=bis.read(b))!=-1){ bos.write(b,0,i); } long end=System.currentTimeMillis(); System.out.println("cost:"+(end-begin)); bos.close(); bis.close(); out.close(); in.close(); }catch (Exception e){ e.printStackTrace(); } }
2、字符流
/** * 字符转换输出流 */ private static void outPutStreamWriterMethod() { try { OutputStream o=new FileOutputStream("E://TestDemoFolder//A.txt"); OutputStreamWriter outputStreamWriter=new OutputStreamWriter(o); outputStreamWriter.write("hello",0,5); outputStreamWriter.close(); o.close(); }catch (Exception e){ e.printStackTrace(); } } /** * 字符转换输入流 */ private static void inputStreamReaderMethod() { try { InputStream in=new FileInputStream("E://TestDemoFolder//A.txt"); InputStreamReader reader=new InputStreamReader(in); int len=0; byte[]b=new byte[2]; while ((reader.read())!=-1){ System.out.println(len); } reader.close(); in.close(); }catch (Exception e){ e.printStackTrace(); } }
/** * 字符缓冲输入流 */ private static void bufferedReaderMethod() { try { InputStream is=new FileInputStream("E://TestDemoFolder//A.txt"); InputStreamReader isr=new InputStreamReader(is); BufferedReader br=new BufferedReader(isr); String str=""; while ((br.readLine())!=null){ System.out.println(str); } //关流 br.close(); isr.close(); is.close(); }catch (Exception e){ e.printStackTrace(); } }
阅读全文
0 0
- 常用java中I/O流
- Java中I/O流
- java的常用I/O流~~
- Java I/O常用流示例
- Java I/O流常用类小结
- java I/O流常用代码
- java中I/O流学习笔记
- Java中I/O流的选择
- Java中复杂的I/O流
- Java中I/O流总结
- Java中I/O流的应用
- 浅议Java中I/O流
- java I/O流
- java I/O流
- java I/O流
- Java I/O流
- JAVA--I/O流
- java I/O流
- int型整数在内存中的存储方式
- webrtc linux 和 android 版本下载与编译
- 操作系统的体系结构:大内核和微内核
- 个人主页
- svn: E155004 is already locked(文件加锁,执行clean up命令)
- 常用java中I/O流
- 实现FTP文件上传与下载
- 生日悖论的解答
- Windows Server 2012 禁止使用Ctrl+Alt+Del 以及禁止开机启动服务器管理器
- Android drawable图标的匹配顺序
- leetcode 11
- 企业征信信息收集
- 不用判断语句找最大数--程序员面试宝典
- 使用quartz的时候遇见的一个问题