1. 程式人生 > >java將物件寫入檔案

java將物件寫入檔案

以前只知道將物件寫入檔案用outPutStream,將檔案讀入到記憶體用inputStream,但是不知道為什麼,今天有人講解後終於知道了為什麼,所以寫到這裡和大家分享,希望大家以後也不會弄混。

outputstream:是將物件從記憶體寫入到外部,相當於輸出,所以用write()。

inputstream:是將外部內容讀入到記憶體,所以是read()。

寫的一個demo如下:

package test;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TestFile {
public static void main(String[] args) {
new TestFile().writeObject();
new TestFile().readObject();
}


public void writeObject() {
FileOutputStream out;
ObjectOutputStream objectOut = null;
try {
out = new FileOutputStream("D:/testFile.txt");
objectOut = new ObjectOutputStream(out);
} catch (Exception e) {
e.printStackTrace();
}
List<Map<String, Object>> customers = new ArrayList<>();
Map<String,Object> showMessage = new HashMap<>();
showMessage.put("customer", "fiona");
showMessage.put("waitTime", 14);
Map<String,Object> showMessage2 = new HashMap<>();
showMessage2.put("customer", "peter");
showMessage2.put("waitTime", 15);
customers.add(showMessage);
customers.add(showMessage2);
try {
objectOut.writeObject(customers);
} catch (IOException e) {
e.printStackTrace();
}
}


@SuppressWarnings("unchecked")
public void readObject() {
List<Map<String, Object>> customers = null;
FileInputStream in;
ObjectInputStream objectIn = null;
try {
in = new FileInputStream("D:/testFile.txt");
objectIn = new ObjectInputStream(in);
} catch (Exception e) {
e.printStackTrace();
}
try {
customers = (List<Map<String, Object>>) objectIn.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for(Map<String,Object> map:customers)
{
for(String key : map.keySet()){
System.out.println(key+":"+map.get(key));
}
}
}
}

列印結果:

customer:fiona
waitTime:14
customer:peter
waitTime:15

相關推薦

java物件寫入檔案

以前只知道將物件寫入檔案用outPutStream,將檔案讀入到記憶體用inputStream,但是不知道為什麼,今天有人講解後終於知道了為什麼,所以寫到這裡和大家分享,希望大家以後也不會弄混。 o

JAVA-內容寫入檔案並匯出到壓縮包

取出資料庫表中的內容寫入到檔案,並將所有檔案寫入到壓縮包最終匯出到指定的某目錄下        //匯出的壓縮包格式  xxxx_date        Dat

java字串寫入檔案操作

import  java.io.File;   import java.io.StringWriter; public void StringFile(final String Filename, final String strBuffer){ try{   // 建立檔案物

使用java資料寫入檔案,並下載到客戶端

最近做了一個功能,覺得挺有意思,決定記錄下來,以前也見過類似的功能,以為很高階,其實很簡單。 第一步:寫一個建立檔案的工具類 public class CSVUtils{ /** * CSV檔案生成方法 * @param head

java中將物件寫入檔案

/** * Object2FileUtils裡面的工具類 * * @author * */ public class Bean4Caches implements Serializable { public HashMap<String, Strin

Java 資料寫入磁碟並讀取磁碟上的檔案

package test; import java.io.BufferedReader;import java.io.FileReader;import java.io.FileWriter;import java.util.ArrayList;import java.util.List; public

Java資料寫入csv檔案使用FTP上傳

//1.先建立檔案     public static boolean createFile(String path,String fileName,String fileContent,String companyId) throws IOException{         Boolean boo=fal

java資料寫入到csv檔案

   對於csv檔案,因為要將資料分別的寫入到每一個單元格中。用什麼來分開當前單元格和下一個單元格之間呢?這裡就用到了“  , ”逗號可以分割單元格。 舉例說明: 我們這裡用一個輸出流將資料寫入到csv檔案中 FileOutputStream fos=new FileOu

字典寫入檔案的例子-python

字典內容寫入json時,需要用json.dumps將字典轉換為字串,然後再寫入。 json也支援格式,通過引數indent可以設定縮排,如果不設定的話,則儲存下來會是一行。 from collections import defaultdict import json video

字串寫入檔案與讀取檔案

 一,讀取檔案內容 File file = new File(filePath); if(file.isFile() && file.exists()) {

使用bat資料寫入檔案

一個簡單的類子 beginAA.bat call aa.bat hostDir user Password aa.bat @echo off set ftpIP=%1 set ftpUser=%2 set ftpPass=%3 set ftpFolder=/home/

filebeat+redis+logstash日誌寫入檔案

filebeat客戶端配置檔案-- filebeat.prospectors: - type: log   harvester_limit: 12   close_inactive: 35m   path

java資料寫入excel

package com.excel.test; import java.io.File; import java.io.IOException; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; impor

【Android實戰】json檔案讀取並資料寫入檔案

package com.json.ss; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStrea

java file物件獲取檔案大小

import java.io.File; import java.text.DecimalFormat; import java.io.FileInputStream; public class GetFileSize {     public longgetFileSiz

python讀取.txt檔案資料和資料寫入檔案.txt

一.讀取.txt檔案中的資料到張量中 說明:將data_x.txt和data_y.txt中的資料分別讀取到x_data張量中 #!/usr/bin/python # coding=utf-8 im

字串寫入檔案

String dataUrl = "aaaaaaasdasdasdsada"; File sf = new File("E:/team_project_store/BI/teamplate_plate/images/" + t1.getPath()); if(!sf.exists(

c語言資料寫入檔案(用連結串列實現)

/*c語言將資料寫入檔案,用連結串列實現*/ #include #include #include /*定義結構體*/ typedef struct Node { char id[10];

【Python】beautifusoup解析HTML並資料寫入檔案

Python版本:3.6 IDE:PyCharm 1.解析HTML(這裡以www.baidu.com為例) headers = { 'Connection': 'Keep-Alive', 'User-Agent': 'Mozilla/5.0 (Windo

shell 講字串當命令輸出寫入檔案,並處檔案中讀出來

#!/bin/sh -x str=cxx* key=keys #command=${key} ${str} #echo ${command} #將以cxx為字首的key 打印出來 並追加到檔案 keys_file redis-cli ${key} ${str} >