第一篇:Java 實(shí)驗(yàn) 文件管理與IO流
作業(yè)要求:每個題保存為一個.java文件,保存在同一工程文件夾中,文件夾的名字為E:Java你的班級+姓名,例如:E:Java信息11張三。
注意:上交的程序包含程序的執(zhí)行結(jié)果,以注釋的形式附在程序后面。
實(shí)驗(yàn)六文件管理與I/O流
一、實(shí)驗(yàn)?zāi)康?.熟悉用文件File類創(chuàng)建、刪除、查看文件或目錄。
2.字節(jié)流、字符流、緩沖流、隨機(jī)流等流式文件的創(chuàng)建,讀寫操作。
3.用字符流和緩沖流從鍵盤接受字符串的方法。
二、實(shí)驗(yàn)內(nèi)容
1.先運(yùn)行該程序。源文件是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 ② 運(yùn)行完程序后,請用“我的電腦”找到創(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)容,修改程序要求每次重新運(yùn)行不覆蓋原內(nèi)容,把所有其它流式文件全部改用隨機(jī)流式文件來實(shí)現(xiàn),新的數(shù)據(jù)填加在文件尾,然后讀出校驗(yàn)。 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());//指針移到文件尾進(jìn)行寫操作 while(!(str=buf.readLine()).equalsIgnoreCase(“q”)) { System.out.print(“請輸入另一個小于255整型數(shù),按Q結(jié)束:”);num1=Integer.parseInt(str); rf1.write(num1);//將整型數(shù)作為ascii碼值所對應(yīng)的字符寫入myfile1.txt中} rf1.seek(0);//指針移到文件頭進(jìn)行讀操作 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(“完成”); } } 三、實(shí)驗(yàn)報(bào)告要求 1.回答第1題【思考問題】提出的問題。 2.寫出第二題要求的源程序。 實(shí)驗(yàn)12:Java高級I/O流程序設(shè)計(jì) 實(shí)驗(yàn)時間:實(shí)驗(yàn)地點(diǎn): 一、實(shí)驗(yàn)?zāi)康募耙?/p> (1)掌握文件類File的使用。 (2)理解隨機(jī)存取文件類RandomAccessFile的使用。 二、實(shí)驗(yàn)設(shè)備環(huán)境及要求 三、實(shí)驗(yàn)任務(wù) (1)按要求編寫Java Application程序,并編譯、運(yùn)行這個程序。 四、實(shí)驗(yàn)內(nèi)容與步驟 1.輸出當(dāng)前目錄下my.txt文件的基本信息。 import java.io.*; import java.util.*; public class FileTest{ public static void main(String args[]){File f=new File(“my.txt”);System.out.println(“Absolute path: ” + f.getAbsolutePath()+“n Can read: ” + f.canRead()+“n Can write: ” + f.canWrite()+“n getName: ” + f.getName()+“n getParent: ” + f.getParent()+“n getPath: ” + f.getPath()+“n length: ” + f.length()+“n lastModified: ” + new Date(f.lastModified()));if(f.isFile())System.out.println(“It's a file”); }}else if(f.isDirectory())System.out.println(“It's a directory”); 2.編寫一個Java Application程序,實(shí)現(xiàn)如下的設(shè)計(jì)功能:運(yùn)行該程序可以列出當(dāng)前目錄下的文件。 import java.io.*; class FileDir{ public static void main(String args[]){ File f=new File(“D:”); File fs[]=f.listFiles(); for(int i=0;i if(fs[i].isFile()) System.out.println(fs[i].getName()); else System.out.println(“ } } } 五、實(shí)驗(yàn)指導(dǎo)與處理 六、分析討論 實(shí)驗(yàn)教師評語成績 簽名: 日期: Java實(shí)驗(yàn)五IO流的一般使用 實(shí)驗(yàn)?zāi)康模赫莆瘴募惖氖褂茫私庖话懔鞯幕緫?yīng)用。加深處理代碼的能力。實(shí)驗(yàn)內(nèi)容: import java.io.*; public class lijun43 { public static void main(String args[]) { File f = new File(“f:mldn.txt”); if(f.exists()) { System.out.println(“文件已存在?!?; } else { System.out.println(“文件不存在”); } } }; import java.io.*; public class lijun45 { public static void main(String args[]) { loop(“d:”); } public static void loop(String dir) { File f = new File(dir); String str[] = null; if(f.isDirectory()) { str = f.list(); for(int i=0;i { loop(dir+“”+str[i]); } } else { System.out.println(dir); } } }; 實(shí)驗(yàn)結(jié)果: 1f:mldn.txt在f盤中出現(xiàn)了。 心得體會: 需要多嘗試,流主要是對文件俠的操作,比如文件俠創(chuàng)建,移動,刪除等,打開一個流, 實(shí)驗(yàn)2文件與輸入輸出流(1) 一、實(shí)驗(yàn)?zāi)康? 能夠使用File類表示文件或目錄,獲取相關(guān)信息,并進(jìn)行文件操作; ? 能夠利用InputStream和OutputStream的子類進(jìn)行字節(jié)讀、寫操作,明白其優(yōu)點(diǎn)及不足; ? 能夠用FileInputStream和FileOutputStream進(jìn)行文件讀寫的操作; ? 理解“逐層包裝”思想,能夠利用“數(shù)據(jù)流”(DataInputStream和DataOutputStream)包裝字節(jié)流,方便各類數(shù)據(jù)的讀寫; ? 能夠利用“緩沖字節(jié)流”(BufferedInputStream和BufferedOutputStream)包裝字節(jié)流,加快數(shù)據(jù)的讀寫速度; ? 熟知System.in和System.out是PrintStream的實(shí)例。 二、實(shí)驗(yàn)步驟 在Eclipse環(huán)境中導(dǎo)入項(xiàng)目“code(lab_2)”,然后按要求完成各小題: 1.打開FileTest.java文件,請按下列要求進(jìn)行操作: (1)按要求填充程序所缺代碼; (2)程序的運(yùn)行需要用到一個命令行參數(shù),請分別用一個文件、目錄作參數(shù)來運(yùn)行程序,看一看結(jié)果有什么不同。 (在Eclipse中設(shè)置命令行參數(shù)方法:Run/Open Run Dialog ?/(x)=Arguments設(shè)置) 2.在上一題的基礎(chǔ)上,修改程序,使之具備輸出指定目錄下所有子目錄中文件絕對路徑名、大小的功能,如下所示: 子目錄: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)方法來實(shí)現(xiàn):該方法先判斷“路徑名”是一個文件,還是一個目錄?如果是文件,則輸出其絕對路徑和大小;若為一個目錄,則先顯示目錄絕對路徑,再列出該目錄下的所有子目錄和文件,通過循環(huán)和遞歸方法來執(zhí)行后續(xù)處理。 3.文件FileOutputStreamTest.java的功能是:利用FileOutputStream類向myfile.txt文件寫入'0'~'9'和“用字節(jié)流寫入文件內(nèi)容”,請?zhí)畛涑绦蛩贝a,并運(yùn)行程序。然后打開myfile.txt文件,查看其內(nèi)容是否與要求相符? 4.文件FileInputStreamTest1.java的功能是:利用FileInputStream類以“逐字節(jié)”方式讀取myfile.txt文件內(nèi)容,并輸出。請?zhí)畛涑绦蛩贝a,并運(yùn)行程序。問題:為什么程序輸出的內(nèi)容為亂碼? 5.在FileInputStreamTest1.java的基礎(chǔ)上,編寫程序FileInputStreamTest2.java,利用FileInputStream類以“字節(jié)數(shù)組”方式讀取myfile.txt文件內(nèi)容,能正確輸出,解決亂碼問題。 思考題:亂碼問題是怎樣解決的? 6.若要將信息“Java開發(fā)典型模塊大全”(書名)、“明日科技”(作者)、79.5(價格)等信息以UTF、double類型保存到文件books.txt中,請用“數(shù)據(jù)流”類編程實(shí)現(xiàn)。 package Stream; import java.io.*; public class Io { public void test1()throws Exception{ File file=new File(“E:/txt.txt”); if(file.exists()){ System.out.println(“是否是文件:”+file.isFile()); System.out.println(“文件名是:”+file.getName()); System.out.println(“路徑是:”+file.getPath()); System.out.println(“絕對路徑是:”+file.getAbsolutePath());System.out.println(“上級目錄是:”+file.getParent());System.out.println(“文件大小是:”+file.length()+“字節(jié)”);} else { file.createNewFile(); } } public void test2()throws Exception{//以字節(jié)流方式讀取 File file=new File(“E:/txt1.txt”); FileInputStream fi=new FileInputStream(file); byte[] content= new byte[fi.available()]; /*for(int i=0;i content[i]=(byte)fi.read(); }//讀取長度后,將其轉(zhuǎn)換成字符型 *///第一種方式 fi.read(content);//第二種方式 String str=new String(content); System.out.println(str.trim()); } public void test3()throws Exception{//以字節(jié)流方式寫入數(shù)據(jù) File file=new File(“E:/txt1.txt”); FileOutputStream fo=new FileOutputStream(file); byte[]content=new String(“你是一個”).getBytes(); fo.write(content); content=new String(“呵呵”).getBytes(); fo.write(content); fo.close(); } public void test4()throws Exception{//用的緩沖方式文本讀取 FileReader file=new FileReader(“E:/txt1.txt”); BufferedReader br=new BufferedReader(file); StringBuffer str=new StringBuffer(); String sw=br.readLine(); if(sw!=null){ str.append(sw+“ n”); } System.out.println(str); } public void test5()throws Exception{//用緩沖的方式寫入數(shù)據(jù)然后再讀入數(shù)據(jù) FileWriter file=new FileWriter(“E:/txt1.txt”); BufferedWriter bw=new BufferedWriter(file); for(int i=1;i<=10;i++){ bw.write(“這是第”+i+“行”); bw.newLine(); } bw.close();//寫放數(shù)據(jù) FileReader file1=new FileReader(“E:/txt1.txt”); BufferedReader br=new BufferedReader(file1); StringBuffer str=new StringBuffer(); String sw=br.readLine(); if(sw!=null){ str.append(sw+“ n”); } System.out.println(str); } public void test6(){//刪除文件 File file=new File(“E:/text.txt”); if(file.exists()){ System.out.println(“開始刪除文件 :”+file.getPath());if(file.delete()){ System.out.println(“文件刪除成功”); } else { System.out.println(“文件刪除失敗”); } } else { System.out.println(“該文件不存在”); } } public void test7()throws Exception{//創(chuàng)建 一個文件夾---創(chuàng)建一個目錄或路徑 File file=new File(“E:/txt1.txt”); if(file.exists()==false){ file.createNewFile(); } } public void test8()throws Exception {//* 將txt.txt復(fù)制文件到txt12.txt去;以字節(jié)流的的形式復(fù)制 File file=new File(“E:/txt.txt”); FileInputStream fi=new FileInputStream(file); byte[] content= new byte[fi.available()]; fi.read(content); fi.read(content, 0, content.length); String str=new String(content); System.out.println(str); File file1=new File(“E:/txt12.txt”); if(file.exists()==false){ file.createNewFile(); }//如果不存在該文件則創(chuàng)建一件文件 再行進(jìn)復(fù)制 FileOutputStream fo=new FileOutputStream(file1,true);fo.write(content); fo.flush(); fo.close(); fi.close();//關(guān)閉流 } public void test9()throws Exception {//另和種方式復(fù)制文件--從緩沖的形式復(fù)制 FileReader file=new FileReader(“E:/txt.txt”); BufferedReader br=new BufferedReader(file); String str=br.readLine(); //從文件 里面讀取出來 FileWriter file1=new FileWriter(“E:/txt1.txt”); BufferedWriter bw=new BufferedWriter(file1); while(str!=null){ bw.write(str); bw.newLine(); str = br.readLine(); } bw.flush(); bw.close(); br.close(); } public void test10()throws Exception{ File file=new File(“E:/txt.txt”); InputStream is = new FileInputStream(file); byte[] array = new byte[3]; int hasRead = 0; File file1=new File(“E:/txt1.txt”); OutputStream os = new FileOutputStream(file1); while((hasRead=is.read(array, 0, array.length))!=-1) { System.out.println(“讀取了”+hasRead+“個字節(jié)”); for(int i=0;i { System.out.println(“第”+(i+1)+“個:”+array[i]); } /**每次從數(shù)組array里寫入字節(jié)到文件 讀多少寫多少*/ os.write(array, 0, hasRead); } os.flush(); os.close(); is.close(); } public static void main(String args[])throws Exception{ Io t=new Io(); t.test9(); } }第二篇:實(shí)驗(yàn)12:Java高級IO流程序設(shè)計(jì)
第三篇:Java實(shí)驗(yàn)五 IO流的一般使用
第四篇:java 文件與輸入輸出流
第五篇:JAVA(IO流方法)