8月8日 Java的IO机制

文件的创建格式:

        //第一种构造方式
        File f1 = new File("E:\\io\\done\\io.txt");// 第一个\作为转义
        //第二种构造方式
        File f2 = new File("E:\\io\\done","io.txt");
        //第三种构造方式
        File f3 = new File("E:\\io\\done");
        File f4 = new File(f3,"io.txt");
        //第四种 URI网络格式,只看前三种

 

课上所学程序代码:

文件操作:

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileDome {

    public static void main(String[] args) throws Exception {
//        //第一种构造方式
//        File f1=new File("E:\\io\\dome\\io.txt");
//        //第二种构造方式
//        File f2=new File("E:\\io\\dome\\", "io.txt");
//        //第三种构造方式
//        File f3=new File("E:\\io\\dome\\");
//        File f4=new File(f3, "io.txt");
//        File f5=new File("E:\\io\\test");
//        //先判断,在删除
//        if(f5.exists()) {
//            f5.delete();
//        }
//        //创建文件夹
//        boolean flag=f5.mkdir();
//        System.out.println(flag);
        //绝对路径
//        File f6=new File("E:\\io\\test\\test.txt");
//        //创建文件
        boolean flag=f6.createNewFile();
        System.out.println(flag);
//        //获取文件的名字
//        System.out.println(f6.getName());
//        //获取文件的路径
//        System.out.println(f6.getParentFile());
//        //获取绝对路径
//        System.out.println(f6.getPath());
        //相对路径
//        File f7=new File(".\\file\\test.txt");
        f7.createNewFile();
//        //获取相对路径
//        System.out.println(f7.getPath());
//        //获取绝对路径有点
//        System.out.println(f7.getAbsolutePath());
//        //获取绝对路径去掉点
//        System.out.println(f7.getCanonicalPath());
//        File f8=new File("E:\\io\\test\\aa.docx");
        //获取文件大小
//        System.out.println(f8.length());
        //获取文件最后修改时间
//        long lo=f8.lastModified();
//        Date date=new Date(lo);
//        SimpleDateFormat sid=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String str=sid.format(date);
//        System.out.println(str);
//        File f9=new File("E:\\io\\test\\io\\test\\io\\test");
//        //创建多层
//        f9.mkdirs();
        File f10=new File("e:"+File.separator+"io"+File.separator+"file.txt");
        f10.createNewFile();
    }

}

 

IO流操作:

package com.dhy.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class IODome {
    public static void main(String[] args) throws Exception {
        int data=0;
        FileInputStream fin=null;
        FileOutputStream fos=null;
        //第一种创建方式
//        File f=new File(".\\file\\my.txt");
//        try {
//            fin=new FileInputStream(f);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        //第二种构造方式
        fin=new FileInputStream(".\\file\\dog.jpg");
        fos=new FileOutputStream(".\\file\\bb.jpg");
        while((data=fin.read())!=-1) {
            fos.write(data);
        }
        fos.close();
        fin.close();
    }
}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值