Hello,

I've a question about DataOutputStream. I'm trying to complete this Pseudo-sentence:

public void writeObjectToFile( DataOutputStream output )
{
  < Writing data field values ​​to file. >
}

This is my code:

public void writeObjectToFile( DataOutputStream output )
{
	String filename = "test.dta";
	try(output = new DataOutputStream(new FileOutputStream(filename)))
	{
		output.writeUTF(name);
		output.writeInt(age);
		output.writeUTF(city);
		output.writeUTF(country);
	}
	catch(IOException e3)
	{
		e3.printStackTrace();
	}
}

Whenever I try this, I get this error:

error: <identifier> expected 
try(output = new DataOutputStream(new FileOutputStream(filnavn)))
          ^

Well, I didn't clearly understand this. What did I do wrong?

Edited 4 Years Ago by Stjerne: n/a

try(output = new DataOutputStream(new FileOutputStream(filename)))

is wrong. This is incorrect try... catch syntax.

try
{
//
}
catch(Exception e)
{
//
}

dantinkakkar: That looks is an almost-right statement, this version is correct syntax:

try(DataOutputStream output = new DataOutputStream(new FileOutputStream(filename)))
   { ...

the problem is that you can't use a try-with-resources on something that's declared outside the try (eg by being passed as a parameter).

Stjerne: you are trying to create a DataOutputStream in your method, but you already have one - it's passed to you as a parameter. Just start writing to the output that is passed to you.The code

String filename = "test.dta";
DataOutputStream output = new DataOutputStream(new FileOutputStream(filename));

belongs in the method that calls writeObjectToFile, eg

String filename = "test.dta";
DataOutputStream output = new DataOutputStream(new FileOutputStream(filename));
writeObjectToFile(output);

Edited 4 Years Ago by JamesCherrill: n/a

Comments
Cool! I learnt someting new! :)

I don't understand how that could be wrong. I followed this example:

public void writeDataFile(String filename)
{
     try (DataOutputStream ut =
             new DataOutputStream(new FileOutputStream(filename)))   
     {
            // Methods
     }

They're almost the same, aren't they?

Well, it worked fine inside the try block, hmm.

Like I said - to use a try-with-resources (which is what that's called) you MUST declare the variable inside the try (...). In you original code the only difference was that the variable was declared outside the try (...) - it was declared as a parameter.
We got by until last year without try-with-resources, so I'm sure you can live without it for now. Especially since you don't want the new DataOutputStream... code anywhere in that method.

Sorry, didn't see your "earlier" post before now :D
So it would be like this then:

try
{
        output = new DataOutputStream(new FileOutputStream("test.dta"));
	output.writeUTF(name);
        output.writeInt(age);
	output.writeUTF(city);
	output.writeUTF(country);
        writeObjectToFile(output);
}

No - time to slow down a bit.
You are coding a method that will write an object to a given DataOutputStream. All the code to write the individual fields belongs inside that method, along with some try/catch error handling. But the DataOutputStream is given to you as a parameter, you do not, must not, create a new one of your own in your method.

When your method is complete, you (or someone else) can call it, and the person who calls it must create a DataOutputStream and pass it to you.

This article has been dead for over six months. Start a new discussion instead.