• 字节流(笔记)


    字节流

    File 的构造方法

    File(File parent,String child)  抽象路径名+child路径名字创建File对象

    File(String pathname) 路径名字符串转化为抽象路径名创建一个新File实例

    File(String p, String c) 路径字符串和路径字符串创建File对象

    File 的方法

    getAbsolutePath()     获取绝对路径

    getName()    获取文件名

    getPath()   获取路径名字

    length()  返回抽象路径名表示的文件长度

    字节流

    OutputStream抽象类       子类FileInputStream  主要方法 read()

     InputStream 抽象类   子类FileOutputStream  主要方法write()

    public class FileOutputStreamDemo {
    	public static void main(String[] args) throws IOException {
    		//需求:将数据写入到文件中。
    		//创建存储数据的文件。
    		File file = new File("c:\file.txt");
    		//创建一个用于操作文件的字节输出流对象。一创建就必须明确数据存储目的地。
    		//输出流目的是文件,会自动创建。如果文件存在,则覆盖。
    		FileOutputStream fos = new FileOutputStream(file);
    		//调用父类中的write方法。
    		byte[] data = "abcde".getBytes();
    		fos.write(data);
    		//关闭流资源。
    		fos.close();
    	}
    }
    
    1 public class FileOutputStreamDemo2 {
    2     public static void main(String[] args) throws Exception {
    3         File file = new File("c:\file.txt");
    4         FileOutputStream fos = new FileOutputStream(file, true);[设置给指定文件续写数据]
    5         String str = "
    "[实现换行]+"itcast";
    6         fos.write(str.getBytes());
    7         fos.close();
    8     }
    9 }
    FileOutputStream(file, true)不覆盖原文件,而是往文件末尾继续添加
    public class FileInputStreamDemo {
        public static void main(String[] args) throws IOException {
            File file = new File("c:\file.txt");
            //创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。
            FileInputStream fis = new FileInputStream(file);
            //读取数据。使用 read();一次读一个字节。
            int ch = 0;
            while((ch=fis.read())!=-1){
                System.out.pr        }intln("ch="+(char)ch);
    
            // 关闭资源。
            fis.close();
        }
    }

    创建一个字节数组,一次性读入一个数组大小的内容

    public class FileInputStreamDemo2 {
    	public static void main(String[] args) throws IOException {
    		/*
    		 * 演示第二个读取方法, read(byte[]);
    		 */
    		File file = new File("c:\file.txt");
    		// 创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。
    		FileInputStream fis = new FileInputStream(file);		
    		//创建一个字节数组。
    		byte[] buf = new byte[1024];//长度可以定义成1024的整数倍。		
    		int len = 0;
    		while((len=fis.read(buf))!=-1){
    			System.out.println(new String(buf,0,len));
    		}
    		fis.close();
    	}
    }
    

     字节流的复制代码实现

    public class CopyFileTest {
        public static void main(String[] args) throws IOException {
            //1,明确源和目的。
            File srcFile = new File("c:\YesDir	est.JPG");
            File destFile = new File("copyTest.JPG");
            
            //2,明确字节流 输入流和源相关联,输出流和目的关联。
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);
            
            //3, 使用输入流的读取方法读取字节,并将字节写入到目的中。
            int ch = 0;
            while((ch=fis.read())!=-1){
                fos.write(ch);
            }
            //4,关闭资源。
            fos.close();
            fis.close();
        }
    }

    字节流数组方式实现复制文件

    public class CopyFileByBufferTest {
        public static void main(String[] args) throws IOException {
            File srcFile = new File("c:\YesDir	est.JPG");
            File destFile = new File("copyTest.JPG");
            // 明确字节流 输入流和源相关联,输出流和目的关联。
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);
            //定义一个缓冲区。
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = fis.read(buf)) != -1) {
                fos.write(buf, 0, len);// 将数组中的指定长度的数据写入到输出流中。
            }
            // 关闭资源。
            fos.close();
            fis.close();
        }
    }

    字节流读取字符文件

    package Io;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class IODemo {
        public static void main(String[] args) throws IOException {
    //        writeDemo();
            readDemo();
            
        }
    
        private static void readDemo() throws IOException {
            File file = new File("F://a//one.txt");
            try {
                FileInputStream fis = new FileInputStream(file);
                int len=0;
                byte [] b =new byte [1024];
                while((len=fis.read(b))!=-1) {
                    System.out.println(new String(b));
                }
                
                
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    
        private static void writeDemo() throws IOException {
            File file = new File("F://a//one.txt");
            try {
                FileOutputStream fos =    new FileOutputStream(file);
                
                fos.write("hellow,word".getBytes());
                
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            
        }
    }
  • 相关阅读:
    springboot整合Quartz框架
    安装 和 配置 HBase
    HBase 安装之后版本的验证的bug:(错误的替换、找不到或无法加载主类、SLF4J)
    HBase基本知识和应用场景
    修改idea的临时数据存放目录(默认保存在C盘用户目录下的.IntelliJIdea2020.3)
    Eclipse中格式化代码快捷键Ctrl+Shift+F失效的解决办法(关于快捷键失效原因可能是与输入法的快捷键冲突)
    参考大数据厦门大学林子雨编著的《大数据技术原理与应用(第3版)》中第三课《HDFS编程实践(Hadoop3.1.3)》遇到的bug
    框架设计思维符合语义即可使用,而不用关心底层的实现
    Ubuntu下无法输入中文问题解决
    HDFS编程实践(Hadoop3.1.3)
  • 原文地址:https://www.cnblogs.com/lyjblogs/p/7886024.html
Copyright © 2020-2023  润新知