Results 1 to 14 of 14
  1. #1
    Join Date
    Apr 2010
    Posts
    20
    Rep Power
    0

    Default writing string array in to a file

    I m new to java. I am using fileoutputstream writer to write an array of bytes in to a file on a single write.
    Is there a way by which i can write array of string in to a file by just one write.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    Put the array in an object and use an ObjectOutputStream.

  3. #3
    Join Date
    Apr 2010
    Posts
    20
    Rep Power
    0

    Default why do i see extra characters

    Thank you for you suggestion. I am using now writeobject method to just print a small string "abcd" in a file but i see some characters printed along with this. How to over come these characters.

    Here is the short code:

    import java.io.*;
    import java.io.Serializable;

    public class main {


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

    FileOutputStream buf = new FileOutputStream(new File ("C://test1//"+ "demo.txt"),true);
    ObjectOutputStream o = new ObjectOutputStream(buf);
    String s= "abcd";

    o.writeObject(s);


    }

    }

    Result:

    t abcd

    Expected:
    abcd.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    i see some characters printed along with this. How to over come these characters.
    The data written to the file is an object, not the characters from the String. There is extra data written to the file to support it being an object.

    Your example doesn't show you writing an array of String.
    What is it that you want to do?
    When the String array is written to the file, what do you want to do with it?
    Do you expect there to be any newline characters in the file?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by abdullahansari View Post
    I m new to java. I am using fileoutputstream writer to write an array of bytes in to a file on a single write.
    Is there a way by which i can write array of string in to a file by just one write.
    Nope, you have to loop over the elements and write those; it doesn't take much:

    Java Code:
    FileWriter fw= ...;
    String[] array= ...;
    
    for (String s: array)
       fw.write(s);
    kind regards,

    Jos

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    You can write the array to a file with one write statement.
    Java Code:
           // Write out a String array
           try {
             String[] sA = {"string 1", "second string", "third Str"};
             FileOutputStream buf = new FileOutputStream(new File ("StringArray_demo.txt"), true);
             ObjectOutputStream oos = new ObjectOutputStream(buf);
             
             oos.writeObject(sA);  // Write the String array to a file
             oos.close();
    
             // Now try to read it in
             FileInputStream fis = new FileInputStream(new File ("StringArray_demo.txt"));
             ObjectInputStream ois = new ObjectInputStream(fis);
             String[] sAI = (String[])ois.readObject();
             System.out.println("sAI=" + Arrays.toString(sAI)); //sAI=[string 1, second string, third Str]
    
           }catch(Exception x) {
             x.printStackTrace();
             return;
           }

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    You can write the array to a file with one write statement.
    True, but the OP doesn't want any additional characters in the file ... (and I'm not sure if the OP is talking about Strings of chars or sequences of bytes).

    kind regards,

    Jos

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    What about newline chars between the individual Strings?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    What about newline chars between the individual Strings?
    Dunno, if I apply Occam's razor we can consider those additional (unwanted) characters too ;-)

    kind regards,

    Jos

  10. #10
    Join Date
    Apr 2010
    Posts
    20
    Rep Power
    0

    Default I dont want to read

    Norm,
    Yes i am not writing an array of strings but i was trying to see if i can write a string and look at the file.
    I dont want to read back the file that i have written.
    I just want to view the data that is written and i dont want some additional characters in the file.

    JosAH,
    The reasing y i dont want to use for loop is that i am trying to receive packets from the ethernet port and so doesnot want to perform multiple writes to the file which consumes more time than a single write.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    I just want to view the data that is written
    It all depends on what you mean by "view". The code I posted is able to "view" the contents of the file by reading it.

    If you use buffering for your writes, the actual I/O won't occur every write.

  12. #12
    Join Date
    Apr 2010
    Posts
    20
    Rep Power
    0

    Default view

    view here means just be able to open the file and see the data that was written from the program. (not reading from the program).
    I didnt get the "buffer"concept. What i am doing is getting data from udp and writing it in to file. Right now, If i collect data on RAM and then perform a single write then there are less missed packet but on the other hand if i write continously in to file.. then the missed packet are significantly larger.

    Pls let me know if there is any other idea to get around this issue.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    able to open the file and see the data that was written
    My program does that.

    I didnt get the "buffer"concept.
    Look at the classes whose names begin with Buffered.
    Some of then are for output.
    The API doc says:
    an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.

  14. #14
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    are we over complicating this, surely this is just basic character streams? Printwriter and BufferedReader no?

Similar Threads

  1. String array from file to ComboBox
    By cselic in forum AWT / Swing
    Replies: 3
    Last Post: 05-06-2010, 05:29 PM
  2. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  3. Replies: 3
    Last Post: 08-24-2008, 01:51 PM
  4. Writing a countdown array to a file.
    By kewlgeye in forum New To Java
    Replies: 6
    Last Post: 05-25-2008, 06:09 AM
  5. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •