Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

input, output stream

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,

I have a problem...

The folowing code dont's work

String strOut = "test of stream";
String paht = "C:\\tmp\\test.log";

InputStream Entrada = System.in;
OutputStream Saida = System.out;

File fileLog = new File(path);
byte vectorbytes[] = new byte[1000];
vectorbytes = strOut.getBytes();

int sizebytes = Entrada.read(vectorbytes);
Saida.write(vectorbytes,0,sizebytes);
Saida = new FileOutputStream(fileLog);

Entrada.close();
Saida.close();

Exist any error?


Thanks and advance

Alan Brandt.
 
Hi Alan,

One problem is that you are creating Saida after you have attempted a write, which is maybe not what you had in mind. Saida is initiaaly pointing to System.out, so the write goes to the console. Then you are creating Saida as a file but nothing gets written to it. Also your original value of strOut gets overwritten by the read action before it gets used. The following works for me.

// Reads from System.in and writes to 'path'.
try {
String path = "C:\\tmp\\test.log";

InputStream entrada = System.in;
OutputStream saida;

File fileLog = new File(path);
byte vectorbytes[] = new byte[1000];

int sizebytes = entrada.read(vectorbytes);
saida = new FileOutputStream(fileLog);
saida.write(vectorbytes, 0, sizebytes);

entrada.close();
saida.flush();
saida.close();
} catch (IOException e) {
// handle any IO exception
}

HTH

scrat

Note: You will only get the first 1000 characters from System.in. To get more you would need to keep reading System.in, perhaps in a while loop, until vectorbytes is empty. Try setting vectorbytes size to [10] to see this effect.
 
Or simply you spelled path wrong when you initialized it :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top