Com.Java.Basis 第十四课 《File类 +IO流》简称字节的Ctrl+A Ctrl+C Ctrl+V(二)

简介: Com.Java.Basis 第十四课 《File类 +IO流》简称字节的Ctrl+A Ctrl+C Ctrl+V(二)

案例九:

package com.JavaBasicsDemo9;
import java.io.FileInputStream;
import java.io.IOException;
public class Demo1Test9 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis=new   FileInputStream("E:\\demo\\haha.txt");
        //调用字节数人流对象数据方法;return
        // int read(); 1
        int num =fis.read();
        System.out.println(num);
        //2
        int num1 =fis.read();
        System.out.println(num1);
        //3
        int num2 =fis.read();
        System.out.println(num2);
        //4
        int num3 =fis.read();
        System.out.println(num3);
    }
}

案例十:

package com.JavaBasicsDemo9;
import java.io.FileInputStream;
import java.io.IOException;
public class Demo1Test10 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis=new   FileInputStream("E:\\demo\\haha.txt");
        //调用字节数人流对象数据方法;return
        int by;
        while((by=fis.read())!=-1) {
            System.out.print((char)by);
            //释放资源
            //fis.close();
        }
    }
}

案例十一:

package com.JavaBasicsDemo9;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo1Test11 {
    public static void main(String[] args) throws IOException {
        // FileOutputStream(String name)
        FileOutputStream fos =new  FileOutputStream("E:\\demo\\herrllo.text" ,true);
        //  void write (int b) 写入数据
        fos.write(121);
        fos.write(11);
        fos.write(111);
        fos.write(101);
        fos.write(111);
        fos.close();
        // 2数组  void write(byte[] by) 将byte数组写入数组中
        byte[] by = {34,56,78};
        fos.write(by);
        //写一句话
        String str = "我爱你";
        byte [] by1=str.getBytes();
        fos.write(by1);
        fos.close();
        //
        byte[] by2 = {34,56,78};
        fos.write(by2,2,1);
    }
}

案例十二:

package com.JavaBasicsDemo9;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo1Test12 {
    public static void main(String[] args)  {
        FileOutputStream fos=null;
        try {
            //创建
            fos =new  FileOutputStream("E:\\demo\\haha.txt" ,true);
            for (int i = 0; i <=10; i++) {
                fos.write("I love you in the end ".getBytes());
                fos.write("\r\n".getBytes());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //释放资源
            try {
                if(fos!=null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

案例十三:

package com.JavaBasicsDemo9;
import java.io.*;
public class BufferStreamDemo1 {
    /**
     *第一种方式
     FileInputStream fis= new   FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\1.jpg");
     FileOutputStream fos =new  FileOutputStream("D:\\haha.jpg" ,true);
     byte[] by =new byte[1024];
     //int read(byte[]) by)
     int num=0;
     while((num=fis.read(by))!=-1) {
     //reading
     fos.write(by, 0, num);
     }
     //关闭流
     fis.close();
     fos.close();
     //基本字节读一个 字节/数组/字节/一个字节数组
     * @param args
     */
    public static void main(String[] args)throws IOException {
        //获取时间
        long startTime1 =System.currentTimeMillis();
        //调用方法
        method1();
        long endTime1 =System.currentTimeMillis();
        System.out.println("方法1:"+(endTime1-startTime1));
        long startTime2 =System.currentTimeMillis();
        //调用方法
        method1();
        long endTime2 =System.currentTimeMillis();
        System.out.println("方法2:"+(endTime2-startTime2));
        long startTime3 =System.currentTimeMillis();
        //调用方法
        method1();
        long endTime3 =System.currentTimeMillis();
        System.out.println("方法3:"+(endTime3-startTime3));
        long startTime4 =System.currentTimeMillis();
        //调用方法
        method1();
        long endTime4 =System.currentTimeMillis();
        System.out.println("方法4:"+(endTime4-startTime4));
    }
    public static void method1() throws IOException {
        FileInputStream fis= new   FileInputStream("C:\\Users\\MZFAITHDREAM\\Pictures\\Saved Pictures\\A-1 (4)\\黑白上色\\01.jpg");
        FileOutputStream fos =new  FileOutputStream("D:\\01.jpg" ,true);
        byte[] by =new byte[1024];
        //int read(byte[]) by)
        int num=0;
        while((num=fis.read(by))!=-1) {
            //reading
            fos.write(by, 0, num);
        }
        //关闭流
        fis.close();
        fos.close();
    }
    public static void method2() throws IOException {
        FileInputStream fis= new   FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\1.jpg");
        FileOutputStream fos =new  FileOutputStream("D:\\method2.jpg" ,true);
        byte[] by =new byte[1024];
        //int read(byte[]) by)
        int num=0;
        while((num=fis.read(by))!=-1) {
            //reading
            fos.write(by, 0, num);
        }
        //关闭流
        fis.close();
        fos.close();
    }
    public static void method3() throws IOException {
        FileInputStream fis= new   FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\1.jpg");
        //创建字节输入缓冲流
        BufferedInputStream bis =new  BufferedInputStream(fis);
        FileOutputStream fos =new  FileOutputStream("D:\\method3.jpg" ,true);
        BufferedOutputStream bos =new  BufferedOutputStream(fos);
        //int read(byte[]) by)
        int num=0;
        while((num=fis.read())!=-1) {
            //reading
            fos.write( num);
        }
        //关闭流
        fis.close();
        fos.close();
    }
    public static void method4() throws IOException {
        FileInputStream fis= new   FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\1.jpg");
        //创建字节输入缓冲流
        BufferedInputStream bis =new  BufferedInputStream(fis);
        FileOutputStream fos =new  FileOutputStream("D:\\method4.jpg" ,true);
        BufferedOutputStream bos =new  BufferedOutputStream(fos);
        byte[] by=new byte [1024];
        //int read(byte[]) by)
        int num=0;
        while((num=fis.read())!=-1) {
            //reading
            fos.write( by,0,num);
        }
        //关闭流
        fis.close();
        fos.close();
    }
}

案例十四:

package com.JavaBasicsDemo9;
public class CodeDemo1 {
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        String str="我爱距离100km江西南字符昌学lookforyou江西区的学院";
        //将字符串转成编码
        byte [] by =str.getBytes("utf-8");
        //将数据给用户看
        String str1 =new String(by,"utf-8");
        System.out.println(str1);
        //网页定义utf-8
        String str2="我爱距离100km江西南区的学院";
        //将字符串转成编码
        byte [] by1 =str1.getBytes("gbk");
        //将数据给用户看
        String str3 =new String(by,"gbk");
        System.out.println(str2);
        System.out.println(str.getBytes());
        System.out.println(str);
        System.out.println(str2);
    }
}

案例十五:

package com.JavaBasicsDemo9;
import java.io.*;
public class CopeFile {
    public static void main(String[] args) throws IOException {
        //创建字节输入
        FileInputStream fis= new   FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\Javaday1_9.zip");
        //创建字节输出
        FileOutputStream fos =new  FileOutputStream("D:\\Day");
        //字符输入
        InputStreamReader isr=new    InputStreamReader(fis,"utf-8");
        //字符输出
        OutputStreamWriter osr=new   OutputStreamWriter(fos,"utf-8");
        //创建一个数组去放入取到的数据
        char[] cr=new char[1024];
        int num=0;
        //while
        while((num=isr.read(cr))!=-1) {
            //reading
            osr.write( cr,0,num);
        }
        isr.close();
        osr.close();
    }
}

案例十六:

package com.JavaBasicsDemo9;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy1 {
    public static void main(String[] args)  throws IOException {
        // TODO Auto-generated method stub
        FileInputStream fis= new   FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\1.jpg");
        FileOutputStream fos =new FileOutputStream("D:\\胡滨.jpg" ,true);
        byte[] by =new byte[1024];
        //int read(byte[]) by)
        int num=0;
        while((num=fis.read(by))!=-1) {
            //reading
            fos.write(by, 0, num);
        }
        //关闭流
        fis.close();
        fos.close();
    }
}

十六个小案例带你走进java的IO流

                   

相关文章
|
9天前
|
Oracle NoSQL 关系型数据库
实时计算 Flink版操作报错之报错:java.lang.ClassNotFoundException: io.debezium.connector.common.RelationalBaseSourceConnector,如何解决
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
|
17天前
|
Java Unix Windows
|
1天前
|
存储 Java API
Java语言IO(输入/输出)编程技术深度解析
Java语言IO(输入/输出)编程技术深度解析
|
2天前
|
存储 监控 Java
Java nio非阻塞io
Java nio非阻塞io
|
4天前
|
Java API
文件IO (File对象, 文件读写)
文件IO (File对象, 文件读写)
13 2
|
5天前
|
存储 Java API
Java IO流
Java IO流
22 2
|
8天前
|
存储 Java
Java IO流:深入解析与技术应用
Java IO流:深入解析与技术应用
256 1
|
9天前
|
存储 Java 编译器
Java文件IO操作基础
Java文件IO操作基础
7 0
|
10天前
|
存储 Java API
【JAVA学习之路 | 进阶篇】IO流及流的分类
【JAVA学习之路 | 进阶篇】IO流及流的分类
|
17天前
|
监控 Java
Java一分钟之-NIO:非阻塞IO操作
【5月更文挑战第14天】Java的NIO(New IO)解决了传统BIO在高并发下的低效问题,通过非阻塞方式提高性能。NIO涉及复杂的选择器和缓冲区管理,易出现线程、内存和中断处理的误区。要避免这些问题,可以使用如Netty的NIO库,谨慎设计并发策略,并建立标准异常处理。示例展示了简单NIO服务器,接收连接并发送欢迎消息。理解NIO工作原理和最佳实践,有助于构建高效网络应用。
19 2