IO

1.使用File操作文件

public class IoTest {

    public static void main(String[] args) throws IOException {
    /*     01.删除或者创建文件
     * File file=new File("e:/io.txt");
       addOrDel(file);   */
        
        File file=new File("e:/java/hello");
        //file.mkdir();   只能创建一层目录
        file.mkdirs();   //同时创建多层目录
    }
    
    /**
     * 删除或者创建文件
     */
    public static void addOrDel(File file) throws IOException {
        if (!file.exists()) { //判断文件是否存在
             if (file.createNewFile()) {//创建成功
                System.out.println("创建成功!");
                System.out.println("是否是文件:"+file.isFile());
                System.out.println("文件名称:"+file.getName());
                System.out.println("文件大小:"+file.length());
                System.out.println("文件的绝对路径:"+file.getAbsolutePath());
            }else {
                System.out.println("创建失败");
            }
        }else {
            System.out.println("文件已经存在!");
            if (file.delete()) {//删除文件
                System.out.println("删除成功!");
            }
        }
    }

}
File测试类

 2.把自己的写好的内容 导出成jar包

01.右键选中写好的类 Export
02.输入jar,之后选中 JAR file
03.输入导出的位置以及文件名

IO

3.使用FileInputStream读取文件内容(字节流)

package cn.bdqn.test;

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

/**
 * 
 * @author 小豆腐
 *                      以后的你,会感谢现在努力的自己!努力!坚持!不放弃!
 *                                                              
 *  所有的输入流都有   读    的方法
 *  所有的输出流都有   写    的方法
 *  
 *  输入输出流都是相对于计算机的内存而言
 *  
 *  字节输入流的基类是  InputStream
 *  字节输出流的基类是  OutputStream
 *  
 *  字符输入流的基类是  Reader
 *  字符输出流的基类是  Writer
 *  
 *  
 *  utf-8 :中文字符以及中文都是占3个字节!  数字和字母都是占1个!
 *    GBK:     中文字符以及中文都是占2个字节!                                                         
 */
public class FileInputStreamTest {

