Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

output to a file: newline character \n

Thread view: 
3rdshiftcoder - 16 Apr 2007 03:11 GMT
hi-

everytime i try to print out a newline character with writeBytes i get a
funny symbol
instead of a new line in my text file. what am i missing?  i use java 1.5x

thanks for any assistance,
jim

package demolib;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.*;
import java.util.List;
import java.util.Iterator;
import java.io.*;
public class ResetVendor {
public static void write(DataOutputStream outStream, String text) {
try {
outStream.writeBytes(text +" "+ "\n");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args)throws IOException{
Transaction tx = null;
Session session = HibernateUtil.getSessionFactory().openSession();
DataOutputStream outFile = new DataOutputStream(new
FileOutputStream("file4.txt"));
try {
tx = session.beginTransaction();
Query query = session.createQuery("from Payeeor");
List books = query.list();
for (Iterator iter = books.iterator(); iter.hasNext();) {
Payeeor vendor = (Payeeor) iter.next();
String s = vendor.getPayeepayor();
write(outFile,s);
//session.saveOrUpdate(vendor);
}
tx.commit();
} catch (HibernateException e) {
if (tx != null) tx.rollback();
throw e;
} finally {
session.close();
outFile.close();
}
}
}
3rdshiftcoder - 16 Apr 2007 03:25 GMT
hi-
i found that if i use \r\n it works.
that is strangely peculiar.
i will have to investigate this \r business.

thanks,
jim
Andrew Thompson - 16 Apr 2007 04:02 GMT
....
>i found that if i use \r\n it works.
>that is strangely peculiar.
>i will have to investigate this \r business.

Investigate System.getProperty("line.separator").  This
produces the correct line separator for each platform.
Also investigate using your shift key once at the
start of each sentence, to assist the reader.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Z. - 16 Apr 2007 04:03 GMT
> hi-
> i found that if i use \r\n it works.
> that is strangely peculiar.
> i will have to investigate this \r business.

Have you looked at System.getProperty("line.separator") ?
3rdshiftcoder - 16 Apr 2007 04:26 GMT
>>System.getProperty("line.separator")

Ok.
I will check out that method call.
Thanks very much for the tip.

jim
Karl Uppiano - 16 Apr 2007 07:09 GMT
>>>System.getProperty("line.separator")
>
> Ok.
> I will check out that method call.
> Thanks very much for the tip.

In addition, DataOutputStream is primarily intended for store and restore
operations, going round trip from live Java object, to file, back to live
Java object. The file written is not necessarily intended for external
consumption. The fact that you can open it in a text editor is
implementation dependent.

If you want to write strings to a readable text file, it might be better to
use something that gives you control of the output encoding, e.g., UTF-8,
ISO-5988, etc., using an OutputStreamWriter on a FileOutputStream.
Mike Schilling - 16 Apr 2007 07:26 GMT
>>>>System.getProperty("line.separator")
>>
[quoted text clipped - 11 lines]
> to use something that gives you control of the output encoding, e.g.,
> UTF-8, ISO-5988, etc., using an OutputStreamWriter on a FileOutputStream.

Or better yet a PrintWriter, which allows you to use println(), which will
figure out the proper line terminator for you.
Oliver Wong - 16 Apr 2007 16:27 GMT
> hi-
> i found that if i use \r\n it works.
> that is strangely peculiar.
> i will have to investigate this \r business.

   Read http://en.wikipedia.org/wiki/Newline

   - Oliver
Greg R. Broderick - 16 Apr 2007 23:54 GMT
"3rdshiftcoder" <go@away.com> wrote in news:4622db85$0$8960
$4c368faf@roadrunner.com:

> everytime i try to print out a newline character with writeBytes i get a
> funny symbol

In Java, char != byte.  Try writing characters with a character-oriented
OutputStream instead of using a byte-oriented Writer.

Cheers

Signature

---------------------------------------------------------------------
Greg R. Broderick            gregb+usenet200612@blackholio.dyndns.org

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------

Lew - 17 Apr 2007 00:18 GMT
> "3rdshiftcoder" <go@away.com> wrote in news:4622db85$0$8960
> $4c368faf@roadrunner.com:
[quoted text clipped - 4 lines]
> In Java, char != byte.  Try writing characters with a character-oriented
> OutputStream instead of using a byte-oriented Writer.

That would be a char-oriented Writer instead of a byte-oriented OutputStream.

Signature

Lew

Greg R. Broderick - 17 Apr 2007 22:56 GMT
>> "3rdshiftcoder" <go@away.com> wrote in news:4622db85$0$8960
>> $4c368faf@roadrunner.com:
[quoted text clipped - 8 lines]
> That would be a char-oriented Writer instead of a byte-oriented
> OutputStream.

Thanks!  Sometimes you've got to tell me "no, no, the OTHER right", after you
tell me to take a right at the next light, too ;-)

Signature

---------------------------------------------------------------------
Greg R. Broderick            gregb+usenet200612@blackholio.dyndns.org

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.