IO流之文件读写操作

在IO流中,文件的读写算是最基本的操作了,这样就可以在一定程度上丰富了功能,文件的读写主要有下面几个步奏:
1:定义文件对象
2:定义输入或者输出流
3:循环遍历以字节的方式输入或者输出

package IO;
import java.io.*;

public class writeoupt {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        File f7=new File("f:\\ff\\write.txt");

            System.out.print("该文件已经存在");

//          开始写入
//          字节输出流
            FileOutputStream fis1=null;
            try {
                fis1=new FileOutputStream(f7);
                String str1="您好";
                byte []bytes=new byte[1024];

                fis1.write(str1.getBytes());
            } catch (Exception e) {
                // TODO: handle exception
            }finally{
                try {
                    fis1.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
//      写入借宿,如果没有文件会自动创建
            FileInputStream fis2=null;
            try {
                fis2=new FileInputStream(f7);
                int count=0;
                byte []bytes1=new byte[1024];
                System.out.println("已经天津爱的内容如下:");
                while((count=fis2.read(bytes1))!=-1){
                    String s=new String(bytes1,0,count);
                    System.out.print(s);
                }
            } catch (Exception e) {
                // TODO: handle exception
            }finally{
                try {
                    fis2.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值