JAVA IO 流 笔记 01

  学生笔记!

1.字节流


运用FileOutputStream和FileInputStream类读写文本文件(只提供了读写字节的方法)

写文件

public class TestByteIO {
    public static void main(String[] args) {
        File f = new File(“c:\\temp.txt”); //创建文件对象
<span style="white-space:pre">	</span>//File f = new File("D:"+File.separator+"aa"+File.separator+"file.txt");指定目录--(mkdir  mkdirs)
<span style="white-space:pre">	</span>
        try {
              //通过文件对象创建文件输出流
            FileOutputStream fileout = new FileOutputStream(f);
            String outstr = "这是写入文件的数据";
            byte buf[] = outstr.getBytes();//将字符串转化成字节
            fileout.write(buf);//将字节写入文件
            fileout.close();  //关闭输出流
        } catch (IOException ex) {
           ex.printStackTrace();
        }
f.exists();  判断创建的文本对象是否存在

读文件

 try {
           // 通过文件对象创建文件输入流
            FileInputStream filein = new FileInputStream(f);
          //创建字节数组,用于接收从文件中读取的字节
            byte buf[] = new byte[1024];
            String instr = ""; //接收字节转化的字符串
            int length = filein.read(buf);
            instr = new String(buf,0,length);//将字节转化成字符串   
            System.out.println(instr);
            filein.close();  //关闭输入流
        } catch (IOException ex) {
           ex.printStackTrace();
        }
    }
}
其他类型的数据:

DateInputStream和DateOutputStream

语法:

File f = new File(“D:\\temp.txt”);

FileInputStream filein = new FileInputStream(f);

DateInputStream datn = new DateInputStream(filein);
DateOutputStream语法一样.
源代码:

public class TestDataIO {
    public static void main(String[] args) {
        File f = new File("c:\\temp.txt");
        try {
            //   通过文件对象创建文件输出流
            FileOutputStream fileout = new FileOutputStream(f);
            DataOutputStream dataout = new DataOutputStream(fileout);
            int id = 1;
            String name = "张三";
            double money = 5000.0;
            dataout.writeInt(id);//写入整型数据
            dataout.writeUTF(name);//写入字符型数据
            dataout.writeDouble(money);//写入双精度数据
            dataout.close();//关闭流
        } catch (IOException ex) {
            ex.printStackTrace();
        }

try {
           //   通过文件对象创建文输入流
           FileInputStream filein = new FileInputStream(f);
           DataInputStream datain = new DataInputStream(filein);
            int id = 0;
            double money = 0;
            String name = "";
            id = datain.readInt();//读取整型数据
            name = datain.readUTF();//读取字符串数据  
            money = datain.readDouble();//读取双精度数据
            System.out.println("帐户信息为:" + id + "  " + name + "  " + money);
            datain.close();//关闭流
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

2.字符流

<1.>Reader和Writer是用于读取或者写入字符的字符流

FileReader和FileWriter作为从字节流到字符流的转换

代码: 

 File f = new File("c:\\temp.txt");
        try {
            FileWriter fw = new FileWriter(f);//创建文件字符流对象

int age = 20;

String name = "李四";

Integer integerNum = new Integer(age);//将 int 类型的age 转换成字符串

String str = integerNum.toString();

//写文件

fw.write(name);

fw.write(str);

 //关闭流
           
fw.close();
        } catch (IOException ex) {
             ex.printStackTrace();
        }

try{
         FileReader fr = new FileReader(f);//创建文件字符流对象
         //创建字符数组,接收读取的数据
         char[] buf = new char[1000];
         //读取文件内容到buf,返回实际数据的长度
         int length = fr.read(buf);//read()  -- 读取字符,如果已到达流的末尾,则返回 -1
         //将字符数组转成字符串
         String readStr = new String(buf,0,length);
         System.out.println(readStr);
         //关闭流
         fr.close();    
        }catch(IOException ex){
            ex.printStackTrace();
        }
  }
}

read() 代码:

int c = 0;

 while ((c = fr.read()) != -1) {
        System.out.print((char)c);
      }

<2.>BufferedReader:从字符输入流中读取文本,缓冲各个字符,提供字符,数组和行的高效读取
常用构造方法

BufferedReader bufferedin = new BufferedReader(Reader对象);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值