ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

IO:输入流InputStream 输出流:OutputStream

2021-01-30 22:01:04  阅读:181  来源: 互联网

标签:OutputStream System InputStream IO import println new buf out


文件:硬盘上的文件 txt  docx 电影 图片
本章的核心:通过IO来操作文件


File

package IOProject;

import java.io.File;
import java.io.IOException;

public class Demo01 {
    public static void main(String[] args) {
        //file可以代表一个不存在的文件
//        File file = new File("F:/JavaSE/abc.txt");
        File file = new File("hello.txt");
        System.out.println("文件分隔符:"+File.separator);
        boolean flag = file.exists();

        System.out.println(file.isFile() == true ? "是文件" : "其他类型");
        System.out.println("绝对路径--" + file.getAbsoluteFile());
        System.out.println("相对路径--" + file.getPath());
        System.out.println("文件名字--" + file.getName());
        System.out.println("文件长度--" + file.length());

        try {
            if (flag) {
                //file.delete();
                //彻底删除(不经过回收站)
               // System.out.println("删除成功");
            } else {
                file.createNewFile();
                System.out.println("创建成功");
            }

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

相对路径/绝对路径:

  • 如果FIle("绝对路径"):getPath() 和getAbsolutePath()的结果一致,打印的都是绝对路径
  • 如果FIle("相对路径"):getPath() 打印相对路径;getAbsolutePath()打印绝对路径

 

 

 

 

流:是一种FIFO的数据结构(First  In  First  Out)

分类:

  

 

说明:

  ①字节流就是 将内容转为了字节形式进行传输, 1 字节 ->8二进制 ,二进制可以传输任何类型的数据,因此字节流也可以传输任何类型的数据。

  ②字节流是8位通用字节流(   1 字节 ->8二进制 ) (字节流可以处理任何类型,处理文本文件以外的其他文件) ;字符流是16位的unicode字符流 (只用于处理字符,处理文本文件)


  ③在while循环中 分批次传输时,字节流使用的缓冲区是 byte[],字符流使用的缓冲区是 char[]


 

输入

package IOProject;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
    public static void main(String[] args) throws IOException {
        InputStream in = null;
        try {

            in = new FileInputStream(new File("F:/JavaSE/cesi/abc.txt"));
//            InputStream in = new FileInputStream("F:/JavaSE/abc.txt");
            System.out.println("流的长度:"+in.available());
            byte[] buf = new byte[in.available()];
            in.read(buf);//将文件abc.txt内容读取到buf中
            //buf:byte[] - >String
            System.out.println(new String(buf));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            in.close();
        }
    }
}

输出

package IOProject;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class OutputStreamDemo {
    public static void main(String[] args) throws IOException {
        OutputStream out = null;
        try {
            out = new FileOutputStream("F:/JavaSE/cesi/xyz.txt");
            out.write("helloworld".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            out.close();
        }
    }
}

文件复制

package IOProject;

import java.io.*;

public class FileCopy {
    public static void main(String[] args) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream("F:/JavaSE/笔记/异常.png");
            out = new FileOutputStream("F:/JavaSE/cesi/异常图片.jpg");
            //abc.txt->内存->xyz.txt
            byte[] buf = new byte[10];
            int len = -1;
            while ((len = in.read(buf)) != -1) {
                out.write(buf,0,len);

            }
            System.out.println("copy成功");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) out.close();
                if (in != null) in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

写读文件末尾位置过程

 

使用字符流进行文件的复制

package IOProject;

import java.io.*;

public class FileCopyCharactor {
    public static void main(String[] args) {
        //1.文件->内存(Reader)

        Reader reader = null;
        Writer writer = null;
        try {
            reader = new FileReader("F:/JavaSE/cesi/个人介绍.txt");
            writer = new FileWriter("F:/JavaSE/cesi/个人完整介绍.txt");
            char[] buf = new char[4];
            int len = -1;
            StringBuffer sb = new StringBuffer();
            while ((len = reader.read(buf)) != -1) {
                sb.append(buf, 0, len);
            }
            System.out.println(sb);
            //2.在内存中  替换占位符

            String content = sb.toString();
            content = content.replace("{name}", "张三")
                    .replace("{enterprise}", "xm")
                    .replace("{weixin}", "123321");
            //3.将替换后的内容  输出到文件 ,内存->文件(Writer)
            writer.write(content);
            System.out.println("输出成功");

            writer.flush();//将管道中的数据 刷出到  文件中

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (writer != null) writer.close();
                if (reader != null)reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

标签:OutputStream,System,InputStream,IO,import,println,new,buf,out
来源: https://www.cnblogs.com/MeatBallh/p/14350766.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有