PDA

View Full Version : Map 2 File و بر عکس



irGeek
شنبه 22 مهر 1391, 16:53 عصر
من یک کد به این شکل دارم که به هیچ وجه نمی تونم تغییرش بدم
public Map readFile2Map(InputStream inputstream)
throws IOException
{
HashMap hashmap = new HashMap();
inputstream.skip(SKIP_BYTE);
int i = inputstream.read();
System.out.println(i +" map size");
inputstream.skip(ANOTHER_SKIP_BYTE - 1);
for(int j = 0; j < i; j++)
{
String s = this.readMap(inputstream);
String s1 = readMap(inputstream);
hashmap.put(s, s1);
}

return hashmap;
}
private String readMap(InputStream inputstream)
throws IOException
{
int i = inputstream.read();
System.out.println(i +" property len");
inputstream.skip(ANOTHER_SKIP_BYTE - 1);
char ac[] = new char[i];
for(int j = 0; j < i; j++)
{
ac[j] = (char)inputstream.read();
inputstream.skip(ANOTHER_SKIP_BYTE - 1);
}

return new String(encryptCharArray(ac));
}

public static char[] encryptCharArray(char ac[])
{
for(int i = 0; i < ac.length; i++)
ac[i] = (char)(ac[i] ^ 0xd8);

return ac;
}


کاری که می خوام بکنم اینکه یک سری مشخصه هار و در یک مپ زخیره و باز یابی کنم
کد من این شکلی هستش:

public void writeMap2File(String fileName)
throws IOException
{
FileOutputStream file = new FileOutputStream(fileName);
file = new FileOutputStream(fileName);
writeMap2File(file,m);
file.close();

}
private void writeMap2File(OutputStream outputstream,Map map)
throws IOException
{
outputstream.write(map.size());
Set keys= map.keySet();
System.out.println(keys.size() +" key size?!");
while(keys.iterator().hasNext()){
String k=(String)keys.iterator().next();

System.out.println(k +" =key ");
writeMapProperty(outputstream,k);
writeMapProperty(outputstream,(String)map.get(k));
System.out.println((String)map.get(k) +" =val ");
keys.remove(k);


}



}
private void writeMapProperty(OutputStream outputstream ,String property)
throws IOException
{
int i = property.length();
char[] chat=new char[i];
property.getChars(0, i, chat, 0);

outputstream.write(i);
for (int j=0; j<i; j++){
int l=(chat[j]);
outputstream.write(l);
}


}

این متغیر ها هم که لازمه

public static int SKIP_BYTE = 214;
public static int ANOTHER_SKIP_BYTE = 21;
public Map m;


اینجوری هم فرا خوانی میشه:

public String lic(String s){
try
{
FileInputStream fileinputstream = new FileInputStream(s);
fileinputstream = new FileInputStream(s);
m = readFile2Map(fileinputstream);
fileinputstream.close();
System.out.println( m.toString());
System.out.println(m.size()) ;
return m.toString();

}
catch(IOException _ex)
{
return "";
}
مشکل کدم کجاست که فایل نوشته شده رو نمی تونه بخونه؟
ممنون

همایون افشاری
دوشنبه 08 آبان 1391, 19:05 عصر
باید خروجی رو هم برامون بزارید.
اگه خروجی خاصی مشاهده نمی شود (!) این کد رو داخل catch آخر قبل از return بزارید:
_ex.printStacktrace();