IO
来源:互联网 发布:catia软件 编辑:程序博客网 时间:2024/06/05 15:45
一、Input 输入 Output 输出
二、新建一个文件
File file =new file ("d:\\aa\\.hello.txt");
boolean flag=file.creatNewFile();//判断是否创建
三、创建文件夹
File file =new file ("d:\\a");(注意File类不仅可以代表一个文件也可以代表一个文件夹)
boolean flag=file.mkdirs();
注意:mkdir只能创建一级目录 而mkdirs可以创建多级目录
四、常用方法
file.exists()判断是否存在
file.delete();删除文件夹或目录
file.getName();文件名
file.getParent();文件所在目录
fIle.getPath();返回相对路径
file.list();列出文件夹下的所有文件
五、IO流的分类
一、按照数据流向分为:1.输入流 2.输出流
二 、按每次存取的单位分为:1.字节流 2.字符流
三、 按照管道是否有直接和数据源相连 1.节点流 2.处理流
六.字节读
1.建立通道
FileInputStream fis = null;
fis = newFileInputStream("c:\\tt\\HelloIO.txt");
2.利用read循环读:read()读一个字节 返回值int 把读入的一个字节 存在int类型的低八位
while((b=fis.read())!=-1){System.out.print((char)b)};
3.关闭通道
七、字符读
第一步、打开通道
FileReader fr = null;
fr = newFileReader("c:\\io\\HelloIo.txt");
第二步:利用read循环读:read()每次读一个字符 英文占一个字节 中文是一个字符 占2个字节
while((b=fr.read())!=-1){count++;System.out.print((char)b);}
第三步、关闭通道
八、字节写
String s="世界";
byte[] byteArr=null;
byteArr=s.getBytes("gbk");//产生的文件编码方式是由getBytes()方式决定的
第一步、建立通道
FileOutputStream fos = null;fos = new FileOutputStream("c:\\io\\HelloIO1.txt",true);//如果想在源文件中追加字符 多加一个true
第二步、利用write写
fos.write(byteArr);
第三步、关闭通道
九、字符写
//char c ='我';//char[] charArr = new char[]{'你','好','啊','!'};String s = "我喜欢学java";
第一步:建立通道
FileWriter fw = null;
fw = new FileWriter("c:\\io\\testFileWriter.txt");//加true追加
第二步、利用read循环读 利用whrit写
fw.write(s)
第三步、关闭通道
十、转换流
InputStreamReader 输入字节流--->输入字符流
OutputStreamWriter输出字节流 --->输出字符流
FileInputStream fis=null;try {fis=new FileInputStream("d:\\IO\\hello.txt");} catch (FileNotFoundException e) {System.out.println("未找到文件");e.printStackTrace();}Reader reader=new InputStreamReader(fis);try {char c=(char)reader.read();System.out.println(c);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}
FileOutputStream fos=null;try { fos =new FileOutputStream("d:\\IO\\hello.txt");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}Writer writer=new OutputStreamWriter(fos);try {writer.write('好');writer.close();} catch (IOException e) {e.printStackTrace();}
十一、按行读写
按行读:BufferedReader的readLine()方法
按行写:PrintWriter的println()方法
//1.建立通道FileReader fis=null;BufferedReader fos=null;String s;try {fis=new FileReader("d:\\io\\hello.txt");fos=new BufferedReader(fis);} catch (FileNotFoundException e) {System.out.println("文件没有找到");e.printStackTrace();}FileWriter fw=null;PrintWriter pw=null;try {fw=new FileWriter("d:\\io\\he.txt");pw=new PrintWriter(fw);} catch (IOException e) {System.out.println("文件夹没有找到");e.printStackTrace();}try {while((s=fos.readLine())!=null){System.out.println(s);//ready()是判断有没有下一行//如果有返回 true 没有返回falseif(fos.ready()){pw.println(s);//往复制文件里面写文字}else{pw.print(s); //利用while循环一行一行的写}} }catch (IOException e) {System.out.println("文件复制失败");e.printStackTrace();}if(fos!=null){try {fos.close();} catch (IOException e) {System.out.println("关闭失败");e.printStackTrace();}}if(pw!=null){pw.close();}if(fw!=null){try {fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
十二、缓冲流
功能:减少和硬盘打交道次数提高读写效率
缓冲流中的数据什么时候会写到文件中?
1.当缓冲流关闭时
2.调用Bufferedwritter的flush()方法
3.缓冲区满 实质是char[] charArr=new char[8192];
十三、数据流
数据流以基本类型String、byte数组为单位读写
FileOutputStream fos=new FileOutputStream("d:\\io\\ok.txt");DataOutputStream dos=new DataOutputStream(fos);dos.writeLong(100000);dos.writeChars("我爱太阳,你爱我"); FileInputStream fis=new FileInputStream("d:\\io\\ok.txt"); DataInputStream dos1=new DataInputStream(fis); long a=dos1.readLong(); char b=dos1.readChar(); System.out.println(a);System.out.println(b);
十四、对象流
以对象为单位读写
序列化:对象保存到文件中
反序列化:把对象从文件中读取出来
知识点:1.序列化的对象所属的类一定要实现Serialzable接口 否则运行会报错 不可序列化对象
2.teansient关键字 只能用来修饰属性 临时的 用这个修饰符修饰的属性不会被序列化到文件中
3.序列化ID 序列化id 唯一的标识一个字节码文件序列化时使用的。class文件,反序列化的时候也要用同一个。class文件
序列化具体的应用:
RMI 远程方法调用
RPC 远程过程调用
- IO
- IO
- io
- IO
- io
- IO
- io
- IO
- IO
- IO
- io
- IO
- IO
- IO
- IO
- IO
- IO
- IO
- 常见C/C++笔试、面试题
- [poj2234]Matches Game nim游戏
- 奶爸的生活
- 【Kmp求字符串前缀在字符串出现的次数】51nod 1277 字符串中的最大值
- Flowery Trails 多条最短路边长之和
- IO
- JDBC- 正确关闭数据库连接
- 输入法切换 圆角与半圆
- 7.24排序讲座总结
- POJ 3096: Surprising Strings (水)
- 解题报告:HDU_5656 CA Loves GCD DP
- 对Redis的初步理解
- 使用CSS实现三列布局
- Java 设计模式_单例模式