我正在从事一个大学项目,在那里我必须从TVMaze API中将名人数据提取到Java。
我使用" dis"将搜索结果存储到二进制文件中,并且由于我对象,我会在读取/写入中添加文件。
当我试图将文件加载到哈希图时,我会遇到错误。
线程中的异常" main" java.lang.stringindexoutofboundsexception:字符串索引超出范围:0
该错误被指向负载方法和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代码:
public static String depad(String s)
{
while(s.charAt(0) == (char)0)
{
s = s.substring(1);
}
return s;
}
查看您的代码:
public static String depad(String s)
{
while(s.charAt(0) == (char)0)
{
s = s.substring(1);
}
}
如果字符串s
的大小为0,该怎么办?然后,您将按预期获得StringIndexOutOfBoundSexception。在阅读可以更改的数据时,您应该始终执行这些类型的检查。