首先,对不起,我的英语很差。 我正在尝试编写一个函数,它将有关whatching视频的信息发送到服务器。当服务器无法访问时,它会将其写入文件并稍后发送。因此,我正在向我的函数发送文件名,使用未命名的名称加载文件,创建一个名称数组并逐个发送所有这些名称。如果出现任何错误,请将它们写回文件。
问题是,当我打开保存的文件时,字符串的第一个符号消失了。在axaclty:写入或读取文件时不要回避。
这是我的代码(它包含一些不必要的操作。它用于测试):
void sendLog (String name) {
String FILENAME = "Log";
String strToSave = "";
String logString = null;
//opening file, just to see what it cantains:
FileInputStream fis;
try {
fis = openFileInput(FILENAME);
fis.read();
ByteArrayOutputStream content = new ByteArrayOutputStream();
int readBytes = 0;
byte[] sBuffer = new byte[512];
while ((readBytes = fis.read(sBuffer)) != -1) {
content.write(sBuffer, 0, readBytes);
}
logString = new String(content.toByteArray());
fis.close();
Log.d ("Log", "Loaded log: '"+logString+"'");
} catch (IOException e) {
e.printStackTrace();
}
//saving empty file.
FileOutputStream fos;
try {
Log.d ("Log", "Saving log: '"+strToSave+"'");
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(strToSave.getBytes());
fos.close();
} catch (IOException e) {
Log.d ("File WRITE", "can't write Log file");
e.printStackTrace();
}
//opening it agane, allready empty.
try {
fis = openFileInput(FILENAME);
fis.read();
ByteArrayOutputStream content = new ByteArrayOutputStream();
int readBytes = 0;
byte[] sBuffer = new byte[512];
while ((readBytes = fis.read(sBuffer)) != -1) {
content.write(sBuffer, 0, readBytes);
}
logString = new String(content.toByteArray());
fis.close();
Log.d ("Log", "Loaded log: '"+logString+"'");
} catch (IOException e) {
e.printStackTrace();
}
int cnt;
//splitting loaded string to array
if (logString.length()>1) {
logString = logString+name+",";
Log.d ("Log", "spliting log. length: "+logString.length());
log = logString.split(",");
long length = logString.length();
long length2 = 0;
cnt = 0;
//sorry for this code. Is there a better solution to know, how many strings in array?
for (int i = 0; length2<length; i++) {
length2 += playList[i].length()+1;
cnt++;
}
}
else {
log = new String[1];
log[0] = name+",";
cnt = 1;
}
//sending them one by one
for (int i = 0; i<cnt; i++) { //playList[i].substring(2, playList[i].length()-1)
Log.d ("Log", "Sending log: '"+log[i].substring(0, log[i].length()-1) + "'");
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("server link here"
+log[i].substring(0, log[i].length()-1)); //need to exclude ","
try {
HttpResponse response = client.execute(request);
} catch (ClientProtocolException e) {
//adding unsended name to string if any errors
strToSave = strToSave+log[i];
e.printStackTrace();
} catch (IOException e) {
strToSave = strToSave+log[i];
e.printStackTrace();
}
}
//here should be empty string, but for testing i'm adding some:
if (strToSave.length() == 0) strToSave = strToSave+log[0];//strToSave = " ";
//saving it to file
try {
Log.d ("Log", "Saving log: '"+strToSave+"'");
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(strToSave.getBytes());
fos.close();
} catch (IOException e) {
Log.d ("File WRITE", "can't write Log file");
e.printStackTrace();
}
}
所以,这里是登录DDMS:
10-31 09:23:17.446: DEBUG/Log(2571): Loaded log: 'UB_MENUS,'
10-31 09:23:17.446: DEBUG/Log(2571): Saving log: ''
10-31 09:23:17.446: DEBUG/Log(2571): Loaded log: ''
10-31 09:23:17.446: DEBUG/Log(2571): Sending log: 'UPLOAD_SCREEN'
10-31 09:23:17.746: DEBUG/Log(2571): Saving log: 'UPLOAD_SCREEN,'
10-31 09:23:39.386: DEBUG/Log(2571): Loaded log: 'PLOAD_SCREEN,'
10-31 09:23:39.386: DEBUG/Log(2571): Saving log: ''
10-31 09:23:39.386: DEBUG/Log(2571): Loaded log: ''
10-31 09:23:39.386: DEBUG/Log(2571): Sending log: 'APP_BUTTON'
10-31 09:23:39.646: DEBUG/Log(2571): Saving log: 'APP_BUTTON,'
...
等等。
答案 0 :(得分:0)
这是什么?
try {
fis = openFileInput(FILENAME);
//********************************************
fis.read(); // <<-- Danger, Will Robinson !!
//********************************************
ByteArrayOutputStream content = new ByteArrayOutputStream();
int readBytes = 0;
byte[] sBuffer = new byte[512];
while ((readBytes = fis.read(sBuffer)) != -1) {
content.write(sBuffer, 0, readBytes);
}
logString = new String(content.toByteArray());
fis.close();
Log.d ("Log", "Loaded log: '"+logString+"'");
} catch (IOException e) {
e.printStackTrace();
}
裸fis.read()
将读取第一个字节并将其丢弃。然后,您将读取输入流的其余部分以填充sBuffer
。这是你缺少角色的最可能原因。