0

I am using BufferedWriter to write strings to a file like this:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Test {

    public static void main(String[] args) throws IOException {
       String myname = "JOHN DOE MAXWELL";
       String myotherName = "MELCHIZEDEK NEBUCHARDINEZZAR";
       String mylocation = "SOUTH EAST";
       String myotherlocation = "SOUTH WEST";
       File f = new File("MyFile.txt");
       BufferedWriter bw  = new BufferedWriter(new FileWriter(f));
       bw.write(myname + "                       " + mylocation);
       bw.newLine();
       bw.write(myothername + "                  " + myotherlocation);
       bw.close();
    }
}

I need to write mylocation such that whatever the length of the string myname, the beginning position of mylocation will not be affected. Please assist.

My Outputshould be:

JOHN DOE MAXWELL          SOUTH EAST
MELCHIZEDEK NEBUCHARDI    SOUTH WEST
ErrorNotFoundException
  • 3,700
  • 23
  • 88
  • 157

4 Answers4

3

You could do

bw.write(String.format("%-20s%s%n", myName, myLocation));

You can use PrintWriter to use printf() which does both.

e.g. Using PrintWriter

pw.printf("%-" + myNameWidth + "s%s%n", myName, myLocation);
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
  • Good answer. One could also produce a generic pad-to-length method, using the same technique. See http://stackoverflow.com/a/391978/474189 for examples. – Duncan Jones Apr 11 '13 at 13:20
  • Out of curiousity what if I wanted three Strings like `bw.write(String.format("%-20s%s%n", myName, myLocation, MyotherString));` – ErrorNotFoundException Apr 12 '13 at 06:44
  • 1
    You need another `%-20s` or `%-32s` or whatever you desired width is. Note the `-` assumes you want left justified. Without it the string is right justified like numbers are. – Peter Lawrey Apr 12 '13 at 08:18
  • If I do `%-20s` the Third String will start right after the second without the space, if I do `%20s` if the two third strings are not of equal lentgth the longer will start first, WHat does s represent? – ErrorNotFoundException Apr 12 '13 at 13:26
  • 1
    The order doesn't change, it will always be the order you give. The number is the width of each field, not to offset from the start. – Peter Lawrey Apr 12 '13 at 13:36
  • 1
    Using "%20s%-20s" can result in the two strings running into each other so you can do "%20s %-20s" to ensure there is always one space between them. – Peter Lawrey Apr 12 '13 at 13:38
  • 1
    Oh ok Thank you , now I got it completely. – ErrorNotFoundException Apr 12 '13 at 13:58
1

such that whatever the length of the string myname, the beginning position of mylocation will not be affected

The only case i can think of is when each one is in a new line.

You must specify the maximum tolerted length after which this order is no longer guaranteed, the formating actually should occur on reading the file, at that point you can determine the longest variable of myname and format your output according to it.

CloudyMarble
  • 34,949
  • 69
  • 92
  • 126
1

try this

bw.write(myname + "                             ".substring(0, 30) + " " + mylocation);
Evgeniy Dorofeev
  • 124,221
  • 27
  • 187
  • 258
1

Using Google Guava:

Strings.padEnd("JOHN DOE MAXWELL", 26, ' ').length()

String will be always 26 characters length.

Sergii Shevchyk
  • 35,850
  • 12
  • 46
  • 59