Educoder -Java高級特性 - IO流


第1關:什么是IO流

  1、編程要求:

 

  2、答案:

    1、BCD              2、C

 

第2關:字節流-輸入輸出

  1、編程要求:

  • 讀取src/step2/input/目錄下的task.txt文件信息並輸出到控制台,使用Java代碼將字符串learning practice寫入到src/step2/output/目錄下的output.txt,若文件目錄不存在,則創建該目錄。

  注意:臨時字節數組需要定義長度為8位,否則會有空格。

  

  2、測試效果:

    補充完代碼后,點擊測評,平台會對你編寫的代碼進行測試,當你的結果與預期輸出一致時,即為通過。

 

  3、代碼展示:

  

package step2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//讀取src/step2/input/目錄下的task.txt文件信息並輸出到控制台,使用Java代碼將字符串learning practice寫入到src/step2/output/目錄下的output.txt,若文件目錄不存在,則創建該目錄。
//注意:臨時字節數組需要定義長度為8位,否則會有空格。
public class Task {
    
    public void task() throws IOException{
        /********* Begin *********/
        
        File file = new File("src/step2/input/task.txt");
        FileInputStream fs =new FileInputStream(file);
        byte[] b =new byte[8];
        fs.read(b);
        String str = new String(b,"UTF-8");
        System.out.println(str);


      
        
        File dir = new File("src/step2/output");
        if(!dir.exists()){
            dir.mkdir();
        }
        FileOutputStream out = new FileOutputStream("src/step2/output/output.txt"); //創建輸出流
        String str1 = "learning practice";
        byte[] c = str1.getBytes();   //將字符串轉換成字節
        out.write(c);                 //寫數據
        out.flush();                  //刷新緩存區數據
        fs.close();                      //釋放資源
        out.close();

        
}
}

 

第3關:字符流 - 輸入輸出

  1、編程要求:

  • src/step3/input/目錄下的input.txt文件復制到src/step3/output/目錄下;

  • 復制的新文件命名為output.txt

  • input.txt文件中只有8個字符。

 

  2、測試說明

   補充完代碼后,點擊測評,平台會對你編寫的代碼進行測試,當你的結果與預期輸出一致時,即為通過。

 

  3、代碼展示:

package step3;

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

public class Task {
    
    public void task() throws IOException{
        /********* Begin *********/
        String file1 = "src/step3/input/input.txt";
        FileReader fr = new FileReader(file1);
        char[] ch = new char[8];
        fr.read(ch);

        String file2 = "src/step3/output/output.txt";
        FileWriter fw = new FileWriter(file2);
        fw.write(ch);
        
        fr.close();
        fw.flush();
        fw.close();

        
        
        
        
        /********* End *********/        
    }
}

 

第4關:復制文件

  1、編程要求

  • 復制src/step4/input/目錄下的input.txt文件到src/step4/output/目錄下,新文件命名為output.txt

  • 復制src/step4/input/目錄下的input.jpg文件到src/step4/output/目錄下,新文件命名為output.jpg

 

  2、測試效果

   補充完代碼后,點擊測評,平台會對你編寫的代碼進行測試,當你的結果與預期輸出一致時,即為通過。

 

  3、代碼展示

package step4;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Task {
    
    public void task() throws IOException{
        /********* Begin *********/
            String file1 = "src/step4/input/input.txt";
        FileInputStream fr = new FileInputStream(file1);
        byte[] b = new byte[1024];
        int len = 0;

        String file2 = "src/step4/output/output.txt";
        FileOutputStream fw = new FileOutputStream(file2);
        while((len = fr.read(b))!=-1){
            fw.write(b,0,len);
        }
        fr.close();
        fw.close();

        String file3 = "src/step4/input/input.jpg";
        String file4 = "src/step4/output/output.jpg";
        FileInputStream fs = new FileInputStream(file3); //定義文件輸入流讀取文件信息
        FileOutputStream fos = new FileOutputStream(file4);//定義文件輸出流寫文件
        len = 0;        //每次讀取數據的長度
        byte[] bys = new byte[1024];    //數據緩沖區
        while( (len = fs.read(bys)) != -1){
            fos.write(bys, 0, len);
        }
        //釋放資源  刷新緩沖區
        fs.close();
        fos.close();


        
        
        /********* End *********/        
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM