need help with input stream

i am just starting with the output and input streams, and i thought it was going fine.

I am pretty sure the outputing to a file is working just fine, but whenever i try to read any of the chars that are in there, i get just a bunch of different hearts, diamonds, clubs, and clovers.

The reason i thought this was working was because i tried it once with a few chars and i read it in just fine, but when i tried it with a different program, it doesn't work.

for output i am using:

FileOutputStream fos=new FileOutputStream(FileName);

DataOutputStream dos=new DataOutputStream(fos);

and for input im using:

FileInputStream fis= new FileInputStream(FileName);

DataInputStream dis= new DataInputStream(fis);

If someone could please help me out, even a little bit, it would be greatly appreciated.

Thank you

Rob

[881 byte] By [rat141312a] at [2007-11-27 0:28:42]
# 1
Why are you using "DataInputStream" and "DataOutputStream"?If you are just reading and writing characters, just use FileInputStream and FileOuputStream.
KathyMcDonnella at 2007-7-11 22:30:20 > top of Java-index,Java Essentials,Java Programming...
# 2
i also have ints and doubles, but so far, im not getting any problems with those
rat141312a at 2007-7-11 22:30:20 > top of Java-index,Java Essentials,Java Programming...
# 3
I see. Okay, post a short snippet of the code. Preferrably a short compilable program that demonstrates the problem.
KathyMcDonnella at 2007-7-11 22:30:20 > top of Java-index,Java Essentials,Java Programming...
# 4

import java.io.*;

public class LoadTest

{

public static void main(String args[]) throws IOException

{

String filename="file.dat";

FileInputStream fis=new FileInputStream(filename);

DataInputStream dis=new DataInputStream(fis);

boolean Break=false;

String test="";

//the while will read chars from the file and put them in a string

//if the readChar picks up a ~, then it breaks from the loop.

while(!Break)

{

char temp=dis.readChar();

System.out.println(temp);//prints out what is being read

if(temp=='~')

Break=true;

else

test=String.format("%s%s",test,temp);

}

System.out.println(test);

//other chars will be read in

//then doubles,

//then ints

}

}

rat141312a at 2007-7-11 22:30:20 > top of Java-index,Java Essentials,Java Programming...
# 5

Your reading code looks fine. Perhaps the writing code was wrong.

See this works:import java.util.*;

import java.io.*;

public class Test {

public static void main(String args[]) throws IOException {

String filename="file.dat";

FileOutputStream fos=new FileOutputStream(filename);

DataOutputStream dos=new DataOutputStream(fos);

dos.writeChar('a');

dos.writeChar('b');

dos.writeChar('c');

dos.close();

fos.close();

FileInputStream fis=new FileInputStream(filename);

DataInputStream dis=new DataInputStream(fis);

while(true) {

char temp=dis.readChar();

System.out.println(temp);//prints out what is being read

System.out.flush();

}

}

}

But if you miswrote the writing code like this,

then you will get wrong result back:...

String filename="file.dat";

FileOutputStream fos=new FileOutputStream(filename);

DataOutputStream dos=new DataOutputStream(fos);

dos.write('a');

dos.write('b');

dos.write('c');

dos.close();

fos.close();

KathyMcDonnella at 2007-7-11 22:30:20 > top of Java-index,Java Essentials,Java Programming...
# 6
im not sure how big this is, but i guess i have been forgetting to use the .close() methods.also, im not sure what .flush() is.
rat141312a at 2007-7-11 22:30:20 > top of Java-index,Java Essentials,Java Programming...
# 7

close() is important if your code is reading and writing to the same file.

flush() is used to make sure the "System.out.println" shows up on screen

even if an error or exception occurred... (You can also use flush() on a file,

but close() will perform flush() automatically)

Once again, my point is, how are you writing the file?

If you are mistakenly using "write()" rather than "writeChar()",

then it could explain the symptoms you see.

KathyMcDonnella at 2007-7-11 22:30:21 > top of Java-index,Java Essentials,Java Programming...
# 8

im using writeChar()

Here is the purpose of the following method, i have some strings, that i am converting into char arrays, i know that part works, and the char arrays are being sent into this method to write them into the file

////writes a given char array to a given filename

public static void arrayIntoFile(char[] chArray, String filename) throws IOException

{

FileOutputStream fos=new FileOutputStream(filename);

DataOutputStream dos=new DataOutputStream(fos);

for(int a=0; a<chArray.length; a++)

{

dos.writeChar(chArray[a]);

}

}

this is as it is now, i haven't put .close() in it yet

and for the doubles and ints i am using:

dos.writeDouble()

dos.writeInt()

also, in the code you just gave me

while(true) {

char temp=dis.readChar();

System.out.println(temp);//prints out what is being read

System.out.flush();

}

is the while loop supposed to be set up as it is?

it looks like an infinite loop.>

rat141312a at 2007-7-11 22:30:21 > top of Java-index,Java Essentials,Java Programming...
# 9

> im using writeChar()

ok

> public static void arrayIntoFile(char[] chArray, String filename) throws IOException

> {

> FileOutputStream fos=new FileOutputStream(filename);

> DataOutputStream dos=new DataOutputStream(fos);

> for(int a=0; a<chArray.length; a++)

> {

>dos.writeChar(chArray[a]);

> }

> }

ok

> this is as it is now, i haven't put .close() in it yet

Try it. If your program reads and writes the file within the same session,

then you should close the file after the write, before you read.

> also, in the code you just gave me...

> the while loop supposed to be set up as it is?

> it looks like an infinite loop.

It is an infinite loop. :)

It was just a quick-and-dirty short code snippet

to make sure it reads every char. (When the file reaches end-of-file,

an exception is thrown which terminates the program).

And thus I added System.out.flush() so that, when the program terminates,

all println still show up properly.

KathyMcDonnella at 2007-7-11 22:30:21 > top of Java-index,Java Essentials,Java Programming...