• Post Reply Bookmark Topic Watch Topic
  • New Topic

Add white spaces to file

 
Mike Boota
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am reading files from a directory one by one and creating one big file out of it. While reading reading when reached at the end of the line in the source file I want to add arround 5000+ white spaces
in the target file. What is the fatest way to add thousands of white spaces to a file. Is there any utility or package I can use rather than looping.


Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create an array of char, fill it with whitespace using java.util.Arrays.fill(), then keep it around. When you need to write X characters to the file, use java.io.Writer.write(theArray, 0, X) (as long as X is less than the size of the array, of course; you weren't clear on whether X varied.
[ February 06, 2007: Message edited by: Ernest Friedman-Hill ]
 
Mike Boota
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I have the following and it works fine.

char[] spaceArray = new char[50000];
java.util.Arrays.fill(spaceArray, ' ');
String spaces = new String(spaceArray);
System.out.println("Length: "+spaces.length());
BufferedWriter out = new BufferedWriter(new FileWriter("c:\\test.txt"));
out.write("aString "+spaces+" end.");
out.close();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!