You are on page 1of 2

20/09/2016

javaOpenremotefileandwritetoitStackOverflow

signup

login

tour

help

Dismiss

AnnouncingStackOverflowDocumentation
WestartedwithQ&A.Technicaldocumentationisnext,andweneedyourhelp.
Whetheryou'reabeginneroranexperienceddeveloper,youcancontribute.

Signupandstarthelping

LearnmoreaboutDocumentation

Openremotefileandwritetoit

Iopenatxtfileonmyserver,gettheIntandwanttoincrementtheintby1andwriteittothe
fileagain.
Igetthefilewiththismethod:
publicintgetCount(){
try{
URLupdateURL=newURL("http://myserver.gov/text.txt");
URLConnectionconn=updateURL.openConnection();
InputStreamis=conn.getInputStream();
BufferedInputStreambis=newBufferedInputStream(is);
ByteArrayBufferbaf=newByteArrayBuffer(50);
intcurrent=0;
while((current=bis.read())!=1){
baf.append((byte)current);
}
/*ConverttheBytesreadtoaString.*/
Stringtmp=newString(baf.toByteArray());
intcount=Integer.valueOf(tmp);
returncount;
}catch(Exceptione){
Log.e(TAG,"getAdCountException="+e);
e.printStackTrace();
return1;
}
}

nowIsimplyincrementthecountandwanttowriteittothefile.
Ifiguredout,thatitispossibletowritetoafilewiththismethod:
BufferedWriterout=newBufferedWriter(newFileWriter("text.txt"));
out.write(count);
out.close();

ButhowIopentheremotefile?Idontfindaway.Thanks!
#####Edit:#####
Ihavewrittenthiscode:
URLurl=newURL("http://myserver.gov/text.txt");
URLConnectioncon=url.openConnection();
con.setDoOutput(true);
OutputStreamWriterout=newOutputStreamWriter(con.getOutputStream());
out.write(count);
out.close();

Butitdoesntwritethecounttothefile.
java http fileio
editedJan31'12at14:36

askedJan31'12at13:52

Leandros
12k

45

81

WhatoptionsIhave? Leandros Jan31'12at15:02

http://stackoverflow.com/questions/9080641/openremotefileandwritetoit

1/2

20/09/2016

javaOpenremotefileandwritetoitStackOverflow

mybad,forasecondIthoughtthatyouhadtotransferafiletoanotherserver,I'lldeletemycomments

GevorgJan31'12at15:13

2Answers

WhenyouwanttoworkwithURLConnectionyoucanfollowtheinstructionshere:Readingfrom
andWritingtoaURLConnection.
Update:YouwillalsoneedarunningserverhandlingPOSTrequeststoupdateyourcounter.
editedJan31'12at16:53

answeredJan31'12at14:06

mtsz
1,125

14

30

Thanks.Iusedthereference,andhavewrittenthiscode: URLurl=new
URL("http://myserver.gov/text.txt");URLConnectioncon=url.openConnection();
con.setDoOutput(true);OutputStreamWriterout=newOutputStreamWriter(con.getOutputStream());
out.write(count);out.close(); Buttheydidn'twritethecounttoit. Leandros Jan31'12at14:34
Whatserverisrunning?Howdoesithandlepostrequests? mtsz Jan31'12at16:09
Itisasimplewebspace. Leandros Jan31'12at16:13
WhenyouuseURLConnectionusuallyHttpURLConnection(docs.oracle.com/javase/6/docs/api/java/net/)

isinstantiated.ThenyoucommunicatewiththeserverviahttprequestlikeGETandPOST.Whenwriting
youareperformingaPOSTrequest,whenyourserverdoesnotknowwhattodowithPOSTrequests,this
willnotwork!Iassumethisisthecasewith"simplewebspace").Whenyoulookattheexampleinthelink
theanswer,youwillnoticeaservletrunningontheserver.mtsz Jan31'12at16:33
Nah.Ok,thanIhavetowriteaserverApp,too.Thanks. Leandros Jan31'12at16:49

Accordingtome.Whenyouareopenremotefile.Firstlyyouhavetoopenconnectionthanread
filecontent.
HttpClienthttpclient=newDefaultHttpClient();
HttpPosthttppost=newHttpPost(url);
HttpResponseresponse=httpclient.execute(httppost);
HttpEntityentity=response.getEntity();

thanyoucanwritefilecontent.
answeredJan31'12at14:04

PradeepSharma
35

http://stackoverflow.com/questions/9080641/openremotefileandwritetoit

2/2

You might also like