四种节点流和Buffered的使用
来源:互联网 发布:过程控制软件 编辑:程序博客网 时间:2024/06/08 07:22
1、FileInputStream的使用
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class TestFileInputOutputStream { /** * 从硬盘中读取一个文件内容到程序中 * */ public static void main(String[] args) { String counte = "河南理工大学";// testFileInputStream1();// testFileInputStream2();// testFileInputStream3(counte); long start = System.currentTimeMillis(); testFileInputOutputStream(); long end = System.currentTimeMillis(); System.out.println(end - start); } //复制文件 public static void testFileInputOutputStream(){ File file1 = new File("C:\\Users\\Administrator\\Desktop\\music.rar"); File file2 = new File("C:\\Users\\Administrator\\Desktop\\DEF.rar"); FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(file1); fos = new FileOutputStream(file2); byte[] b = new byte[1024]; int len; while((len = fis.read(b)) != -1){ fos.write(b, 0, len); } fis.close(); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void testFileInputStream3(String counte){ //输出 File file = new File("C:\\Users\\Administrator\\Desktop\\DEF.txt"); try { FileOutputStream fos = new FileOutputStream(file); fos.write(counte.getBytes()); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void testFileInputStream2(){ //使用数组 File file = new File("C:\\Users\\Administrator\\Desktop\\ABC.txt"); try { FileInputStream fis = new FileInputStream(file); byte[] b = new byte[5];//读取到的数据 int len; try { while((len = fis.read(b)) != -1){// for (int i = 0; i < len; i++) {// System.out.print((char)b[i]);// } String st = new String(b, 0, len); System.out.print(st); } fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 以字节方式读取文件(),并输出到控制台 * */ public static void testFileInputStream1(){ File file = new File("C:\\Users\\Administrator\\Desktop\\ABC.txt"); try { FileInputStream fis = new FileInputStream(file); //3、调用FileIn普通Stream的方法 int b = fis.read(); while (b != -1) { System.out.print((char)b); b = fis.read(); } fis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
2、FileOutputStream的使用
import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class TestFileReaderWriter { public static void main(String[] args) {// testFileReader(); testFileReaderWriter(); } /** * 复制文本文件 * */ public static void testFileReaderWriter(){ FileReader fr = null; FileWriter fw = null; File file1 = new File("C:\\Users\\Administrator\\Desktop\\123.txt"); File file2 = new File("C:\\Users\\Administrator\\Desktop\\456.txt"); try { fr = new FileReader(file1); fw = new FileWriter(file2); char[] c = new char[1024]; int len; while((len = fr.read(c)) != -1){ fw.write(c, 0, len); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if (fw != null) { try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (fr != null) { try { fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * 读取文本文件 * */ public static void testFileReader(){ File file = new File("C:\\Users\\Administrator\\Desktop\\123.txt"); FileReader fr = null; try { fr = new FileReader(file); char[] c = new char[1024]; int len; while((len = fr.read(c)) != -1){ String str = new String(c, 0, len); System.out.println(str); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if (fr != null) { try { fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}
3、FileReaderWriter的使用
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class TestBufferedInputOutputStream { /** * 缓冲流 * BufferedInputStream * BufferedOutputStream * BufferedReader * BufferedWriter */ public static void main(String[] args) {// testBufferedInputOutputStream();// testBufferedReaderWriter(); testBufferedReaderWriter2(); } //缓冲流 文本文件的复制 public static void testBufferedReaderWriter2(){ File file1 = new File("C:\\Users\\Administrator\\Desktop\\123.txt"); FileReader fr = null; BufferedReader br= null; try { fr = new FileReader(file1); br = new BufferedReader(fr); String len; while((len = br.readLine()) != null){ System.out.println(len); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void testBufferedReaderWriter(){ File file1 = new File("C:\\Users\\Administrator\\Desktop\\123.txt"); File file2 = new File("C:\\Users\\Administrator\\Desktop\\456.txt"); FileReader fr = null; FileWriter fw = null; BufferedReader br= null; BufferedWriter bw = null; try { fr = new FileReader(file1); 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); bw.flush(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }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(); } } } } //缓冲流 非文本文件的复制 public static void testBufferedInputOutputStream(){ File file1 = new File("C:\\Users\\Administrator\\Desktop\\123.txt"); File file2 = new File("C:\\Users\\Administrator\\Desktop\\456.txt"); //2、先创建相应的节点流,FileInputStream、FileInputStream FileInputStream fis; FileOutputStream fos; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { fis = new FileInputStream(file1); fos = new FileOutputStream(file2); //3、将创建的节点流的对象作为形参传递给缓冲流的构造器 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); bos.flush(); } bis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if (bos != null) { try { bos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (bis != null) { try { bis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}
3 0
- 四种节点流和Buffered的使用
- linux中line buffered和fully buffered的区别
- Java IO: 字符流的Buffered和Filter
- JAVA中Scanner和Buffered的区别
- IO学习之使用带缓冲的字符流(Buffered)读取数据
- Java IO流原理之常用字节流和字符流详解以及Buffered高效的原理
- JavaIO流原理之常用字节流和字符流详解以及Buffered高效的原理
- Java IO: Buffered和Data
- 使用MPICH构建一个四节点的集群系统
- Buffered流以及其思想
- java 缓冲(Buffered。。。。)流
- MyISAM 表的index blocks 是buffered 和被所有的threads 共享。
- extjs 分页 分页工具分页和缓存分页的简单使用 buffered、pagingtool
- Direct IO和Buffered IO简单分析
- 研究-Buffered IO和Direct IO
- Java IO(十):Buffered 和 Data
- delphi TreeView 从数据库添加节点的四种方法
- 黑马程序员—字节流Stream和缓冲流Buffered的小练习
- tomcat7可以运行jdk8的项目吗/jdk8项目在tomcat7上可以使用吗
- HDU 1171(dp46)
- iOS 程序 main 函数之前发生了什么
- 夏令营day11总结
- 微服务架构 (四): 提升微服务分布式远程调用的可靠性与性能; Time Out 与 Circuit Breaker
- 四种节点流和Buffered的使用
- 递归算法
- 洗牌(shuffle)问题详解
- BootStrap 教程 之 插件(04)
- HDU 2048 (dp46)
- Java中的文件和流相关知识
- 文件系统中数据读取的详细过程
- 《实战Java...》读书笔记2
- LeetCode 23. Merge k Sorted Lists