0基础入行Java开发--JAVA 基础IO流
小职 2021-08-10 来源 : 阅读 459 评论 0

摘要:本文主要是介绍了0基础入行Java开发--JAVA 基础IO流,通过具体的内容向大家展现,希望对大家Java开发的学习有所帮助。

本文主要是介绍了0基础入行Java开发--JAVA 基础IO流,通过具体的内容向大家展现,希望对大家Java开发的学习有所帮助。

0基础入行Java开发--JAVA 基础IO流

IO流:概括为两个对应,一个桥梁。


1.按照功能来划分:

输入流:只能从中读取数据,而不能向其写入数据。

输出流:只能向其写入数据,而不能从中读取数据。

2.按照处理单元来划分

字节流和字符流操作的方式基本上完全相同。操作的数据单元不同

字节流:操作的是8位的字节 InputStream/OutputStream 作为字节流的基类

字符流:操作的是16位的字符 Reader/Writer 作为字符流的基类


Fileinputstream FileOutputStream不带缓冲区的字节流


 public static void main(String[] args) {

        try {

            FileInputStream fis = new FileInputStream("D:\\img.jpg");

            FileOutputStream fos = new FileOutputStream("D:\\img2.jpg");

            byte[] b = new byte[1024];

            int a = 0;

            while ((a=fis.read(b))!=-1){

                fos.write(b,0,a);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

 

    }


BufferedInputStream  bufferedOutputStream  带缓冲区的字节流


 public static void main(String[] args) {

 

        try {

            FileInputStream fis = new FileInputStream("d:\\img2.jpg");

            FileOutputStream fos = new FileOutputStream("d:\\zxs.jpg");

            BufferedInputStream bis=new BufferedInputStream(fis);

            BufferedOutputStream bos=new BufferedOutputStream(fos);

            byte[] b =new byte[1024];

            int a = 0;

            while ((a=fis.read(b))!=-1){

                bos.write(b,0,a);

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

 

    }


FileReader    FileWriter   不带缓冲区的字符流


BufferedReader BufferedWriter  带缓冲区的字节流


public static void main(String[] args) {

        BufferedReader br=null;

        BufferedWriter bw=null;

        InputStream in;

        br=new BufferedReader(new InputStreamReader(System.in));

         try {

//将控制台输入的内容存在d:\\ceshi.txt

            bw=new BufferedWriter(new FileWriter(new File("d:\\ceshi.txt"),true));

            String temp="";

            System.out.println("请在控制台输入");

            while ((temp=br.readLine())!=null){

                if (temp.equals("quit")){

                    break;

                }

                bw.write(temp);

                bw.newLine();

            }

            bw.flush();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }



我是小职,记得找我

✅ 解锁高薪工作

✅ 免费获取基础课程·答疑解惑·职业测评

0基础入行Java开发--JAVA 基础IO流

本文由 @小职 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程