    public static void main(String[] args) {
        InputStream stream = null;
        try {
            stream = new FileInputStream("e:/hello.txt");
            System.out.println("可读取的字节数:" + stream.available());
            int num = 0;
            while ((num = stream.read()) != -1) {
                // 出现中文乱码 因为utf-8中中文占3个字节
                System.out.println((char) num);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

4.使用OutputStream写入文件内容(字节流)

package cn.bdqn.test;

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

/**
 * 
 * @author 小豆腐
 *                      以后的你,会感谢现在努力的自己!努力!坚持!不放弃!
 *     
 *  01.java项目的编码格式 要和输出文件的编码一致 都是UTF-8
 *  02.如果系统中没有指定的文件,会默认创建
 *  03.如果重复输出,则上次的内容会被覆盖 
 *     如果不想覆盖!使用重载!在第二个参数的位置输入true
 */
public class OutputStreamTest {

    public static void main(String[] args) {
        OutputStream stream = null;
        try {
            // stream = new FileOutputStream("e:/hello.txt");
            stream = new FileOutputStream("e:/hello.txt", true); // 在原本的内容上拼接
            // 这里是整体作为一个参数
            stream.write("中国1".getBytes());
            stream.write("中国2".getBytes());
            // 强行把缓冲区的数据写到输出流中
            stream.flush();
            stream.write("中国3".getBytes());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

 5.使用FileReader读取文件内容(字符流)

public class FileReaderTest03 {
    public static void main(String[] args) throws Exception {
        //获取当前的编码格式
        System.out.println("使用的编码为:"+System.getProperty("file.encoding"));
        //创建输入流 Reader
        Reader reader=new FileReader("e:/hello.txt");
        //因为读取的字符  创建数据的中转站  会有多余的空格产生
        char [] words=new char[1024];
        int  num;
        //需要字符串的 拼接
        StringBuilder sb=new StringBuilder();
        while((num=reader.read(words))!=-1){
            sb.append(words);
        }
        System.out.println(sb.toString());
        //关闭输入流
        reader.close();
    }
}
FileReader

 6.使用BufferedReader读取文件内容(字符流)

public class BufferedReaderTest04 {
    public static void main(String[] args) {
        /*
         * 创建输入流 Reader
         * BufferedReader和FileReader联合使用
         * 效率高   底层有默认的缓冲区 还可以逐行读取
         */
        
        
        //创建BufferedReader对象  传递Reader的对象
        BufferedReader br=null;
        Reader reader=null;
        try {
            reader = new FileReader("e:/hello.txt");
            br=new BufferedReader(reader);
            //一行一行的读取
            String  line=null;
            while((line=br.readLine())!=null){
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            //关闭流   先开的后关
            try {
                br.close();
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
BufferedReader

 7.使用OutputStreamWriter写入文件(字符流)

public class OutputStreamWriterTest05 {
    public static void main(String[] args) {
        try {
            /*
             * 输出流  默认的会给我们创建新的文件
             * 不使用第二个参数!  那么默认会覆盖之前的内容
             * ctrl +shift +t   选中需要查询的类或者接口
             */
            Writer writer=new FileWriter("e:/hello.txt",true);
            writer.write("我爱北京*!");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
OutputStreamWriter

8.使用BufferedWriter写入文件(字符流)

public class BufferedWriterTest06 {
    public static void main(String[] args) {
        try {
            //创建输出流对象
            Writer writer=new FileWriter("e:/hello.txt",true);
            //创建BufferedWriter对象
            BufferedWriter bw=new BufferedWriter(writer);
            //换行
            bw.newLine();
            bw.write("北京也爱你!");
            bw.newLine();
            bw.write("北京也爱你!");
            bw.close();
            writer.close();
            
            //获取输入流
            Reader reader=new  FileReader("e:/hello.txt");
            BufferedReader br=new BufferedReader(reader);
            String  line=null;
            while((line=br.readLine())!=null){
                System.out.println(line);
            }
            br.close();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        
    }
}
BufferedWriter

9.使用InputStreamReader解决中文乱码问题

public class InputStreamReaderTest07 {
    public static void main(String[] args) {
        
        BufferedReader br=null;
        InputStreamReader isr=null;
        InputStream stream=null;
        try {
            //创建输入流对象
            stream=new FileInputStream("e:/hello.txt");
            System.out.println("文件的大小:"+stream.available());
            //使用InputStreamReader来解决乱码
            isr=new InputStreamReader(stream, "utf-8");
            //读取
             br=new BufferedReader(isr);
            String  line=null;
            while((line=br.readLine())!=null){
                System.out.println(line);
            }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                br.close();
                isr.close();
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
InputStreamReader

10.读取2进制文件

public class DataInputStreamTest08 {
    public static void main(String[] args) throws Exception {
        //创建输入流  
       InputStream fis=new FileInputStream("e:/mm.mp3");
        //读取2进制文件
       DataInputStream dis=new DataInputStream(fis);
       
       //复制文件到另一个目录
       OutputStream fos=new FileOutputStream("e:/U1/慢慢.mp3");
       //以2进制的方式输出到指定的目录
       DataOutputStream dos=new DataOutputStream(fos);
        
       //开始读取
       int  data;
       while((data=dis.read())!=-1){
           //写入
           dos.write(data);
       }
       dos.close();
       fos.close();
       dis.close();
       fis.close();
    }
DataInputStream

 11.序列化和反序列化

/**
 * 序列化:将内存中对象的状态或者信息  转换成 持久化的过程!
 * 反序列化:把持久化的对象 变成 内存中的一个对象的过程!
 *    目的:
 *      01.使自定义的对象  持久化!对象本身是在内存中的!我们想把它持久化!
 *      02.把对象从一个地方传递到另一个地方!
 *      03.使程序具有维护性!
 *      
 *  怎么才能实现对象的序列化??
 *  1.让对象所属的类  实现Serializable 接口   之后, 这个类 就可以序列化了!
 *   Serializable:只是一个能否被序列化的标记!
 */
public class Student implements Serializable{  //实体类
    
    private  Integer  id;
    private  Integer  age;
    private  String  name;
    
    @Override
    public String toString() {
        return "Student [>;
    }
    public Student() {
        super();
    }
    public Student(Integer id, Integer age, String name) {
        super();
        this.id = id;
        this.age = age;
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    

}
用于序列化的Student类
public class SerializableTest {
    public static void main(String[] args) throws Exception { //序列化操作
        //首先实例化一个对象
        Student student=new Student(1, 500, "小白2");
        //想序列化?从内存中  放入 持久化的介质中  输出流
        FileOutputStream  fos=new FileOutputStream("e:/student.txt");
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        //开始持久化
        oos.writeObject(student);
        oos.close();
        fos.close();
    }
}
序列化
/**
 * 反序列化
 * 需要和 序列化时候的包名  一致   不然 没法反序列化
 */
public class StudentTest {
    public static void main(String[] args) throws Exception {
        // 从文件中把对象 拿到 内存中 输入流
        FileInputStream fis = new FileInputStream("e:/student.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        // 读取文件中的对象
        Student student = (Student) ois.readObject();
        System.out.println(student.getId());
        System.out.println(student.getAge());
        System.out.println(student.getName());
        ois.close();
        fis.close();
    }
}
反序列化

 IO

相关推荐