1. 程式人生 > 程式設計 >C語言實現簡單的控制檯三子棋遊戲

C語言實現簡單的控制檯三子棋遊戲

1.關於IO:

  • 用於處理裝置之間的資料傳輸,如讀寫檔案,網路通訊等
  • Java中,對於資料的輸入/輸出操作以“流/stream”的方式進行
  • jav.io包下提供各類“流”類和介面,用於獲取不同種類的資料,並通過標準方法輸入或者輸出資料

2.IO流的原理和分類:

  • 按照資料單位不同:位元組流、字元流
  • 按照資料流的流向不同:輸入流、輸出流
  • 按照流的角色不同:節點流、處理流:
  • 節點流:直接作用於檔案上的就是節點流,就是在兩個裝置之間傳輸的
  • 處理流:在節點流之上又包了一層,凡是在已有流上包的一層就是處理流,可以起到比如說:加快流的傳輸速度等作用

輸入流 ->

資料 ===================> 程式

<- 輸出流

3.IO流體系

(抽象基類)  位元組流  字元流

輸入流    InputStream Reader

輸出流    OutputStream Writer

下面所有的分類都是以上派生的!

4.流的體系結構

抽象基類    節點流(或流檔案)    緩衝流(處理流的一種)

InputStream   FileInputStream      BufferedInputStream

OutputStream FileOutputStream     BufferedOutputStream

Reader     FileReader         BufferedReader

Writer     FileWriter         BufferedWriter

一:節點流:

關於字元流的:

package day11;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileReaderWriterTest {

    public static void main(String[] args) {
        File file = new File("hello.txt
"); System.out.println(file.getAbsoluteFile()); } public void testFileReader() throws IOException { //1.例項化File類物件,指明要操作的檔案 File file = new File("hello.txt"); //相較於當前project下的 //2.提供具體的流 FileReader fr = new FileReader(file); //3.資料的讀入 //read():返回讀入的一個字元。如果達到檔案末尾,返回-1 int data = fr.read(); while (data != -1){ System.out.print((char)data); data = fr.read(); } //4.流的關閉 //必須要手動關閉,因為對於java的回收機制中,對於其他物理連線,比如資料庫連線、輸入流輸出流、Socket連線無能為力 fr.close(); } //不丟擲異常該怎麼寫? public void testFileReader1(){ FileReader fr = null; try { //1.例項化File類物件,指明要操作的檔案 File file = new File("hello.txt"); //相較於當前project下的 //2.提供具體的流 fr = new FileReader(file); //3.資料的讀入 //read():返回讀入的一個字元。如果達到檔案末尾,返回-1 int data; while ((data = fr.read()) != -1){ System.out.print((char)data); } } catch (IOException e) { e.printStackTrace(); } finally { //4.流的關閉 //必須要手動關閉,因為對於java的回收機制中,對於其他物理連線,比如資料庫連線、輸入流輸出流、Socket連線無能為力 try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } //對read()操作升級:使用read的過載方法 public void testFileReader2(){ FileReader fr = null; try { //1.File類的例項化 File file = new File("hello.txt"); //2.FileReader流的例項化 fr = new FileReader(file); //3.讀入的操作具體的細節 //read(char[] cbub):返回每次讀入cbuf陣列中的字元的個數 //如果達到檔案末尾,返回-1 char[] cbuf = new char[5]; //先建立為5個一波 int len; while ((len = fr.read(cbuf)) != -1){ //方式一: // 錯誤的寫法: // for (int i = 0; i < cbuf.length; i++) { // System.out.print(cbuf[i]); // } //正確的: for (int i = 0; i < len; i++) { System.out.print(cbuf[i]); } //方式二: //錯誤的寫法,對應於方式一的寫法錯誤 // String str = new String(cbuf); // System.out.println(str); //正確的寫法: //對應於方式二的寫法: String str = new String(cbuf,0,len); System.out.println(str); } } catch (IOException e) { e.printStackTrace(); } finally { if (fr != null) { //4.資源的關閉 try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } } //從記憶體中寫出資料到硬碟的檔案裡 public void testFileWriter(){ FileWriter fw = null; try { //1.提供File類的物件,指明寫出到的檔案 File file = new File("hello1.txt"); //2.提供FileWriter的物件,用於資料的寫出 fw = new FileWriter(file,true); ////寫出時如果沒有建立檔案,那麼就會自動建立檔案 //如果已經建立了這個檔案 //當後面append寫的是false時,就會使得原來檔案被覆蓋 //當後面的append寫的是true時,就是使得原來的檔案的內容往後接上 //如果後面append什麼也不加:那麼就是預設false,對原來的檔案覆蓋 //3.寫出的操作 fw.write("shit\n"); fw.write("fuck!"); } catch (IOException e) { e.printStackTrace(); } finally { //4.流資源的關閉 if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } //檔案的複製 public void testFileReaderFileWriter(){ FileReader fr = null; FileWriter fw = null; try { //1.建立File類的物件——2個——讀入和寫出的檔案 File srcFile = new File("hello.txt"); File destFile = new File("hello2.txt"); //2.建立流的物件——輸入流和輸出流 fr = new FileReader(srcFile); fw = new FileWriter(destFile); //3.資料的讀入和寫出操作 char[] cbuf = new char[5]; int len; //記錄每次讀入到cbuf陣列中的字元的個數 while ((len = fr.read(cbuf)) != -1){ //每次寫出len個字元 fw.write(cbuf,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fw != null) { //4.關閉流資源 fw.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (fr != null) { //4.關閉流資源 fr.close(); } } catch (IOException e) { e.printStackTrace(); } //萬一第一個try中出現異常,那麼第二個還是可以執行到 } } }

關於位元組流的:

package day11;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 測試FileInputStream和FileOutputStream的使用
 */
public class FileInputOutputStreamTest {

    public void testFileInputStream() throws IOException {
        FileInputStream fis = null;
        try {
            //1.造檔案
            File file = new File("hello.txt");
            //2.造流
            fis = new FileInputStream(file);
            //3.讀資料
            byte[] buffer = new byte[5];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                String str = new String(buffer,0,len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                //.關閉
                fis.close();
            }
        }
    }

    public void testFileInputOutputStream(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File file = new File("原圖.jpg");
            File destFile = new File("原圖.jpg");
            fis = new FileInputStream(file);
            fos = new FileOutputStream(destFile);
            byte[] buffer = new byte[5];
            int len;
            while ((len = fis.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    //指定路徑下檔案的複製
    public void copyFile(String srcPath,String destPath){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File file = new File(srcPath);
            File destFile = new File(destPath);
            fis = new FileInputStream(file);
            fos = new FileOutputStream(destFile);
            byte[] buffer = new byte[5];
            int len;
            while ((len = fis.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

結論:

1.對於文字檔案(.txt, .java, .c, .cpp),使用字元流處理

2.對於非文字檔案(.jpg, .mp3, .mp4, .avi, .doc, .ppt...),使用位元組流來處理

二.緩衝流

讀寫速度相比於上面的方法更加快

package day11;

import java.io.*;

//作用:提高流的讀取、寫入速度
public class BufferedTest {
    //實現非文字檔案的複製
    public void BufferedStreamTest(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1.造檔案
            File srcFile = new File("原圖.jpg");
            File destFile = new File("原圖2.jpg");
            //2.造流
            //2.1.造節點流
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            //2.2.造緩衝流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            //3.複製的細節:讀取,寫入
            byte[] buffer = new byte[10];
            int len;
            while ((len = bis.read(buffer)) != -1){
                bos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.資源關閉:
            //要求:
            //先關閉外層的流,再關閉內層的流
            //說明:關閉外層流的同時,內層流也會自動關閉,所以關閉內層流的動作可以忽略
            if (bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
//            if (fis != null){
//                fis.close();
//            }
//            if (fos != null){
//                fos.close();
//            }
        }
    }

}