output to a file: newline character \n

3

3rdshiftcoder

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();
}
}
}
 
3

3rdshiftcoder

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
 
A

Andrew Thompson

3rdshiftcoder wrote:
....
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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
Z

Z.

3rdshiftcoder said:
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") ?
 
3

3rdshiftcoder

System.getProperty("line.separator")

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

jim
 
K

Karl Uppiano

3rdshiftcoder said:
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.
 
M

Mike Schilling

Karl Uppiano said:
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.

Or better yet a PrintWriter, which allows you to use println(), which will
figure out the proper line terminator for you.
 
G

Greg R. Broderick

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

--
---------------------------------------------------------------------
Greg R. Broderick (e-mail address removed)

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

Lew

Greg said:
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.
 
G

Greg R. Broderick

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 ;-)

--
---------------------------------------------------------------------
Greg R. Broderick (e-mail address removed)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top