亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

redis 如何 把文件的內容 存進去

redis 如何 把文件的內容 存進去

慕的地6264312 2019-03-01 07:02:01
redis 如何 把文件的內容 存進去
查看完整描述

2 回答

?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

寫了個保存文件的方法。public class TestRedis{
Jedis redis = new Jedis("localhost");
//序列化方法
public byte[] object2Bytes(Object value) {
if (value == null)
return null;
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream outputStream;
try {
outputStream = new ObjectOutputStream(arrayOutputStream);
outputStream.writeObject(value);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
arrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return arrayOutputStream.toByteArray();
}
//反序列化方法
public Object byte2Object(byte[] bytes) {
if (bytes == null || bytes.length == 0)
return null;
try {
ObjectInputStream inputStream;
inputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
Object obj = inputStream.readObject();
return obj;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
//保存文件方法
public void setFile(String key,String path){

File fr = new File(path);
redis.set(key.getBytes(), object2Bytes(fr));
}
//讀取文件對象方法
public File getFile(String key){
Jedis redis = new Jedis("localhost");
File file = (File)byte2Object(redis.get(key.getBytes()));
return file;
}

public void testFile(String key,String path)throws Exception{
setFile("test", "D:\\test.txt");
File file = getFile("test");
BufferedReader br = new BufferedReader(new FileReader(file));
String record = null;
while ((record = br.readLine()) != null) {
System.out.println("record:"+record);
}

}

public static void main(String[] args) throws Exception{
TestRedisos = new TestRedis();
os.testFile("test", "D:\\test.txt");
}
}



查看完整回答
反對 回復 2019-03-08
  • 2 回答
  • 0 關注
  • 4154 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號