java基础之io流
2016-04-22 10:44
351 查看
file类
file类只能用于获取文件的名字,创建文件,目录等等的操作,不能对文件内容进行读写。输入流和输出流
输入流是读的操作,输出流是写的操作。输入流父类: InputStream 和 Reader (以这两个结尾的都是输入流的子类)
输出流父类: OutputStream 和 Writer (以这两个结尾的都是输出流的子类)
字节流和字符流
字节流常用于音乐 ,视频等操作,比如类:FileInputStream ,FileOutputStream。字符流长用于文件等操作。比如类:File[b]Reader ,FileWriter 。[/b]
[b]前面加BUffered 来缓冲 可以加快处理速度[/b]
读代码:
public class Read { private static FileReader fr = null; private static BufferedReader br = null; /** * @param args * 在这里用到buffer 缓冲流 来包装 节点流 具体见01_IO之 file的filecreat类 * * 此类做的方法是读文件文本内容 */ public static void main(String[] args) { File fileRead = new File("D:\\xuexi\\javaio\\test.txt"); try { fr = new FileReader(fileRead); br = new BufferedReader(fr); try { String readLine = br.readLine(); while (readLine != null) { System.out.println(readLine); readLine = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ try { br.close(); fr.close(); } catch (IOException e) { e.printStackTrace(); } } } }写代码:
public class Write { private static BufferedWriter bw = null; /** * @param args * 在这里用到buffer 缓冲流 来包装 节点流 具体见01_IO之 file的filecreat类 * * 此类做的方法是写文件文本内容 */ public static void main(String[] args) { File fileWrite = new File("D:\\xuexi\\javaio\\test.txt"); try { bw = new BufferedWriter(new FileWriter(fileWrite,true)); bw.write(new Date()+"我是插入的数据"); bw.flush();//写操作先flush一遍 bw.close(); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- spring bean 使用继承
- Java代理-JAVA动态代理,CGLIB动态代理
- java中接口的定义原则
- Eclipse安装Spring插件
- Struts2的dispatcher chain redirect和redirectAction的区别
- Spring 中log4j的配置和使用
- 安卓高手之路之java层Binder
- eclipse 项目中搜索资源(类方法,文件名,文件中的字符串)(图解)
- eclipse 项目中搜索资源(类方法,文件名,文件中的字符串)(图解)
- eclipse 项目中搜索资源(类方法,文件名,文件中的字符串)(图解)
- eclipse 项目中搜索资源(类方法,文件名,文件中的字符串)(图解)
- Socket分片:基于Netty的Java实现
- JavaMail发送邮件
- myeclipse的作用
- Java层Binder使用(ServiceManager)
- spring框架中web.xml的配置详解
- java反射技术
- 【Leetcode】:268. Missing Number 问题 in JAVA
- Java内省详解
- java的comparable接口和comparator接口