Java DataInputStream字符串超出范围:0

时间:2017-04-27 12:45:41

标签: java binaryfiles datainputstream

我正在开展一个大学项目,我必须将TVMaze API中的名人数据提取到Java中。

我使用' DIS'将搜索结果存储到二进制文件中。而且由于我是对象,我在读/写时填充和删除文件。

当我尝试将文件加载回HashMap时,我收到错误。

  

线程中的异常" main" java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0

错误指向Load方法和Depad方法。

昨天它对我有用,但由于一个未知的原因它停止了工作。

保存代码:

    private static void save(HashMap<String, ArrayList<Person>> persons) throws IOException
{
    try
    {
        ArrayList<Person> p = convertFileData(persons);
        File f = new File("person.dat");
        if(!f.exists())
        {
            f.createNewFile();
        }

        DataOutputStream dos = new DataOutputStream(new FileOutputStream(f));
        Iterator<Person> iter = p.iterator();
        while(iter.hasNext())
        {
            Person s = iter.next();
            dos.writeDouble(s.getScore());
            dos.writeBytes(pad(s.getQueryName(), 30));
            dos.writeBytes(pad(s.getName(), 30));
            dos.writeInt(s.getId());
            List<String> images = s.getImageUrls();
            for(String image : images)
            {
                dos.writeBytes(pad(image, 80));
            }
            dos.writeBytes(pad(s.getPersonUrl(), 50));
            dos.writeDouble(s.getMyRating());
            ArrayList<String> comments = s.getMyComments();
            int commentSize = comments.size();
            System.out.println(commentSize);
            dos.writeInt(commentSize);
            for(String comment : comments)
            {
                dos.writeBytes(pad(comment, 40));
            }
        }
        dos.close();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
}

加载代码:

    public static HashMap<String, ArrayList<Person>> load() throws FileNotFoundException, IOException
{
   File f = new File("person.dat");
   HashMap<String, ArrayList<Person>> persons = new HashMap<>();
   if(f.exists())
   {
       DataInputStream dis = new DataInputStream(new FileInputStream(f));
       ArrayList<Person> x = new ArrayList<>();
       while(dis.available() > 0)
       {
           double score = dis.readDouble();
           System.out.println(score);

           byte[] queryNameBytes = new byte[30];
           dis.read(queryNameBytes);
           String queryName = depad(new String(queryNameBytes));
           System.out.println(queryName);

           byte[] nameBytes = new byte[30];
           dis.read(nameBytes);
           String name = depad(new String(nameBytes));
           System.out.println(name);

           int id = dis.readInt();
           System.out.println(id);

           ArrayList<String> imageUrls = new ArrayList<>();

           byte[] mediumImage = new byte[80];
           dis.read(mediumImage);
           String mImage = depad(new String(mediumImage));
           imageUrls.add(mImage);

           byte[] originalImage = new byte[80];
           dis.read(originalImage);
           String oImage = depad(new String(originalImage));
           imageUrls.add(oImage);
           System.out.println(imageUrls);

           byte[] personLinkBytes = new byte[50];
           dis.read(personLinkBytes);
           String personLink = depad(new String(personLinkBytes));
           System.out.println(personLink);

           double myRating = dis.readDouble();
           System.out.println(myRating);

           ArrayList<String> myComments = new ArrayList<>();

           int commentCount = dis.readInt();
           System.out.println(commentCount);
           for(int i = 0; i < commentCount; i++)
           {
                byte[] myCommentsBytes = new byte[40];
                dis.read(myCommentsBytes);
                String myComment = depad(new String(myCommentsBytes));
                myComments.add(myComment);
           }
           System.out.println(myComments);
           x.add(new Person(score, queryName, name, id, imageUrls, personLink, myRating, myComments));

           persons.put(queryName, x); // -> Add person to Map               
       }
       dis.close();
   }
   return persons;
}

Pad代码:

    public static String pad(String s, int size)
{
    while (s.length() < size)
    {
        s = (char)0 + s;
    }
    return s;
}

Depad Code:

    public static String depad(String s)
{
    while(s.charAt(0) == (char)0)
    {
        s = s.substring(1);
    }
    return s;
}

1 个答案:

答案 0 :(得分:0)

看看你的代码:

public static String depad(String s)
{
    while(s.charAt(0) == (char)0)
    {
        s = s.substring(1);
    }
}

如果字符串s的大小为0,该怎么办?然后,您将按预期获得StringIndexOutOfBoundsException。在读取可能发生变化的数据时,您应该始终进行这些类型的检查。