Java——IO流

來源:互聯網
上載者:User

標籤:處理   簡介   檔案中   轉換   string   com   關閉   file   返回   

一、io簡介

位元組:byte,電腦中儲存資料的單元,一個位元組有8位,是一個很具體的儲存空間

字元:人們使用的記號,抽象意義上的符號,如  ‘1’   ‘中’   ‘a‘   ‘$‘

字元集和編碼:

  • 有哪些字元
  • 規定每個字元分別用一個還是多個位元組儲存、用哪些位元組儲存

io就是輸入與輸出,即“讀”和“寫”

根據資料走向:輸入資料流、輸出資料流;根據處理的資料類型:位元組流、字元流

  

 

 

二、位元組輸入資料流
import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class InputStreamTest {public static void main(String[] args) {File file = new File("text.txt");try {// 1、建立一個名為text的檔案file.createNewFile();FileInputStream fis = new FileInputStream("text.txt");                //從輸入資料流中讀取資料的下一個位元組,返回0-255間的int值,讀到末尾返回-1// fis.read();// 將輸入資料流中的資料讀取到位元組數組中byte[] input = new byte[1];fis.read(input);String str = new String(input);System.out.println(str);
              fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

  

String 和 byte []間的轉換

  • byte [] strbyte = str.getBytes();
  • String st = new String(byte);
三、位元組輸出資料流
 1 import java.io.FileOutputStream; 2 import java.io.IOException; 3  4 public class OutputStreamTest { 5  6     public static void main(String[] args) { 7         try { 8             // 開啟輸入資料流 9             FileOutputStream fos = new FileOutputStream("textw.txt");10 11             // 要寫入的資料12             String str = "寫入資料:1234";13             byte[] out = str.getBytes();14 15             // 把資料寫入到指定檔案中16             fos.write(out);17 18             // 關閉流19             fos.close();20 21         } catch (IOException e) {22             // TODO Auto-generated catch block23             e.printStackTrace();24         }25     }26 27 }
View Code 四、通過位元組流實現檔案拷貝
 1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5  6 public class CopyStream { 7  8     public static void main(String[] args) { 9         try {10             // 建立輸入輸出資料流,從text.txt檔案中讀取資料,把讀取到的資料存到textnew.txt中11             FileInputStream fis = new FileInputStream("text.txt");12             FileOutputStream fos = new FileOutputStream("textnew.txt");13 14             // 儲存資料的數組15             byte[] shuju = new byte[50];16 17             while (fis.read(shuju) != -1) {18                 fis.read(shuju);19             }20 21             // 將位元組數組中的資料寫入到textnew.txt22             fos.write(shuju);23 24             fis.close();25             fos.close();26         } catch (FileNotFoundException e) {27             // TODO Auto-generated catch block28             e.printStackTrace();29         } catch (IOException e) {30             // TODO Auto-generated catch block31             e.printStackTrace();32         }33 34     }35 36 }
View Code

 

Java——IO流

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.