猿问
下载APP

redis 如何 把文件的内容 存进去

redis 如何 把文件的内容 存进去


Cats萌萌
浏览 115回答 1
1回答

不负相思意

写了个保存文件的方法。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");}}
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答