Results 1 to 14 of 14
- 08-26-2010, 09:10 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 20
- Rep Power
- 0
- 08-26-2010, 10:21 PM #2
Put the array in an object and use an ObjectOutputStream.
- 08-27-2010, 06:54 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 20
- Rep Power
- 0
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.
- 08-27-2010, 07:06 PM #4
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.i see some characters printed along with this. How to over come these characters.
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?
- 08-27-2010, 07:40 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 08-27-2010, 07:51 PM #6
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; }
- 08-27-2010, 07:55 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 08-27-2010, 07:59 PM #8
What about newline chars between the individual Strings?
- 08-27-2010, 08:28 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 08-27-2010, 09:17 PM #10
Member
- Join Date
- Apr 2010
- Posts
- 20
- Rep Power
- 0
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.
- 08-27-2010, 09:36 PM #11
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.I just want to view the data that is written
If you use buffering for your writes, the actual I/O won't occur every write.
- 08-27-2010, 09:48 PM #12
Member
- Join Date
- Apr 2010
- Posts
- 20
- Rep Power
- 0
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.
- 08-27-2010, 10:17 PM #13
My program does that.able to open the file and see the data that was written
Look at the classes whose names begin with Buffered.I didnt get the "buffer"concept.
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.
- 08-29-2010, 04:25 AM #14
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
String array from file to ComboBox
By cselic in forum AWT / SwingReplies: 3Last Post: 05-06-2010, 05:29 PM -
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 01:29 AM -
Writing integer pixel array(Range:0-255) into .txt file
By Mazharul in forum Java 2DReplies: 3Last Post: 08-24-2008, 01:51 PM -
Writing a countdown array to a file.
By kewlgeye in forum New To JavaReplies: 6Last Post: 05-25-2008, 06:09 AM -
Reading/Writing a File using byte array
By Java Tip in forum Java TipReplies: 0Last Post: 01-16-2008, 10:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks