Java中使用IO(输入输出)来读取和写入,读写设备上的数据、硬盘文件、内存、键盘......,根据数据的走向可分为输入流和输出流,这个走向是以内存为基准的,即往内存中读数据是输入流,从内存中往外写是输出流。

根据处理的数据类型可分为字节流和字符流

1.字节流可以处理所有数据类型的数据,在java中以Stream结尾

2.字符流处理文本数据,在java中以Reader和Writer结尾。

我们来看个IO流的详解图:

IO流的本质是对字节和字符的处理,那么我们平时也是用来处理文件的,就从文件处理开始接触这方面的知识。

1.文件操作(创建文件和文件夹,查看文件)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//创建一个文件路径
File file = new File("D:\\testData.txt");
if(file.exists()){
//得到文件路径
System.out.println(file.getAbsolutePath());
//得到文件大小
System.out.println("文件大小:"+file.length());
}
//创建文件和创建文件夹
File file1 = new File("d:\\iotest.txt");
if(!file1.exists())
{
    try {
        file1.createNewFile();
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}else{
    System.out.println("文件已存在");
}
//创建文件夹
File file2 = new File("d:\\testIO");
if(file2.isDirectory())
{
    System.out.println("文件夹存在");
}else{
    file2.mkdir();
}
 
//列出一个文件夹下的所有文件
File f = new File("d:\\testIO");
if(f.isDirectory())
{
    File lists[] = f.listFiles();
    for(int i=0;i<lists.length;i++)
    {
        System.out.println(lists[i].getName());
    }
}

常用字节流FileInputStream和FileOutputStream:

FileInputStream:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:\\testData.txt");
            byte bytes[]=new byte[1024];
            int n=0;
            while((n=fis.read(bytes))!= -1){
                String str = new String(bytes,0,n);
                System.out.print(str);
            }
        catch (Exception e) {
            e.printStackTrace();
        finally{
            try {
                fis.close();
            catch (IOException e) {
                e.printStackTrace();
            }
        }

 

查看输出:

FileOutputStream:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("D:\\testData.txt");
            String str = "报效国家,舍生忘死";
            byte bytes[] = str.getBytes();
            fos.write(bytes);
        catch (Exception e) {
            e.printStackTrace();    
        finally {
            try {
                fos.close();
            catch (Exception e2) {
                e2.printStackTrace();
            }
        }

查看一下:

如果是续写文件,则可以加上参数:

字符流FileReader和FileWriter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//字符流
        //文件写出  输入流
        FileReader freader = null;
        //写入到文件  输出流
        FileWriter fwriter = null;
        try {  
            //创建输入对象
            freader = new FileReader("d:\\testData.txt");
            //创建输出对象
            File f1 = new File("e:\\testData.txt");
            if(!f1.exists()){
                f1.createNewFile();
            }
            fwriter = new FileWriter(f1);
             
            //读入到内存
            char chars[] = new char[1024];
            int n=0;
            while((n=freader.read(chars))!= -1)
            {
                fwriter.write(chars);
                //System.out.println(chars);
            }
             
             
             
        catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }finally{
            try{
            freader.close();
            fwriter.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
         
         
         
        //缓冲字符流  bufferedReader  bufferedWriter
        BufferedReader bfreader = null;
        try {
            FileReader freader = new FileReader("d:\\testData.txt");
            bfreader = new BufferedReader(freader);
            //循环读取
            String s ="";
            while((s=bfreader.readLine())!= null)
            {
                System.out.println(s);
            }
        catch (Exception e) {
            // TODO: handle exception
        }

  

最新文章

  1. javaScript生成二维码(支持中文,生成logo)
  2. 文件操作之FileOpenPicker、FileSavePicker和FolderPicker
  3. IE11下Forms身份认证无法保存Cookie的问题
  4. javascript继承(八)-封装
  5. C puzzles详解【16-20题】
  6. Unity3D开发Windows Store应用程序 注意事项
  7. 1069: [SCOI2007]最大土地面积 - BZOJ
  8. 让Eclipse和NetBeans共享同一个项目
  9. [LeetCode#271] Encode and Decode Strings
  10. postman 第6节录制case
  11. Android高级控件(四)——VideoView 实现引导页播放视频欢迎效果,超级简单却十分的炫酷
  12. 控制结构(1): 分枝/叶子(branch/leaf)
  13. Python之jieba库的使用
  14. 行业相关的webgl项目
  15. ssh框架 基本整合
  16. Python *Mix_w4
  17. MySQL5.6主从复制搭建基于日志(binlog)
  18. 在spring MVC 中关于session失效的判断 有一个类SessionStatus
  19. 9 - Python函数定义-位置参数-返回值
  20. SpringBoot 事务隔离性和传播性

热门文章

  1. Python3-paramiko-SFTP上传文件夹到多台远程服务器
  2. 2015 编程之美初赛第一场 AC题
  3. 网络基础篇(一)--TCP/IP协议族
  4. [WebView学习之二]:使用Web Apps 支持不同分辨率屏
  5. C语言之基本算法40—字符串删除元音字母倒序输出
  6. Cocos2d-X开发中国象棋《三》開始场景的实现
  7. 【BASH】bash shell的使用实例
  8. Oracle 静态监听注冊具体解释
  9. Building Maintainable Software-java篇之Couple Architecture Components Loosely
  10. 邪恶的C++