I/O模式

文件:存储数据的一种方式;

File类

读文件

//读文件
        try {
            FileInputStream fileInputStream=
                    new FileInputStream("f:/Io/test.txt");
            byte[] data = new byte[1024];
            try {
                int len;
                while((len=fileInputStream.read(data))!=-1){
                    String string = new String(data,0,len);
                    System.out.println(string);
                }
                fileInputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

写文件

try {
            FileOutputStream fos = 
                    new FileOutputStream("f:/Io/abc.txt");
            try {
                fos.write("abc".getBytes());
                fos.flush();
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

判断文件是否存在

if(!file3.exists()){
            System.out.println("bucunzai");

创建文件

File file = new File("f:"+File.separator
                            +"io"+File.separator+"old");//找到所指文件夹
        if(!file.exists()){
            file.mkdir();//如果该文件夹不存在则创建一个文件夹
        }
        if (file.exists()) {
            File file2 = new File(file.getAbsolutePath()
                                    +File.separator+"old.txt");//如果存在所述文件夹,判断所述文件是否存在
            if(!file2.exists()){ // 如果文件不存在
                try {
                    FileOutputStream fileOutputStream=new FileOutputStream(file2);//创建新的输出文件;
                    try {
                        fileOutputStream.write("hello file".getBytes());
                        fileOutputStream.flush();
                        fileOutputStream.close();//关闭输出流;

                    }

删除文件

if(file.exists()){
        file.delete();
        }
        if (!file.exists()) {
            System.out.println("no.....");
        }

BufferReader

try {
            FileReader fReader = 
                    new FileReader("f:/Io/test.txt");
            //流链   
            BufferedReader bReader = new BufferedReader(fReader);//高级流,提供了一个缓冲区;
            try {
                String string = null;
                while((string = bReader.readLine())!=null){
                    System.out.println(string);
                }
                fReader.close();//先关里面的
                bReader.close();//再关外面的
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }//一次读一行
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

BufferWriter

FileWriter fWriter;
        try {
            fWriter = new FileWriter("f:/Io/test.txt");
            BufferedWriter bWriter = new BufferedWriter(fWriter);
            bWriter.write("hello file");
            bWriter.newLine();
            bWriter.write("ok");
            fWriter.flush();
            bWriter.flush();
            fWriter.close();
            bWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值