writing a string into a text file

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.PrintStream;

public class Main {

public static void main(String[] args) {

String result;

result = "testtest";

try {

PrintStream ps = new PrintStream(new FileOutputStream("D:\\test\\output.txt"));

ps.println(result);

ps.close();

} catch (Exception e) {

System.out.println(e);

}

}

}

I have a couple of string to write into text file

- for example--

String test1="aaa";

String test2="bbbb";

String test3="c";

String test4="dd";

condition to write into text file....

test1 has to write between 1 - 5 position.

test2 has to write between 6 - 15 postition.

test3 has to write between 16 - 10 position.

test4 has to write between 11 - 15 position.

-

how can i write those strings into a certain position?

how can i write those strings for 3 times in a text file?(make 3 rows)

[1036 byte] By [warapa] at [2007-11-27 8:35:04]
# 1

figure out how write them into a string, then use my helper util class like this:

String str = "some\nstuff\nhere";

ASCIIFile file = new ASCIIFile("c:/somefile.txt");

file.setContents(str);

//done, file written!

code for ASCIIFile can be found here:

http://www.doesthatevencompile.com/current-projects/code-sniplets/ASCIIFile.htm

change its package to work with your codes

mkoryaka at 2007-7-12 20:31:29 > top of Java-index,Java Essentials,New To Java...
# 2
how to set the position of string to write into a text file?
warapa at 2007-7-12 20:31:29 > top of Java-index,Java Essentials,New To Java...
# 3
You want to pad fields with spaces? Method format makes that easy: http://java.sun.com/javase/6/docs/api/java/io/PrintWriter.html#format(java.lang.String,%20java.lang.Object...)
Hippolytea at 2007-7-12 20:31:29 > top of Java-index,Java Essentials,New To Java...
# 4
yes..String test1="aaa";String test2="b";it has to write test1 into text file between 1 and 7.test2 writes between 8 and 10.so...the result will be like1 2 3 4 5 6 7 8 9 10a a ab b
warapa at 2007-7-12 20:31:29 > top of Java-index,Java Essentials,New To Java...
# 5
yes..String test1="aaa";String test2="b";it has to write test1 into text file between 1 and 7.test2 writes between 8 and 10.so...the result will be like1 2 3 4 5 6 7 8 9 10a a a s s s s b b ss means there is a space
warapa at 2007-7-12 20:31:29 > top of Java-index,Java Essentials,New To Java...
# 6
method format will do that quite nicely.
Hippolytea at 2007-7-12 20:31:29 > top of Java-index,Java Essentials,New To Java...
# 7
got it..String format = "%1$-10s%2$-10s%3$-20s\n";I tried this way...and it works...is there any better way?thanksMessage was edited by: warap
warapa at 2007-7-12 20:31:29 > top of Java-index,Java Essentials,New To Java...
# 8
You can drop the field numbers, since you aren't doing anything tricky,and its better to use %n for the newline character -- it's platform independent:"%-10s%-10s%-20s%n"
Hippolytea at 2007-7-12 20:31:29 > top of Java-index,Java Essentials,New To Java...