国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

簡單的輸入輸出流(2)

2019-11-11 03:55:38
字體:
來源:轉載
供稿:網友

從一個文本文件中讀取數據

public class FileInputStreamDemo1 { /** * 使用InputStream抽象類的子類FileInputStream類 * 從一個文件中讀取流數據 */ public static void test1() { try { FileInputStream myStream = new FileInputStream("src/a.txt"); int a = 0; while((a=myStream.read())!=-1){ //從a.txt讀取內容一直到文件的末尾 System.out.PRint((char)a); } myStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { test1(); }}

簡單的輸出流 輸入一個字符到文本中

public class Demo1 { /** * 輸入一個字符到文件中 */ public static void test1() { try { OutputStream os = new FileOutputStream("src/b.txt"); os.write(97); os.flush(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }運行結果為a,因為ascii碼97對應的字符為‘a’;輸出一個數組/** * 輸出一個數組 */ public static void test2() { byte myArray[] = new byte[5]; myArray[0]=97; myArray[1]=98; myArray[2]=99; myArray[3]=100; myArray[4]=101; try { OutputStream os = new FileOutputStream("src/b.txt"); os.write(myArray); os.flush(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

輸出結果為abcde 輸出字符串

public static void test3() { String myContent = "I am a good man!"; try { OutputStream os = new FileOutputStream("src/b.txt"); os.write(myContent.getBytes()); os.flush(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

運行結果為:I am a good man! 。

將一個文件的內容輸出到另一個文件中

public static void copy(){ try { FileInputStream myStream = new FileInputStream("src/1.png"); OutputStream os = new FileOutputStream("2.jpg"); int a = 0; while((a = myStream.read())!=-1){ //一直讀到文件的末尾 os.write((char)a); } os.flush(); os.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
上一篇:Linux會話淺析

下一篇:文章標題

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洛扎县| 宾阳县| 东平县| 宜城市| 九龙县| 福泉市| 荔浦县| 成安县| 双牌县| 德格县| 阳东县| 迁西县| 抚顺市| 南昌县| 锡林郭勒盟| 芒康县| 伊通| 大石桥市| 木兰县| 泰兴市| 岢岚县| 涟源市| 澄城县| 绥棱县| 肇庆市| 麻阳| 平江县| 达孜县| 奇台县| 陈巴尔虎旗| 巨鹿县| 阿克苏市| 南丰县| 民勤县| 西宁市| 绥江县| 三门县| 泸州市| 仙桃市| 泉州市| 古蔺县|