4 Replies - 2111 Views - Last Post: 16 December 2010 - 07:18 AM Rate Topic: -----

#1 nacholibre   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

java outFile stream passing to a function

Posted 15 December 2010 - 08:36 PM

Hello guys, i am kinda dumbfounded not to find an example that shows passing a file stream among methods.
Can anyone show me how?

something like this in C++
void myfunction(ifstream &fp, ...) 

is it the same?
Is This A Good Question/Topic? 0
  • +

Replies To: java outFile stream passing to a function

#2 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: java outFile stream passing to a function

Posted 15 December 2010 - 08:54 PM

In JAva, just pass the object. Java will take care of the pointers for you. ;)
Was This Post Helpful? 0
  • +
  • -

#3 nacholibre   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

Re: java outFile stream passing to a function

Posted 15 December 2010 - 10:02 PM

thanks that worked.

no I am kinda having problem with writing a string to the file

so i have the following; out is the out file stream.

String[] data = str.split("/n");

try{
   out.write(data[i]);
}catch(java.io.IOException exp){ exp.printStackTrace();}


the error i am getting is..

main.java:115: cannot find symbol
symbol  : method write(java.lang.String)
location: class java.io.DataOutputStream
                                out.write(data[i]);


writeUTF instead of write worked fine but the file was formatted weird as expected...

any idea how i can write regular string to a file?
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: java outFile stream passing to a function

Posted 16 December 2010 - 07:15 AM

Use a DataOutputStream on the FileOutputStream. The DataOutputStream is the programmer-friendly class for output streams in general.

API: http://download.orac...tputStream.html

Here's an example:
DataOutputStream dos = new DataOutputStream(
        new FileOutputStream("filename.file"));



DataOutputStream has 3 methods which directly print Strings:
writeChars: This simply outputs the String as a bunch of characters (the file type is a character file type and not a binary data type).
writeBytes: This outputs the String AS bytes (good for storing Strings in binary files)

And you are familiar with the third, writeUTF().
Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: java outFile stream passing to a function

Posted 16 December 2010 - 07:18 AM

If all you're writing is a String type, then you should probably be using a Writer, such as FileWriter
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1