第一篇:關(guān)于java文件流的簡單示例
package myjava;
import java.io.*;
public class InputStreamDemo1 {
//根據(jù)文件返回一個輸入流 private FileInputStream getFileInputStream(File file){FileInputStream fis=null;try{fis=new FileInputStream(file);}catch(IOException e){System.out.println(e.getMessage());}return fis;} //根據(jù)文件返回一個輸輸出流 private FileOutputStream getFileOutputStream(File file){FileOutputStream fos=null;try{fos=new FileOutputStream(file);}catch(IOException e){System.out.println(e.getMessage());}catch(Exception e1){System.out.println(e1.getMessage());}return fos;} public static void main(String args[]){File input=new File(“G:Java網(wǎng)絡編程.pdf”);File output=new File(“G:output.txt”);InputStreamDemo1 isd=new InputStreamDemo1();FileInputStream fis=isd.getFileInputStream(input);FileOutputStream fos=isd.getFileOutputStream(output);int b=0;
}} try {while((b=fis.read())!=-1){fos.write(b);} fis.close();fos.close();} catch(Exception e){System.out.println(e.getMessage());}
第二篇:java 文件與輸入輸出流
實驗2文件與輸入輸出流(1)
一、實驗目的? 能夠使用File類表示文件或目錄,獲取相關(guān)信息,并進行文件操作; ? 能夠利用InputStream和OutputStream的子類進行字節(jié)讀、寫操作,明白其優(yōu)點及不足;
? 能夠用FileInputStream和FileOutputStream進行文件讀寫的操作; ? 理解“逐層包裝”思想,能夠利用“數(shù)據(jù)流”(DataInputStream和DataOutputStream)包裝字節(jié)流,方便各類數(shù)據(jù)的讀寫;
? 能夠利用“緩沖字節(jié)流”(BufferedInputStream和BufferedOutputStream)包裝字節(jié)流,加快數(shù)據(jù)的讀寫速度;
? 熟知System.in和System.out是PrintStream的實例。
二、實驗步驟
在Eclipse環(huán)境中導入項目“code(lab_2)”,然后按要求完成各小題:
1.打開FileTest.java文件,請按下列要求進行操作:
(1)按要求填充程序所缺代碼;
(2)程序的運行需要用到一個命令行參數(shù),請分別用一個文件、目錄作參數(shù)來運行程序,看一看結(jié)果有什么不同。
(在Eclipse中設置命令行參數(shù)方法:Run/Open Run Dialog ?/(x)=Arguments設置)
2.在上一題的基礎上,修改程序,使之具備輸出指定目錄下所有子目錄中文件絕對路徑名、大小的功能,如下所示:
子目錄:C:jdk1.6.0sample
子目錄:C:jdk1.6.0samplewebservices
子目錄:C:jdk1.6.0samplewebservicesEbayServer
文件: C:jdk1.6.0samplewebservicesEbayServerbuild.properties,大小: 512 字節(jié) 文件: C:jdk1.6.0samplewebservicesEbayServerbuild.xml,大小: 3168 字節(jié)1
……
提示:參考課件 FileSpace.java內(nèi)容,通過一個以“路徑名”為參數(shù)的靜態(tài)方法來實現(xiàn):該方法先判斷“路徑名”是一個文件,還是一個目錄?如果是文件,則輸出其絕對路徑和大??;若為一個目錄,則先顯示目錄絕對路徑,再列出該目錄下的所有子目錄和文件,通過循環(huán)和遞歸方法來執(zhí)行后續(xù)處理。
3.文件FileOutputStreamTest.java的功能是:利用FileOutputStream類向myfile.txt文件寫入'0'~'9'和“用字節(jié)流寫入文件內(nèi)容”,請?zhí)畛涑绦蛩贝a,并運行程序。然后打開myfile.txt文件,查看其內(nèi)容是否與要求相符?
4.文件FileInputStreamTest1.java的功能是:利用FileInputStream類以“逐字節(jié)”方式讀取myfile.txt文件內(nèi)容,并輸出。請?zhí)畛涑绦蛩贝a,并運行程序。問題:為什么程序輸出的內(nèi)容為亂碼?
5.在FileInputStreamTest1.java的基礎上,編寫程序FileInputStreamTest2.java,利用FileInputStream類以“字節(jié)數(shù)組”方式讀取myfile.txt文件內(nèi)容,能正確輸出,解決亂碼問題。
思考題:亂碼問題是怎樣解決的?
6.若要將信息“Java開發(fā)典型模塊大全”(書名)、“明日科技”(作者)、79.5(價格)等信息以UTF、double類型保存到文件books.txt中,請用“數(shù)據(jù)流”類編程實現(xiàn)。
第三篇:Java 實驗 文件管理與IO流
作業(yè)要求:每個題保存為一個.java文件,保存在同一工程文件夾中,文件夾的名字為E:Java你的班級+姓名,例如:E:Java信息11張三。
注意:上交的程序包含程序的執(zhí)行結(jié)果,以注釋的形式附在程序后面。
實驗六文件管理與I/O流
一、實驗目的1.熟悉用文件File類創(chuàng)建、刪除、查看文件或目錄。
2.字節(jié)流、字符流、緩沖流、隨機流等流式文件的創(chuàng)建,讀寫操作。
3.用字符流和緩沖流從鍵盤接受字符串的方法。
二、實驗內(nèi)容
1.先運行該程序。源文件是sy6_1.java。然后按【思考問題】分析程序。
import java.io.*;
public class sy6_1{
public static void main(String[] args)throws Exception
{
int x=0;
File Mypath;
Mypath=new File(“E:aaaa”);
if(!Mypath.exists())
{System.out.println(“創(chuàng)建新目錄”);Mypath.mkdir();}
else System.out.println(“目錄已存在”);
File Myfile1=new File(Mypath,“myfile1.txt”);
File Myfile2=new File(Mypath,“myfile2.txt”);
File Myfile3=new File(Mypath,“myfile3.txt”);
FileInputStream Fin=new FileInputStream(Myfile1);
FileOutputStream Fout=new FileOutputStream(Myfile1);
DataOutputStream Dout=new DataOutputStream(new FileOutputStream(Myfile2));
DataInputStream Din=new DataInputStream(new FileInputStream(Myfile2));
PrintWriter PWout=new PrintWriter(new FileWriter(Myfile3));
RandomAccessFile RAread=new RandomAccessFile(Myfile3,“r”);
String str;
int num1;
BufferedReader buf;//緩沖流
buf=new BufferedReader(new InputStreamReader(System.in));
System.out.print(“請輸入一個小于255整型數(shù):”);
while(!(str=buf.readLine()).equalsIgnoreCase(“q”))
{ System.out.print(“請輸入另一個小于255整型數(shù),按Q結(jié)束:”);
num1=Integer.parseInt(str);
Fout.write(num1);
}Fout.close();
System.out.println(“你剛輸入的數(shù)據(jù)是:”);
while((x=Fin.read())!=-1)
{ System.out.println(x);
}Fin.close();
System.out.print(“請輸入int范圍內(nèi)整型數(shù):”);
while(!(str=buf.readLine()).equalsIgnoreCase(“q”))
{ System.out.print(“請輸入另一個整型數(shù),按Q結(jié)束:”);
num1=Integer.parseInt(str);
Dout.writeInt(num1);
}Dout.close();
int leng=Din.available()/4;
int xxx=0;
while(xxx { xxx++; x=Din.readInt(); System.out.println(x); } Din.close(); System.out.print(“請輸入第一個字符串:”); while((str=buf.readLine())!=null) { System.out.print(“請輸入另一個字符串,按Ctrl+Z結(jié)束:”); PWout.println(str);//寫入myfile3.txt中 } PWout.close(); RAread.seek(0); while(RAread.getFilePointer() {System.out.println(RAread.readLine());//從myfile3.txt中一行一行讀并輸出在控制臺上 } RAread.close(); System.out.println(“完成”); } } 【思考問題】 ① 本程序共用到哪幾種流式文件?都用于做什么? 答:基本輸入輸出流:System.in.輸入(從鍵盤) System.out.輸出(顯示器) 字節(jié)流類:FileOutputStream 文件輸出 FileInputStream 文件輸入 DataOutputStream數(shù)據(jù)輸出 DataInputStream數(shù)據(jù)輸入 字符流類:PrintWriter輸入 緩沖文件流:BufferedReader ② 運行完程序后,請用“我的電腦”找到創(chuàng)建的文件,并分別打開文件看其內(nèi)容,你 看到的是你輸入的數(shù)據(jù)嗎? 答:myfile1和myfile2中的數(shù)據(jù)非輸入數(shù)據(jù),myfile3中可以看見輸入的數(shù)據(jù)。③ 將創(chuàng)建輸入流對象Fin放在輸出流Fout前,看發(fā)生什么? ④ 對第二種流式文件判斷文件占用字節(jié)的長度用available()方法,而此處用int leng=Din.available()/4;為什么除以4? 2.按照第1題的內(nèi)容,修改程序要求每次重新運行不覆蓋原內(nèi)容,把所有其它流式文件全部改用隨機流式文件來實現(xiàn),新的數(shù)據(jù)填加在文件尾,然后讀出校驗。 import java.io.*; public class sy6_2 { public static void main(String[] args)throws Exception { File Mypath; Mypath = new File(“E:aaa”); if(!Mypath.exists()) {System.out.println(“創(chuàng)建新目錄”);Mypath.mkdir();} else System.out.println(“目錄已存在”); File Myfile1 = new File(Mypath, “myfile1.txt”); File Myfile2 = new File(Mypath, “myfile2.txt”); File Myfile3 = new File(Mypath, “myfile3.txt”); RandomAccessFile rf1 = new RandomAccessFile(Myfile1, “rw”); RandomAccessFile rf2 = new RandomAccessFile(Myfile2, “rw”); RandomAccessFile rf3 = new RandomAccessFile(Myfile3, “rw”); String str; int num1; BufferedReader buf;//緩沖流 buf = new BufferedReader(new InputStreamReader(System.in)); System.out.print(“請輸入一個小于255整型數(shù):”); rf1.seek(rf1.length());//指針移到文件尾進行寫操作 while(!(str=buf.readLine()).equalsIgnoreCase(“q”)) { System.out.print(“請輸入另一個小于255整型數(shù),按Q結(jié)束:”);num1=Integer.parseInt(str); rf1.write(num1);//將整型數(shù)作為ascii碼值所對應的字符寫入myfile1.txt中} rf1.seek(0);//指針移到文件頭進行讀操作 int x=0; while((x=rf1.read())!=-1) { System.out.println(x); } rf1.close(); System.out.print(“請輸入int范圍內(nèi)整型數(shù):”); rf2.seek(rf2.length()); while(!(str = buf.readLine()).equalsIgnoreCase(“q”)){ System.out.print(“請輸入另一個整型數(shù),按Q結(jié)束:”); num1 = Integer.parseInt(str); rf2.writeInt(num1); } int x1 = 0; for(int l = 0;l { rf2.seek(l*4); x1 = rf2.readInt(); System.out.println(x1); } rf2.close(); System.out.print(“請輸入第一個字符串:”); rf3.seek(rf3.length()); while((str = buf.readLine())!= null){ System.out.println(“請輸入另一個字符串,按Ctrl+Z結(jié)束:”);rf3.writeUTF(str);//寫入myfile3.txt中 } rf3.seek(0); while(rf3.getFilePointer()< rf3.length()){ System.out.println(rf3.readUTF());//從myfile3.txt中讀出字符串并輸出在控制臺上 } rf3.close(); System.out.println(“完成”); } } 三、實驗報告要求 1.回答第1題【思考問題】提出的問題。 2.寫出第二題要求的源程序。 使用java輸入輸出流實現(xiàn)文件的復制: public class Test { public static void copy(File source,File target){ InputStream in=null; OutputStream out=null; try { in=new BufferedInputStream(new FileInputStream(source));out=new BufferedOutputStream(new FileOutputStream(target));byte[] buff=new byte[1024]; while(in.read(buff)>0){ out.write(buff); } } catch(Exception e){ // TODO: handle exception e.printStackTrace(); }finally{ try { if(in!=null) in.close(); if(out!=null) out.close(); } catch(Exception e2){ // TODO: handle exception } } } public static void main(String[] args){ // TODO Auto-generated method stub } } 實驗7 流(2學時) 一、實驗目的1.熟悉流類庫中各種常用流的使用方法。 2.能夠使用流類實現(xiàn)基本的文件讀寫。 二、實驗內(nèi)容 1.編寫程序,在控制臺窗口提示輸入兩個整數(shù),然后接收這兩個整數(shù),并輸出它們的和。(要求:鍵盤輸入通過流封裝System.in獲取,不要使用Scanner類) import java.io.*;System.out.println(x);System.out.println(“y”);public class Num1 {n=in.readLine();public static void main(String []args)y=Integer.parseInt(n);{System.out.println(y);int x=0,y=0;}catch(IOException e)BufferedReader in=new BufferedReader({ newSystem.out.println(“error”);InputStreamReader(System.in));} String n;int s=x+y; try{System.out.println(s);System.out.println(“x”);} n=in.readLine();} x=Integer.parseInt(n); 2.設計學生類Student,屬性:編號(整型);姓名(字符串),成績(整型)。編寫一個程序:要求:(1)輸入3個學生的姓名和成績,將其姓名和成績保存到data.txt中;(2)然后從該文件中讀取數(shù)據(jù),求得這三個學生的平均成績。 import java.io.*;DataOutputStream dout = newDataOutputStream(public class num2 {newpublic static void main(String[]args)FileOutputStream(“D:data.txt”));{String n; BufferedReader in=new BufferedReader(for(int i=0;i<5;i++)new{ InputStreamReader(System.in));n=in.readLine(); int num=Integer.parseInt(n);try{String name=in.readLine(); n=in.readLine();int grade=Integer.parseInt(n);dout.writeBytes(num+“rn”);dout.writeBytes(name+“rn”);dout.writeBytes(grade+“rn”);}dout.close();}catch(IOException e1){int num=Integer.parseInt(n); n=din.readLine();String name=n;n=din.readLine();int grade=Integer.parseInt(n);ave+=grade;}System.out.println(“平均成績”+ave*1.0/5);System.out.println(“文件寫入失敗”);}try{DataInputStream din =new DataInputStream(new FileInputStream(“D:data.txt”));int ave=0;String n;for(int i=0;i<5;i++){n=din.readLine(); 三、實驗要求 完成程序設計并提交實驗報告。 在“);失敗”);} } }catch(FileNotFoundException e){System.out.println(“文件不存}catch(IOException e2){System.out.println(”文件讀取}第四篇:使用java輸入輸出流實現(xiàn)文件的復制
第五篇:java流實驗內(nèi)容及代碼