java代碼流,java io流代碼

java輸入輸出流與文件,求源代碼!感謝大佬!

你好,java的API中提供了用于對(duì)象輸入輸出文件的操作,實(shí)例代碼如下:

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),安鄉(xiāng)企業(yè)網(wǎng)站建設(shè),安鄉(xiāng)品牌網(wǎng)站建設(shè),網(wǎng)站定制,安鄉(xiāng)網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,安鄉(xiāng)網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

定義單詞類如下(注意:你定義的類要實(shí)現(xiàn)Serializable接口)

public class Words implements Serializable {

private int size;

private String[] words;

public Words(){};

public Words(String...strs){

this.words = strs;

this.size = strs.length;

}

@Override

public String toString() {

return "Words{" +

"size=" + size +

", words=" + Arrays.toString(words) +

'}';

}

}

2. 對(duì)象輸入輸出api測(cè)試類

public class ObjIOTest {

public static void main(String[] args) {

String path = "d:/myIOTest.txt";

ObjIOTest objIOTest = new ObjIOTest();

Words words = new Words("hello", "my", "dear", "friend");

try {

objIOTest.writeObject(path,words);

Words wordsFromFile = (Words)objIOTest.readObject(path);

System.out.println(wordsFromFile.toString());

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

}

}

//java serialize a object to file

public void writeObject(String path,Object map) throws IOException{

File f=new File(path);

FileOutputStream out=new FileOutputStream(f);

ObjectOutputStream objwrite=new ObjectOutputStream(out);

objwrite.writeObject(map);

objwrite.flush();

objwrite.close();

}

// read the object from the file

public Object readObject(String path) throws IOException, ClassNotFoundException{

FileInputStream in=new FileInputStream(path);

ObjectInputStream objread=new ObjectInputStream(in);

Object map=objread.readObject();

objread.close();

return map;

}

}

把兩段代碼拷貝到一個(gè)包下即可運(yùn)行了,希望您的問(wèn)題得到解答

用java編寫(xiě)的有輸入輸出流源代碼

/**

* 讀寫(xiě)指定文件或者讀寫(xiě)指定某一個(gè)文件夾下的全部文件(不包括文件夾)

* @throws Exception

*/

public static void moveFile() throws Exception {

Scanner scan = new Scanner(System.in);

System.out.println("請(qǐng)輸入源路徑:");//輸入源文件地址,可以是文件夾,可以是具體某個(gè)文件

String uDisk = scan.nextLine();

File file = new File(uDisk);//獲得讀取文件

if ((file.exists())) {//當(dāng)文件存在

System.out.println("請(qǐng)輸入目標(biāo)路徑:");//文件復(fù)制目標(biāo)路徑

String targetFolder = scan.nextLine();

File target = new File(targetFolder);//獲得寫(xiě)入文件

if (!target.exists()) {

if (!target.mkdir()) {

throw new Exception("創(chuàng)建目標(biāo)目錄失敗");

}

} else if (!target.isDirectory()) {

throw new Exception("與目標(biāo)目錄同名的文件已經(jīng)存在");

}

File[] temp = null;

if(file.listFiles()==null || file.listFiles().length=0){

temp=new File[]{file};//輸入的源路徑指定文件,將文件添加到文件數(shù)組中

}else{

temp = file.listFiles();//如果輸入的源路徑是文件夾,則獲取文件夾下文件的個(gè)數(shù)

}

if ((temp != null) (temp.length 0)) {//文件數(shù)組temp有值

int i = 0;

for (int length = temp.length; i length; i++) {//循環(huán)文件數(shù)組

if (!temp[i].isDirectory()) {

String fileName = temp[i].getName();

File t = new File(targetFolder + File.separator

+ fileName);//創(chuàng)建輸出文件

if (!t.createNewFile()) {

throw new Exception("創(chuàng)建輸出文件失敗");

}

FileOutputStream out = new FileOutputStream(t);//創(chuàng)建文件輸出流

FileInputStream in = new FileInputStream(temp[i]);//創(chuàng)建文件輸入流

byte[] buffer = new byte[256];

while (in.read(buffer) 0) {//循環(huán)輸入流,直到輸入流無(wú)數(shù)據(jù)

out.write(buffer);//寫(xiě)入文件

}

}

}

}

}

}

調(diào)用例子:

public static void main(String[] args) throws Exception {

moveFile();

}

java以下代碼中,我用文件輸出流和內(nèi)存輸出流同樣輸出output對(duì)象,為什么結(jié)果不一樣?

呃,時(shí)隔幾年,不知道你解決問(wèn)題沒(méi)有。首先應(yīng)該明白,System.out.println(OBJ)會(huì)自動(dòng)調(diào)用OBJ.toString()方法。查閱官方文檔表示,ByteArrayInputStream類中重寫(xiě)了toString( )方法,Converts the buffer's contents into a string decoding bytes using the platform's default character set.即 “使用平臺(tái)的默認(rèn)字符集將緩沖區(qū)的內(nèi)容轉(zhuǎn)換為字符串解碼字節(jié)。

但是 FileOutputStream類中并未重寫(xiě)toString( ) 。使用的是從Object類中繼承下來(lái)的。所以你能看到 obj@xxx 格式的輸出。

當(dāng)前文章:java代碼流,java io流代碼
網(wǎng)頁(yè)路徑:http://www.muchs.cn/article6/hssgog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站全網(wǎng)營(yíng)銷推廣、網(wǎng)站改版、服務(wù)器托管、域名注冊(cè)、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管