IO

1.使用File操做文件java

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包app

01.右鍵選中寫好的類 Export
02.輸入jar,以後選中 JAR file
03.輸入導出的位置以及文件名
ide

 

 

 

 

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寫入文件內容(字節流)ui

 

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讀取文件內容(字符流)this

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寫入文件(字符流)spa

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寫入文件(字符流)3d

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解決中文亂碼問題code

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 [id=" + id + ", age=" + age + ", name=" + name + "]";
    }
    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();
    }
}
反序列化

 

相關文章
相關標籤/搜索