我在我的服務器上打開一個txt文件,獲取Int並希望將int遞增1並將其再次寫入該文件。打開遠程文件並寫入它
我得到的文件,這種方法:
public int getCount() {
try {
URL updateURL = new URL("http://myserver.gov/text.txt");
URLConnection conn = updateURL.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
/* Convert the Bytes read to a String. */
String tmp = new String(baf.toByteArray());
int count = Integer.valueOf(tmp);
return count;
} catch(Exception e) {
Log.e(TAG, "getAdCount Exception = " + e);
e.printStackTrace();
return -1;
}
}
現在我簡單的增加計數並希望將其寫入文件。
我想通了,這是可以寫一個文件用這種方法:
BufferedWriter out = new BufferedWriter(new FileWriter("text.txt"));
out.write(count);
out.close();
但我怎麼打開遠程文件?我沒有找到辦法。謝謝!
#####編輯:#####
我寫了這個代碼:
URL url = new URL("http://myserver.gov/text.txt");
URLConnection con = url.openConnection();
con.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
out.write(count);
out.close();
但它不寫計數的文件。
我有什麼選擇? – Leandros 2012-01-31 15:02:11
我的不好,一秒鐘我以爲你不得不將文件傳輸到另一臺服務器,我會刪除我的意見 – Gevorg 2012-01-31 15:13:00