The Artima Developer Community
Sponsored Link

Java Answers Forum
writing to a file

7 replies on 1 page. Most recent reply: Nov 14, 2002 9:31 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
George

Posts: 10
Nickname: swiftcrash
Registered: Nov, 2002

writing to a file Posted: Nov 12, 2002 2:59 PM
Reply to this message Reply
Advertisement
anyone knows how i could write variables into a file
the first 10 chars is name , then last then address right next to each other..

file should look like

johndoe...542wellsway..23333333...9999
samdoe....456tommyway..55555555... 6666

any ideas would help
thanx


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: writing to a file Posted: Nov 12, 2002 3:59 PM
Reply to this message Reply
Integer is Serializable, so you could just wrap your ints in Integers and serialize.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: writing to a file Posted: Nov 12, 2002 4:49 PM
Reply to this message Reply
Come to think of it ObjectInputStream and ObjectOutputStream both have int (and other primitive) methods anyhow, so you could use writeInt() instead.

Furthermore, it seems that the results of
out.writeObject(new Integer(x));
and
out.writeInt(x);
are the same.

Finally, and most simply, if you have some kind of address object and all it contains is things like Strings and ints, then all you have to do is have it implement Serializable and you can dump the whole object to the stream.

George

Posts: 10
Nickname: swiftcrash
Registered: Nov, 2002

Re: writing to a file Posted: Nov 12, 2002 8:23 PM
Reply to this message Reply
Thanx. this are all stored in variables. most of them are strings and they need to go in a certain format in the file.
can you give me an example please, the books are not that great


if i have the file created and the name is afile

how do i opened it and write to it like
this variables
<%! String name;%>
<%! String address;%>
where i can write the variables to the first record then go to the next one and write the same. and is there such a method to add the rest of the record with whitespaces.
i could not fing any but setLength maybe.. does that put white spaces there?
do you know anywhere that i can reab about this on the net please...

Dante T. Salvador

Posts: 19
Nickname: anteng
Registered: Nov, 2002

Re: writing to a file Posted: Nov 13, 2002 8:41 AM
Reply to this message Reply
try reading this:

C:\jdk1.3.1\tutorial\essential\io\index.html

then code something. then go back here!

Dante T. Salvador

Posts: 19
Nickname: anteng
Registered: Nov, 2002

Re: writing to a file Posted: Nov 13, 2002 8:44 AM
Reply to this message Reply
try reading this:

http://java.sun.com/docs/books/tutorial/essential/io/index.html

then code something, then come back here!

George

Posts: 10
Nickname: swiftcrash
Registered: Nov, 2002

Re: writing to a file Posted: Nov 13, 2002 8:11 PM
Reply to this message Reply
thanx. i appreciate the help anyhow... like you said i will figure it out like usual.

Thanx

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: writing to a file Posted: Nov 14, 2002 9:31 AM
Reply to this message Reply
Okay, sounds like you want to write to a fix-width text file. On the other hand, your example looks a bit like JSP. Anyway, if you do want to write lines that are all the same length and have different elements of information at fixed positions, then that would probably be easy enough to do by building a list of Strings (or StringBuffers, more likely) that are formatted as you like and then writing these to the text file.

By the way, there is a difference between whitespace, which is an unspecified number (and or/mixture) of spaces, tabs or newlines, versus just spaces. I think you want to use spaces, not whitespace.

Flat View: This topic has 7 replies on 1 page
Topic: JDBC Previous Topic   Next Topic Topic: Converting a String to an Integer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use