zoukankan      html  css  js  c++  java
  • file类和io流

    一、file类

      file类是一个可以用其对象表示目录或文件的一个Java.io包中的类

     1 import java.io.File;
     2 import java.io.IOException;
     3 public class Test3 {
     4 
     5     public static void main(String[] args) throws IOException {
     6         
     7         File f = new File("iofile.txt");            //file类创建对象
     8         
     9         System.out.println(f.exists());                //判断是否存在
    10         
    11         if(f.exists()){
    12             f.delete();                            //删除文件
    13             System.out.println(f.delete());
    14         }
    15         
    16         f.createNewFile();                        //创建新文件
    17         System.out.println(f.createNewFile());
    18         
    19         System.out.println(f.getName());        //获取名字
    20         
    21         System.out.println(f.getAbsolutePath());//绝对路径
    22         
    23         System.out.println(f.getParent());        //父目录
    24         
    25         System.out.println(f.length());            //大小
    26         
    27         System.out.println(f.isAbsolute());     //是否为绝对路径
    28     
    29 
    30     }
    31 
    32 }

    二、IO流

      IO流是一组从源到目的地的有序数据序列

      1、inputstream

        

     1         import java.io.FileInputStream;                //导入包
     2         import java.io.IOException;                      //抛出异常
     3 
     4         public class Input{
     5 
     6             public static void main(String[] args) {
     7 
     8                 InputStream();
     9             }
    10 
    11             public static void InputStream() {        //封装方法
    12                 FileInputStream f = new FileInputStream("D:\workspace\maji\input.txt");            //创建输入流类的对象
    13 
    14                     byte[] a = new byte[300];          //新建一个300长度的数组对象  
    15                     int lenth = 0;
    16                     while ( lenth != -1) {        //检测是否读完源文件
    17 
    18                         System.out.println(new String(a, 0, lenth));                //把最多lenth个的数据读入byte数组a中
    19 
    20                         lenth = f.read(a);        //读取数组长度
    21                     }
    22                     
    23                         f.close();                //关闭输入流
    24 
    25                 }
    26 
    27             }    

        2、output

    import java.io.FileOutputStream;                        //导入包
    import java.io.IOException;
    
            public class Test4 {
    
                public static void main(String[] args) throws IOException {
                    outputStream();
    
                }
    
                public static void outputStream() throws IOException {                    //封装方法
                    FileOutputStream f = new FileOutputStream("Test.txt");//创建输出流类的对象                
                    
                    
                        String arr = "hello world!";                //字符串
                        byte[] a = arr.getBytes();                    //将此arr转为为 byte存入a数组中                
                        f.write(a);                                    //读取a
    
                        f.close();                                    //结束
                        
    
                    }
    
            }
  • 相关阅读:
    Python操作redis数据库
    计算机基础与操作系统
    git使用快速入门
    RESTful API设计规范
    Python数据分析常用的库总结
    关于Cookie和Session
    一个开发的Linux使用心得总结
    排序算法与查找算法
    Django的form,model自定制
    Redis基础、高级特性与性能调优
  • 原文地址:https://www.cnblogs.com/-maji/p/7082344.html
Copyright © 2011-2022 走看看