cancel
Showing results for 
Search instead for 
Did you mean: 

InputStreamReader and FileOutputStream

Former Member
0 Kudos

hello everyone,

im trying to upload a file to a km server, and im using this code below,

the problem is that it seems to write only the first character of the text file, or in case of other file it doesnt work at all.

for example,

if a text file is:

sally bought a nice sandwich

and then went home.

only "s" will be written to the file which i later check on the server.

whats wrong?

thanks in advance, Eli.

the code:

String fileName = wdContext.currentFileElement().getFileResource().getResourceName();

InputStream inputStream = wdContext.currentFileElement().getFileResource().read(false);

File destfile = new File(fileName);

FileOutputStream out = new FileOutputStream(destfile);

if (inputStream != null)

{

out.write(inputStream.read());

}

BufferedInputStream bufInStream = new BufferedInputStream(new FileInputStream(destfile));

Content con = new Content(bufInStream, "byte", -1);

IResource newResource = folder.createResource(fileName, null, con);

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Probably a missing close() statement.

Armin

Former Member
0 Kudos

i've tried the following code after your suggestion ( i did forget a close statement).

To my logic , i closed the fileoutputstream after the If statement, and then bufferedinputstream after creating the content, still no help, i keep getting this one single character.

String fileName = wdContext.currentFileElement().getFileResource().getResourceName();

InputStream inputStream = wdContext.currentFileElement().getFileResource().read(false);

File destfile = new File(fileName);

FileOutputStream out = new FileOutputStream(destfile);

if (inputStream != null)

{

out.write(inputStream.read());

}

out.close();

BufferedInputStream bufInStream = new BufferedInputStream(new FileInputStream(destfile));

Content con = new Content(bufInStream, "byte", -1);

bufInStream.close();

IResource newResource = folder.createResource(fileName, null, con);

Former Member
0 Kudos

InputStream.read() reads only one byte from stream. Use a InputStreamReader wrapped by a BufferedReader.

See http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStreamReader.html

Armin

Former Member
0 Kudos

hi ,

thanks for the quick answer,

i tried using the read method of the buffered reader but it's read methods (except the readline) dont return anything to me

i get zero read characters, and an empty array of chars.

only if specify the size of the array hard coded, i get an array of chars, but I dont want to hard code it, i need to somehow get the size of the needed array.

and also, how will i write this back into a file? (I can only write byte arrays), will this work on xls files? or pictures?

Edited by: Eli Eren on Nov 24, 2008 10:17 PM

Edited by: Eli Eren on Nov 24, 2008 10:28 PM

Former Member
0 Kudos

i eventually looped untill Read returned -1.

thanks for trying to help though, much appreciated.

Answers (0)