Creating and writing file from a FileOutputStream in Java -
okay, i'm working on project use java program initiate socket connection between 2 classes (a filesender
, filereceiver
). basic idea filesender
this:
try { writer = new dataoutputstream(connect.getoutputstream()); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } //while have bytes send while(filein.available() >0){ //we write them out our buffer writer.write(filein.read(outbuffer)); writer.flush(); } //then close our filein filein.close(); //and our socket; connect.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace();
the constructor contains code checks see if file exists, , socket connected, , on. inside filereader though:
input = recvsocket.accept(); bufferedreader br = new bufferedreader(new inputstreamreader(input.getinputstream())); fileoutputstream fout= new fileoutputstream(filename); string line = br.readline(); while(line != null){ fout.write(line.getbytes()); fout.flush(); line = br.readline(); } system.out.println("before recv close statements"); fout.close(); input.close(); recvsocket.close(); system.out.println("after recv clsoe statements");
all inside try-catch block. so, i'm trying have filesender
reading in file, converting bytes, sending , flushing out. filereceiver
, reads in bytes, writes fileout, flushes, , continues waiting more. make sure close things open, so... here comes weird part.
when try , open created text file in eclipse, tells me "an swt error has occured ... recommended exit workbench... see .log more details.". window pops saying "unhandled event loop exception, (no more handles)". however, if try open sent text file in notepad2, get
thisisasenttextfile
which (well, minus fact there should line breaks, i'm working on that...). know why happening? , while we're checking, how add line breaks?
(and particularly bad way transfer files on java without getting other libraries?)
edit: update: changed code following (filereceiver), without changing sender:
try { input = recvsocket.accept(); //inputstream br = new inputstream(input.getinputstream()); filewriter fout= new filewriter(filename); //bufferedwriter out = new bufferedwriter(fout); //string line = br. byte info = (byte) input.getinputstream().read(); while((int)info != 0){ fout.write(info); fout.flush(); info = (byte) input.getinputstream().read(); } fout.flush(); system.out.println("before recv close statements"); fout.close(); //input.close(); recvsocket.close(); system.out.println("after recv clsoe statements"); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); }
this works. text file right size in bytes (before getting 4kb standard size), , has right formatting. i'm going try on image next, , keep updated.
from first glance: why using dataoutputstream ? totally inadequate goal. use provided outputstream:
writer = connect.getoutputstream();
btw, it's dubious practice call "writer" variable, java makes clear conceptual difference between readers/writers (char oriented) , streams (byte oriented).
update: bad practice, calls trouble: mixing readers/writers (char oriented) , streams (byte oriented) unnecessarily - , without specifyng charset encoding.
bufferedreader br = new bufferedreader(new inputstreamreader(input.getinputstream()));
you must use reader when dealing text (in know encoding), use inputstream if dealing bytes.
Comments
Post a Comment