Hey,

I want to know whether it is possible to send a formatted string ie.

System.out.printf("%2d%6d%23f...",a,b,c);

to a .txt file so that when I view the .txt it will appear properly justified with spaces and tabs.

Thanks

Recommended Answers

All 2 Replies

You will require to use PrintStream for that

e.g.

import java.io.FileOutputStream;
import java.io.PrintStream;

public class FormattedFileIO{
  public static void main(String[] args) {
		int a=1;
		int b=2;
		double c=3.45;

		System.out.printf("%2d%6d%23f...",a,b,c);

		FileOutputStream out; 
        PrintStream ps;
        try {
			out = new FileOutputStream("myfile.txt");
            ps = new PrintStream(out);
            ps.format("%2d%6d%23f...",a,b,c);
            ps.close();
        }
        catch (Exception e){
			e.printStackTrace();
        }
  }

}

Thanks a lot the printstream worked wonders.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.