更新時(shí)間:2020-09-16 16:33:06 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽3039次
本文對(duì)JavaIO流的編程練習(xí)題做解答,里面有詳細(xì)的思路解析,做題時(shí)最好養(yǎng)成先寫思路在編程的習(xí)慣。
1.在程序中寫一個(gè)"HelloJavaWorld你好世界"輸出到操作系統(tǒng)文件Hello.txt文件中
package?com.xykj.lesson5;
import?java.io.File;
import?java.io.FileOutputStream;
public?class?Test5?{
/**
*?在程序中寫一個(gè)"HelloJavaWorld你好世界"輸出到操作系統(tǒng)文件Hello.txt文件中
*
*?程序分析:文件寫入,要用到輸出流FileOutputStream
*?*/
public?static?void?main(String[]?args)?{
//?向文件D:/Hello.txt,寫入內(nèi)容
File?file?=?new?File("D:/Hello.txt");
try?{
//?創(chuàng)建輸出流
FileOutputStream?fos?=?new?FileOutputStream(file);
//把String類型的字符串轉(zhuǎn)化為byte數(shù)組的數(shù)據(jù)保存在輸出流中
fos.write("HelloJavaWorld你好世界".getBytes());
fos.flush();//刷新輸出流
fos.close();//關(guān)閉輸出流
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
}
2.拷貝一張圖片,從一個(gè)目錄到另外一個(gè)目錄下(PS:是拷貝是不是移動(dòng))
package?com.xykj.lesson6;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileOutputStream;
public?class?Test6?{
/**
*?拷貝一張圖片,從一個(gè)目錄到另外一個(gè)目錄下(PS:是拷貝是不是移動(dòng))
*
*?程序設(shè)計(jì)思路:
*?這題不能使用renameTo,
*?解題步驟:
*?1、在目的地址創(chuàng)建一個(gè)圖片文件
*?2、讀取源地址文件的字節(jié)流
*?3、把讀取到的字節(jié)流寫入到目的地址的文件里面
*?4、刷新輸出流,并關(guān)閉就可以了
*
*?@throws?Exception
*?*/
public?static?void?main(String[]?args)?{
//?本題示范把D盤下的mm.jpg復(fù)制到D盤java文件夾里面
//?源文件地址
File?fileFrom?=?new?File("D:/mm.jpg");
//?目的文件地址
File?fileTo?=?new?File("D:/java/mm.jpg");
//?1、創(chuàng)建目的文件地址
try?{
if?(!fileTo.createNewFile())?{
System.out.println("創(chuàng)建文件失敗!");
}
//?2、讀取源地址文件的字節(jié)流
FileInputStream?fis?=?new?FileInputStream(fileFrom);
FileOutputStream?fos?=?new?FileOutputStream(fileTo);
int?len?=?0;
byte[]?buf?=?new?byte[1024];
while?((len?=?fis.read(buf))?!=?-1)?{
//?3、把讀取到的字節(jié)流寫入到目的地址的文件里面
fos.write(buf,?0,?len);
}
//?刷新下輸出流
fos.flush();
//?關(guān)閉輸入流和輸出流
fis.close();
fos.close();
System.out.println("文件復(fù)制成功!");
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
}
3.統(tǒng)計(jì)一個(gè)文件calcCharNum.txt(見(jiàn)附件)中字母'A'和'a'出現(xiàn)的總次數(shù)
package?com.xykj.lesson7;
import?java.io.File;
import?java.io.FileInputStream;
public?class?Test7?{
/**
*?統(tǒng)計(jì)一個(gè)文件calcCharNum.txt(見(jiàn)附件)中字母'A'和'a'出現(xiàn)的總次數(shù)
*
*?程序分析:
*?讀取文件用FileInputStream
*?一次只讀一個(gè)字節(jié)(一個(gè)字母就是一個(gè)字節(jié)),當(dāng)字節(jié)內(nèi)容和A或a相等時(shí),相應(yīng)的數(shù)量加1
*?*/
public?static?void?main(String[]?args)?{
try?{
//添加文件路徑
File?file?=?new?File("D:/java/calcCharNum.txt");
//創(chuàng)建文件讀取流
FileInputStream?fis?=?new?FileInputStream(file);
int?numA?=?0;//字母A的數(shù)量
int?numa?=?0;//字母a的數(shù)量
int?len?=?0;//每次讀取的字節(jié)數(shù)量
while?((len=fis.read())!=?-1)?{
//統(tǒng)計(jì)字母a的數(shù)量
if?(new?String((char)len+"").equals("a"))?{
numa++;
}
//統(tǒng)計(jì)字母A的數(shù)量
if?(new?String((char)len+"").equals("A"))?{
numA++;
}
}
//打印出文件內(nèi)字母的數(shù)量
System.out.println("a的數(shù)量是:"+numa);
System.out.println("A的數(shù)量是:"+numA);
System.out.println("a和A出現(xiàn)的總次數(shù):"+(numA+numa));
fis.close();//關(guān)閉輸入流
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
}
4.統(tǒng)計(jì)一個(gè)文件calcCharNum.txt(見(jiàn)附件)中各個(gè)字母出現(xiàn)次數(shù):A(8),B(16),C(10)...,a(12),b(10),c(3)....,括號(hào)內(nèi)代表字符出現(xiàn)次數(shù);
package?com.xykj.lesson8;
import?java.io.File;
import?java.io.FileInputStream;
import?java.util.HashMap;
import?java.util.Iterator;
import?java.util.Map.Entry;
public?class?Test8?{
/**
*?統(tǒng)計(jì)一個(gè)文件calcCharNum.txt(見(jiàn)附件)中各個(gè)字母出現(xiàn)次數(shù):
*?A(8),B(16),C(10)...,a(12),b(10),c(3)....,括號(hào)內(nèi)代表字符出現(xiàn)次數(shù);
*
*?程序分析:
*?1.這里沒(méi)中文字符,依然可以只用字節(jié)流來(lái)讀取文件
*?2.不能保存相同的主鍵值,可以使用HashMap:key-value來(lái)實(shí)現(xiàn)
*?3.先獲得該key的value,如果存在key的話value的值加1
*?*/
public?static?void?main(String[]?args)?{
//?文件路徑
File?file?=?new?File("D:/java/calcCharNum.txt");
try?{
//?創(chuàng)建讀取文件的輸入流
FileInputStream?fis?=?new?FileInputStream(file);
//?創(chuàng)建集合HashMap類存放要保存的key-value
HashMap?map?=?new?HashMap<>();
//?讀取文件
int?len?=?0;//?每次讀取的文件長(zhǎng)度
int?count?=?0;
while?((len?=?fis.read())?!=?-1)?{
//?每次獲取到的字母
char?c?=?(char)?len;
//這里使用try?catch是因?yàn)?map.get(c?+?""),第一次get不到東西會(huì)出現(xiàn)空指針
try?{
//?通過(guò)每次的key值獲取它的value值,
//?但是在它的key值沒(méi)有時(shí)或報(bào)空指針錯(cuò)誤,所以要try?catch處理
//?當(dāng)她有key值,就可以獲取到相應(yīng)的value值
count?=?map.get(c?+?"");
}?catch?(Exception?e)?{//?什么都不用輸出
}
//?如果有它的key值對(duì)應(yīng)的value值要加1
map.put(c?+?"",?count?+?1);
}
fis.close();
//?讀完后把結(jié)果打印出來(lái)
//迭代器的使用
Iterator>?iterator?=?map.entrySet().iterator();
while?(iterator.hasNext())?{
Entry?entry?=?iterator.next();
System.out.print(entry.getKey()?+?"("?+?entry.getValue()+?")?\t");
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
}
以上就是動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)的小編針對(duì)“Java io流編程練習(xí)題,經(jīng)典系列(一)”的內(nèi)容進(jìn)行的回答,希望對(duì)大家有所幫助,如有疑問(wèn),請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743