字节流和字符流

  1.完成文件和目录操作(File)

public static void main(String[] args) {
//        完成文件和目录操作(File)
        File f1=new File("D:\\t.txt");
        File f2=new File("k.txt");
        File f3=new File("D:\\abt");
        File f4=new File("D:\\abt\\egd");
        try {
            boolean b1=f1.createNewFile();
            boolean b2=f2.createNewFile();
            boolean b3=f3.mkdir();
            boolean b4=f4.mkdirs();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 2.字节流的读取和写入
public static void main(String[] args) {
//        字节流的读取和写入
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("d:\\ye.jpg");
            fos = new FileOutputStream("d:\\yyy.jpg");
            byte[] b = new byte[1024];
            int legth = 0;
            while ((legth = fis.read(b)) != -1) {
                fos.write(b, 0, legth);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }
 3. 字符流的读取和写入​​​​​​​
public static void main(String[] args) {
//        字符流的读取和写入
        BufferedReader br=null;
        BufferedWriter bw=null;
        try {
             br = new BufferedReader(new FileReader("d:\\abs.txt"));
             bw=new BufferedWriter(new FileWriter("d:\\abC.txt"));

             String t="";
            while ((t=br.readLine())!=null){
                 bw.write(t);
                 bw.newLine();
             }
            bw.flush();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }

    }
 4.字节流向字符流的转换
public static void main(String[] args) {
//        字节流向字符流的转换
        BufferedWriter bw=null;
        BufferedReader br =null;
                 br = new BufferedReader(new InputStreamReader(System.in));
        try {
              bw = new BufferedWriter(new FileWriter("d:\\abs.txt"));
              String t="";
              while ((t=br.readLine())!=null){
                  if (t.equals("kkk")){
                      break;
                  }
                  bw.write(t);
                  bw.newLine();
            }
              bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值