问题序列化和反序列化的ArrayList

编程入门 行业动态 更新时间:2024-10-27 09:40:27
本文介绍了问题序列化和反序列化的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

每当我试图序列文件出现错误:FileNotFound。不知道为什么。这是我的FileHelper code:

包org.stocktwits.helper; 进口java.io.ByteArrayInputStream中; 进口java.io.ByteArrayOutputStream中; 进口的java.io.File; 进口java.io.FileInputStream中; 进口java.io.FileOutputStream中; 进口java.io.IOException异常; 进口的java.io.InputStream; 进口java.io.ObjectInputStream中; 进口java.io.ObjectOutput中; 进口java.io.ObjectOutputStream中; 进口的java.util.ArrayList; 进口org.stocktwits.model.Quote; 公共类FileHelper {     //返回一个字节数组的文件的内容。     公共静态的byte [] getBytesFromFile(档案文件)抛出IOException异常{         InputStream的是=新的FileInputStream(文件);         //获取文件的大小         长长度= file.length();         //您不能创建使用长类型的数组。         //它需要一个int类型。         //之前转换成int类型,检查         //确保文件不大于Integer.MAX_VALUE。         如果(长度>为Integer.MAX_VALUE){             //文件过大         }         //创建字节数组来保存数据         字节[]字节=新字节[(INT)长]。         //读取的字节数         INT偏移= 0;         INT numRead = 0;         而(偏移< bytes.length                &功放;&安培; (numRead = is.​​read(字节,胶印,bytes.length偏移))> = 0){             胶印+ = numRead;         }         //确保所有字节都被读入         如果(偏移< bytes.length){             抛出新的IOException异常(无法完全读取文件+ file.getName());         }         //关闭输入流,并返回字节         is.close();         返回字节;     }     公共静态无效serializeQuotes(ArrayList中<报价>引号){         尝试 {             //序列化到一个文件             的ObjectOutput OUT =新的ObjectOutputStream(新的FileOutputStream(quotes.ser));             out.writeObject(引号);             out.close();             //序列化到一个字节数组             ByteArrayOutputStream BOS =新ByteArrayOutputStream();             OUT =新的ObjectOutputStream(BOS);             out.writeObject(引号);             out.close();             //获取序列化对象的字节             // byte []的BUF = bos.toByteArray();         }赶上(IOException异常E){             的System.out.println(E);         }     }     @燮pressWarnings(未登记)     公共静态无效deserializeQuotes(ArrayList中<报价>引号){         尝试 {             //从文件反序列化             档案文件=新的文件(quotes.ser);             ObjectInputStream中的=新的ObjectInputStream(新的FileInputStream(文件));             //反序列化对象             报价=(ArrayList的<报价>)in.readObject();             附寄();             //获取一些字节数组数据             字节[]字节= FileHelper.getBytesFromFile(文件);             //看到读入文件到一个字节数组此方法的实现             //从一个字节数组反序列化             在=新的ObjectInputStream(新ByteArrayInputStream的(字节));             报价=(ArrayList的<报价>)in.readObject();             附寄();         }赶上(ClassNotFoundException异常E){             的System.out.println(E);         }赶上(IOException异常E){             的System.out.println(E);         }     } }

解决方案

我少code解决了这个问题:

私人无效serializeQuotes(){         FileOutputStream中FOS;         尝试 {             FOS = openFileOutput(Constants.FILENAME,Context.MODE_PRIVATE);             ObjectOutputStream的OOS =新的ObjectOutputStream(FOS);             oos.writeObject(引号);             oos.close();         }赶上(FileNotFoundException异常E){             e.printStackTrace();         }赶上(IOException异常E){             e.printStackTrace();         }     }     @燮pressWarnings(未登记)     私人无效deserializeQuotes(){         尝试{             的FileInputStream FIS = openFileInput(Constants.FILENAME);             ObjectInputStream的OIS =新的ObjectInputStream(FIS);             报价=(ArrayList的<报价>)ois.readObject();         }赶上(FileNotFoundException异常E){             e.printStackTrace();         }赶上(IOException异常E){             e.printStackTrace();         }赶上(ClassNotFoundException异常E){             e.printStackTrace();         }     }

BOOM SHAKA LAKA。

Anytime I try to serialize a file I get the error: FileNotFound. Not sure why. Here is my FileHelper code:

package org.stocktwits.helper; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.util.ArrayList; import org.stocktwits.model.Quote; public class FileHelper { // Returns the contents of the file in a byte array. public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); // You cannot create an array using a long type. // It needs to be an int type. // Before converting to an int type, check // to ensure that file is not larger than Integer.MAX_VALUE. if (length > Integer.MAX_VALUE) { // File is too large } // Create the byte array to hold the data byte[] bytes = new byte[(int)length]; // Read in the bytes int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException("Could not completely read file "+file.getName()); } // Close the input stream and return bytes is.close(); return bytes; } public static void serializeQuotes(ArrayList<Quote> quotes){ try { // Serialize to a file ObjectOutput out = new ObjectOutputStream(new FileOutputStream("quotes.ser")); out.writeObject(quotes); out.close(); // Serialize to a byte array ByteArrayOutputStream bos = new ByteArrayOutputStream() ; out = new ObjectOutputStream(bos) ; out.writeObject(quotes); out.close(); // Get the bytes of the serialized object //byte[] buf = bos.toByteArray(); } catch (IOException e) { System.out.println(e); } } @SuppressWarnings("unchecked") public static void deserializeQuotes(ArrayList<Quote> quotes){ try { // Deserialize from a file File file = new File("quotes.ser"); ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); // Deserialize the object quotes = (ArrayList<Quote>) in.readObject(); in.close(); // Get some byte array data byte[] bytes = FileHelper.getBytesFromFile(file); // see Reading a File into a Byte Array for the implementation of this method // Deserialize from a byte array in = new ObjectInputStream(new ByteArrayInputStream(bytes)); quotes = (ArrayList<Quote>) in.readObject(); in.close(); } catch (ClassNotFoundException e) { System.out.println(e); } catch (IOException e) { System.out.println(e); } } }

解决方案

I solved it with less code:

private void serializeQuotes(){ FileOutputStream fos; try { fos = openFileOutput(Constants.FILENAME, Context.MODE_PRIVATE); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(quotes); oos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } @SuppressWarnings("unchecked") private void deserializeQuotes(){ try{ FileInputStream fis = openFileInput(Constants.FILENAME); ObjectInputStream ois = new ObjectInputStream(fis); quotes = (ArrayList<Quote>) ois.readObject(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); }catch(ClassNotFoundException e){ e.printStackTrace(); } }

BOOM SHAKA LAKA.

更多推荐

问题序列化和反序列化的ArrayList

本文发布于:2023-10-22 23:54:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519111.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   ArrayList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!