




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、Java IO操作的實例代碼2008-11-18 23:00215人閱讀評論(0)收藏舉報Java IO操作的實例代碼(部分來自網(wǎng)絡(luò)源碼)/* 程序輸出的時候會有亂碼,原因是因為read()方法讀取一個字節(jié),中文字符* 是由兩個字節(jié)構(gòu)成,取一個字節(jié)(字符的一半)就轉(zhuǎn)成一個字符,所以輸出就會是亂碼* author DaHai*/public class TestFileInputStreampublic static void main(String args)int b = 0;FileInputStream in = null;tryin = new FileInputStream(&quo
2、t;c:/TestFileInputStream.java");catch(FileNotFoundException e)System.out.println("找不到指定的文件");/終止當(dāng)前正在運行的 Java 虛擬機。參數(shù)用作狀態(tài)碼;根據(jù)慣例,非零的狀態(tài)碼表示異常終止。 System.exit(-1); trylong num = 0;/只有讀到文件的結(jié)尾才為-1while(b=in.read()!=-1)System.out.print(char)b);num+; in.close();System.out.println();System.out.pr
3、intln("共讀取了"+ num +"個字節(jié)");catch(IOException e)System.out.println("文件讀取錯誤");System.exit(-1);Java代碼 復(fù)制文件import java.io.*; public class TestFileOutputStream public static void main(String args) int b = 0; FileInputStream in = null; FileOutputStream out = null; try in = new
4、 FileInputStream("c:/TestFileInputStream.java"); /如果這個時候C盤下面還沒有Test.java,那么new FileOutputStream會創(chuàng)建一個空的Test.java文件,但不會創(chuàng)建目錄 out = new FileOutputStream("c:/Test.java"); while(b = in.read()!=-1) out.write(b); in.close(); out.close(); catch(FileNotFoundException e) System.out.println(
5、"找不到指定文件"); System.exit(-1); catch(IOException e) System.out.println("文件復(fù)制錯誤"); System.exit(-1); System.out.println("文件已復(fù)制"); 從文件里讀取字符數(shù)據(jù) Java代碼 import java.io.*; public class TestFileReader public static void main(String args) FileReader fr = null; int c = 0; try fr = n
6、ew FileReader("c:/TestFileReader.java"); while(c = fr.read()!=-1) System.out.print(char)c); catch(FileNotFoundException e) System.out.println("系統(tǒng)找不到指定的文件"); catch(IOException e) System.out.println("文件讀取錯誤"); 向文件里寫字符數(shù)據(jù) 下面這個小程序可以寫出全世界所有的字符Java代碼 import java.io.*; public c
7、lass TestFileWriter public static void main(String args) FileWriter fw = null; int b = 0; try fw = new FileWriter("c:/Unicode.dat"); for(b=0;b<65536;b+) fw.write(b);fw.flush();fw.close(); catch(FinleNotFoundException e) e.printStackTrace();System.out.println(e.getMessage();System.exit(0
8、); catch(IOException e) e.printStackTrace();System.out.println(e.getMessage();System.exit(0); 帶緩沖區(qū)功能的輸入流 Java代碼 import java.io.*; public class TestBufferStream public static void main(String args) try /一個字節(jié)的輸入流并且不帶緩沖區(qū) FileInputStream fis = new FileInputStream("c:/TestFileInputStream.java")
9、; /相當(dāng)于在上面的fis流上又套了一層管道,實際上讀取的是一樣的數(shù)據(jù),但是它有了帶緩沖區(qū)的功能 BufferedInputStream bis = new BufferedInputStream(fis); int c = 0; System.out.println(bis.read(); System.out.println(bis.read(); /mark一個標(biāo)記,從第100個開始往外讀 bis.mark(100); for(int i=0;i<10 && (c=bis.read()!=-1;i+) System.out.print(char)c+" &
10、quot;); System.out.println(); /重置到上面的mark處 bis.reset(); for(int i=0;i<=10 && (c=bis.read()!=-1;i+) System.out.print(char)c+" "); bis.close(); catch(IOException e) e.printStackTrace(); Java代碼 修飾流的使用import java.io.*; public class TestBufferStream public static void main(String arg
11、s) try /在FileWriter寫出一個字符外面套層BufferedWriter管道 BufferedWriter bw = new BufferedWriter(new FileWriter("c:/write.txt"); BufferedReader br = new BufferedReader(new FileReader("c:/write.txt"); String s = null; for (int i = 0; i <= 100; i+) s = String.valueOf(Math.random(); bw.write
12、(s); bw.newLine(); bw.flush(); / readLine()方法為讀一行數(shù)據(jù) while (s = br.readLine() != null) System.out.println(s); bw.close(); br.close(); catch (IOException e) e.printStackTrace(); System.out.println("讀取文件出錯"); Java代碼 import java.io.*; public class TestTransForm2 public static void main(String
13、args) /等待從鍵盤的輸入,System.in可以理解為一根管道直接插在了鍵盤上 /System.in字節(jié)流輸入一個字節(jié),外面套層InputStreamReader轉(zhuǎn)換流 InputStreamReader isr = new InputStreamReader(System.in); /isr轉(zhuǎn)換流的外面又套層緩沖流,為了使用下面的readLine()方法,可以記取一行 BufferedReader br = new BufferedReader(isr); String s = null; try s = br.readLine(); while(s!=null) if(s.equal
14、sIgnoreCase("exit") break; System.out.println(s.toUpperCase(); s = br.readLine(); br.close(); catch(IOException e) e.printStackTrace(); Java代碼 import java.io.*; public class TestDataStream public static void main(String args) /在內(nèi)存中定義了一個空間,同時一根管道插在了上面 ByteArrayOutputStream baos = new ByteAr
15、rayOutputStream(); /DataOutputStream直接寫基礎(chǔ)數(shù)據(jù)類型(boolean,double等),而且可以寫UTF-8(省空間,網(wǎng)絡(luò)上常用) DataOutputStream dos = new DataOutputStream(baos); try dos.writeDouble(Math.random(); dos.writeBoolean(true); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray(); System.out.println(bais.availabl
16、e(); DataInputStream dis = new DataInputStream(bais); /輸出按照先入先出的形式輸出 System.out.println(dis.readDouble(); System.out.println(dis.readBoolean(); dos.close(); dis.close(); catch (IOException e) e.printStackTrace(); Java代碼 import java.io.*; public class TestPrintStream1 public static void main(String a
17、rgs) PrintStream ps = null; try FileOutputStream fos = new FileOutputStream("d:/bak/log.dat"); ps = new PrintStream(fos); catch (IOException e) e.printStackTrace(); if(ps != null) System.setOut(ps); int ln = 0; for(char c = 0; c <= 60000; c+) System.out.print(c+ " "); if(ln+ &
18、gt; =100) System.out.println(); ln = 0; Java代碼 import java.io.*; public class TestPrintStream2 public static void main(String args) String filename = args0; if(filename!=null)list(filename,System.out); public static void list(String f,PrintStream fs) try BufferedReader br = new BufferedReader(new Fi
19、leReader(f); String s = null; while(s=br.readLine()!=null) fs.println(s); br.close(); catch (IOException e) fs.println("無法讀取文件"); Java代碼 import java.util.*; import java.io.*; public class TestPrintStream3 public static void main(String args) String s = null; BufferedReader br = new Buffere
20、dReader(new InputStreamReader(System.in); try FileWriter fw = new FileWriter("d:/bak/logfile.log", true); PrintWriter log = new PrintWriter(fw); while (s = br.readLine()!=null) if(s.equalsIgnoreCase("exit") break; System.out.println(s.toUpperCase(); log.println("-"); lo
21、g.println(s.toUpperCase(); log.flush(); log.println("="+new Date()+"="); log.flush(); log.close(); catch (IOException e) e.printStackTrace(); Java代碼 import java.io.*; public class TestObjectIO public static void main(String args) throws Exception T t = new T(); t.k = 8; FileOutputStream fos = new FileOutputStream("d:/share/java/io/test
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權(quán)】 IEC 60086-4:2025 CMV EN Primary batteries - Part 4: Safety of lithium batteries
- 菊花種植收購事宜合同
- 基于大數(shù)據(jù)驅(qū)動的企業(yè)轉(zhuǎn)型升級合作協(xié)議
- 企業(yè)廣告牌制作合同
- 塔吊租賃協(xié)議樣本
- 環(huán)境監(jiān)測與評估合同
- 防雷裝置檢測技術(shù)服務(wù)合同
- 場地轉(zhuǎn)讓合同協(xié)議書
- 房地產(chǎn)項目合作協(xié)議
- 自動化生產(chǎn)線改造項目合作合同
- 光纜線路施工安全協(xié)議書范本
- 《我國國有企業(yè)股權(quán)融資效率實證研究》相關(guān)概念及國內(nèi)外文獻(xiàn)綜述2600字
- 2025年湖南交通職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性測試近5年??及鎱⒖碱}庫含答案解析
- 成本合約規(guī)劃培訓(xùn)
- 交通法規(guī)教育課件
- 小學(xué)校長任期五年工作目標(biāo)(2024年-2029年)
- 2022-2024年浙江中考英語試題匯編:閱讀理解(說明文)教師版
- 小學(xué)生思政課課件
- 2016屆高三備考建議
- 北師大版六年級下冊數(shù)學(xué)全冊表格式教案
- 2021年阿里巴巴全球數(shù)學(xué)競賽預(yù)選賽試題及參考答案
評論
0/150
提交評論