Results 1 to 14 of 14
- 01-12-2009, 06:13 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 42
- Rep Power
- 0
Help! - How to insert a new line to a text file
Hi all,
I have written a seimple program to test out a bigger idea.
This reads 2 columns from a database (about 477 rows at the moment)
and it then writes these to a text file.
I need to put each row on a new line in the text file but '\n' seems to just return a special character in the .txt
Can anybody suggest how I do this?
here is my code:
Thanks in advance,Java Code:import java.sql.*; import java.io.*; import java.io.FileWriter; class SimpleOraJava { public static void main(String args[]) throws SQLException, IOException { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); String serverName = "servername"; int port = 1521; String user = "user"; String password = "password"; String SID = "nikuuat"; String URL = "jdbc:oracle:thin:@" + serverName + ":" + port + ":" + SID; Connection conn = DriverManager.getConnection(URL, user, password); String SQL = "SELECT DSTI_PROJ_REF, id FROM NIKU.ODF_CA_PROJECT where dsti_proj_act = 'ADM'"; Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery(SQL); FileWriter fout = new FileWriter("c:\\test.txt"); //Create new file stream while (rs.next()) { //cycle through result set fout.write(rs.getString(1) + "\t" + rs.getInt(2) + "\n"); } fout.close(); //close file stream System.out.println("Complete"); stat.close(); conn.close(); } }
MattLast edited by matpj; 01-12-2009 at 06:14 PM. Reason: forgot to say thank you!
- 01-12-2009, 08:30 PM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Use a BufferedWriter
:)Java Code:BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\test.txt")); bw.newLine();
~MattI die a little on the inside...
Every time I get shot.
- 01-12-2009, 08:41 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 42
- Rep Power
- 0
thanks for the reply!
does that mean it cannot be done with FileWriter?
what are the pros/cons of BufferedWriter compared to FileWriter?
Thanks again,
Matt
- 01-13-2009, 09:10 AM #4
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
from
BufferedWriter (Java 2 Platform SE 5.0)
Write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.
In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,
- 01-13-2009, 09:21 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
With FileWriter you can use system properties as well.
Then simply write this newLine once you write a line of text.Java Code:String newLine = System.getProperty("line.separator");
- 01-13-2009, 01:08 PM #6
Member
- Join Date
- Sep 2008
- Posts
- 42
- Rep Power
- 0
Thanks for that - it works brilliantly.
Whilst I am at it, I need to actually make some of the fields a fixed length on the text file. Is this easy to acheive with the code I currently have (above)?
thanks in advance,
Matt
- 01-13-2009, 01:55 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you bit more clearly explain what you want to do here? Are you want to make the fixed length on the file text? What that file content, and how those data added to the file?
- 01-13-2009, 02:59 PM #8
Member
- Join Date
- Sep 2008
- Posts
- 42
- Rep Power
- 0
Hi,
in the code above, I am selecting 2 columns, and writing them to the file with a tab seperating them.
The first of the two fields I am returning I want to make 32 characters (no matter how many come back from the database).
this will result in the value being padded to the right by a certain number of spaces.
for example, I currently get the following output:
XML Code:INT5603BL 5000033 INT5700BL 5000035 TST14JAN 5000002 INT5200BL 5000019 INT5301BL 5000027 INT6002BL 5000170 INT4400BL 5000081 INT4000BL 5000083 INT1100BL 5000084
this is just two fields seperated with a tab.
I want to be able to say "give me the first field, but make sure the total length is 11 charaters, then give me the second field, making the output:
The actual end result will be several fields all set to specified lengths (in my spec)XML Code:INT5603BL 5000033 INT5700BL 5000035 TST14JAN 5000002 INT5200BL 5000019 INT5301BL 5000027 INT6002BL 5000170 INT4400BL 5000081 INT4000BL 5000083 INT1100BL 5000084
I have tried using:
but this pads the value to the LEFT making the total number of characters (including the value returned from the database) 11. I need the trailing spaces to the right.Java Code:fout.write(String.format("%11s",rs.getString(1)) + "\t" + rs.getInt(2) + newLine);
Does this make sense?
Regards,
matt
- 01-13-2009, 03:43 PM #9
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Yea, it does, for me. ;) You could, of course, write a simple method to pad it to the right, it probably also can be done with some method similar to the one you used now, but I wouldn't know.
I die a little on the inside...
Every time I get shot.
- 01-13-2009, 04:24 PM #10
Member
- Join Date
- Sep 2008
- Posts
- 42
- Rep Power
- 0
Hi,
well I have now found a method which seems to work ok:
being extremeley wet behind the ears when it come sto Java, i'm not exactly sure I am doing things the right (or best) way.Java Code:public static String padRight(String s, int n) { return String.format("%1$-" + n + "s", s); }
I've simply pasted that method above my Main method.
Everything is in the one class at the moment.
I'm sure it will work, but it seems a bit too amateur.
I'm trying to think wether I need to split things out into other classes - but i'm not sure if I can or need to...
- 01-15-2009, 03:28 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Formatting is the best you have to use. And I don't think you need to go for a separate class to do this formatting. It's just need if you want to use your own formatting process other than JDK APIs.
- 02-23-2010, 10:28 PM #12
Member
- Join Date
- Feb 2010
- Posts
- 1
- Rep Power
- 0
to write newline in .txt
I suggest, u can add "\r\n" into your source code
original code
fout.write(rs.getString(1) + "\t" + rs.getInt(2) + "\n");
replace with
fout.write(rs.getString(1) + "\t" + rs.getInt(2) + "\r\n");
-
- 02-24-2010, 05:28 PM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
remove variables/line in a text file
By ddatta8 in forum New To JavaReplies: 2Last Post: 01-04-2009, 03:05 AM -
Saving To A New Line Using A Text File
By jadaleus in forum Advanced JavaReplies: 10Last Post: 10-24-2008, 07:21 PM -
Saving To A New Line Using A Text File
By jadaleus in forum Advanced JavaReplies: 1Last Post: 10-24-2008, 12:31 AM -
saving to a new line a text
By jadaleus in forum Advanced JavaReplies: 1Last Post: 10-20-2008, 06:10 PM -
Reading in data from file line by line
By bluekswing in forum New To JavaReplies: 1Last Post: 10-02-2007, 12:19 AM


1Likes
LinkBack URL
About LinkBacks


Bookmarks