IO流

一.输入输出(I/O)

定义:程序与外部设备或其他计算机进行交互操作

二.流

2.1 概念:在计算机的输入输出操作中各部件之间的数据流动

2.2据传输方向分为:输入流à从存储介质或数据通道中读取数据

                 输出流à往存储介质或数据通道中

2.3流序列中的数据形式:原始二进制数据/编码处理数据

三.流特性

     


四.流的相关类


五.缓冲流


六.代码实例

6.1 缓冲流

 1 import java.io.*;
 2 
 3 public class Demo_BufferReader1 
 4 {
 5     public static void main(String[] args) 
 6     {
 7         String online;//缓冲字符流数据写入接收变量
 8         int count=0;//行数计数变量
 9        try {
10             //创建文件输入字符流
11             FileReader a=new FileReader("C:/Users/罗摩衔那/Desktop/write.txt");
12             //创建缓冲流对象,将文件流对象作为参数
13             BufferedReader u=new BufferedReader(a);
14             while((online=u.readLine())!=null)//每次读取一行
15             {
16                 System.out.println(online);
17                 count++;
18             }
19             System.out.println("读取了: "+count+"行!");
20             u.close();
21     } catch (IOException e) {
22         System.out.println(e);
23     }
24     }
25 }

6.2 文件路径

 1 import java.io.File;
 2 
 3 public class Demo_File 
 4 {
 5    public static void main(String[] args) 
 6    {
 7        /*
 8         * 1.创建File对象
 9         * 相对路径:相对于我们的项目路径
10         * 绝对路径:"D:/编程视频笔记/JAVA/java自学笔记"
11         */
12     File file=new File("D:/编程视频笔记/JAVA/java自学笔记");
13     
14     //2.获取File对象的绝对路径
15     String absolutePath=file.getAbsolutePath();
16     System.out.println(absolutePath);
17      }
18 }

 

 6.3  输入流

 1 /*
 2  * 文件的写入
 3  */
 4 import java.io.FileNotFoundException;
 5 import java.io.FileReader;
 6 
 7 public class Demo_FileRead 
 8 {
 9    public static void main(String[] args) 
10    {
11       //创建字符流输入对象
12        FileReader ft=null;
13        int c=0;
14        //指定字符流指定的输入源
15        try {
16         ft=new FileReader("C:/Users/罗摩衔那/Desktop/write.txt");
17         //判断是否存在16进制字符流
18         while((c=ft.read())!=-1)
19         {
20             System.out.print((char)c);
21         }
22         ft.close();
23     } catch (FileNotFoundException e) {
24         e.printStackTrace();
25         System.out.println("找不到指定文件!");
26         System.exit(-1);//终止异常程序
27     }catch(Exception e)
28        {
29         e.printStackTrace();
30         System.out.println("文件读取错误!");
31         System.exit(-1);
32        }
33    }
34 }

6.4 输出流

 

 1 import java.io.*;
 2 /*
 3  * 文件的写出
 4  */
 5 public class Demo_FileWrite 
 6 {
 7    public static void main(String[] args) 
 8    {
 9      //创建FileWrite对象
10        FileWriter r=null;
11        //确定输出源目的文件
12        try {
13         r=new FileWriter("C:/Users/罗摩衔那/Desktop/write.txt");
14         //写入20个数
15         for(int i=0;i<5000;i++)
16         {
17             r.write(i);//成功写入C:/Users/罗摩衔那/Desktop/write.txt文件夹
18         }
19         r.close();//关闭输出流
20         System.out.println("文件写入成功!");
21     } catch (IOException e) {
22         e.printStackTrace();
23     }
24    }
25 }

 6.5 文件拷贝

 1 /*
 2  * 文件拷贝
 3  */
 4 public class Demo_output
 5 {
 6    public static void main(String[] args) 
 7    {
 8        int b=0;
 9     //创建两个输入输出流对象
10        FileInputStream in=null;
11        FileOutputStream out=null;
12       
13        
14        //指定输入输出源文件
15        try {
16            //可能出现找不到文件的情况
17            in=new FileInputStream("C:/Users/罗摩衔那/Desktop/java.txt");
18            out=new FileOutputStream("C:/Users/罗摩衔那/Desktop/write.txt");
19            
20            //从输入源复制数据到输出源
21            //判断是否有二进制数据
22            while((b=in.read())!=-1)//证明存在二进制数据,此处可能存在文件复制错误的情况
23            {
24                out.write(b);
25            }
26            in.close();
27            out.close();
28     } catch (FileNotFoundException e) {
29         e.printStackTrace();
30         System.out.println("找不到指定文件!");
31         System.exit(-1);//终止异常程序
32     }catch(IOException e)
33        {
34         e.printStackTrace();//打印堆栈轨迹
35         System.out.println("文件复制错误!");
36         System.exit(-1);//终止异常程序
37        }
38        System.out.println("文件复制成功!");
39    }
40 }

6.6 文件操作

 1 public class Demo01_File 
 2 {
 3    public static void main(String[] args) throws IOException
 4    {
 5     File f=new File("aaa/bbb/ccc");
 6     //创建单级文件夹
 7     boolean flag=f.mkdir();
 8     System.out.println(flag);
 9     //创建多级文件夹
10     flag=f.mkdirs();
11     System.out.println(flag);
12     //创建文件
13     flag=f.createNewFile();
14     System.out.println(flag);
15     }
16 }

 1 import java.io.*;
 2 import java.text.SimpleDateFormat;
 3 import java.util.Date;
 4 public class Floder
 5 {
 6    public static void main(String[] args) 
 7    {
 8        //File file = delete();
 9        //File(file);
10      
11      File f=new File("C:/Users/罗摩衔那/Desktop/reader.txt");
12      //1.获取绝对路径
13      String absolute=f.getAbsolutePath();
14      System.out.println(absolute);
15      
16      //2.public String getPath()
17      //获取构造方法传入的路径
18      String path=f.getPath();
19      System.out.println(path);
20      
21      //获取长度
22      long len=f.length();
23      System.out.println(len);
24      
25      //最后一次修改时间
26      long modify=f.lastModified();//返回的是一个毫秒数
27      //转换成date时间格式
28      Date date=new Date(modify);
29      //格式转换为字符串形式
30      SimpleDateFormat sim=new SimpleDateFormat("y年M月d日  h:m:s");
31      String result=sim.format(date);
32      System.out.println(result);
33      
34    }
35 
36 private static void File(File file) {
37     //是否为文件夹
38      boolean directory=file.isDirectory();
39      System.out.println(directory);
40      //是否为文件
41      boolean file1=file.isFile();
42      System.out.println(file1);
43      //是否存在
44      boolean exit=file.exists();
45      System.out.println(exit);
46 }
47 
48 private static File delete() {
49     /*
50         * public boolean delete()
51         * 删除此抽象路径名表示的文件或目录
52         * 如果此路径表示
53         */
54      File file=null;
55      //直接删除
56      file=new File("C:/Users/罗摩衔那/Desktop/aa.txt");
57      file.delete();
58      //删除文件夹,文件夹必须是空文件夹才能删除
59      file=new File("C:/Users/罗摩衔那/Desktop/aa");
60      file.delete();
61     return file;
62 }
63 }

原文地址:https://www.cnblogs.com/zjm1999/p/10116128.html