-4
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new ByteArrayOutputStream(), encoding)); 

public void write(final List<Column> columnsList) throws IOException {
        String data = null;
        int length = columnsList.size();
        int writeIndex = 0;
        for (Column cd : columnsList) {
            data = cd.getData();            
            writer.write(data);
            writer.write(getDelimiter()); // it will return '\t'
        }
        writer.write("\n");
    }

The above code is not placing the tab space in file, and in actual it writes \t in file. When I write the file with hard coded "\t", it really does write tab space. But I am receiving the "\t" delimiter from web-end. I am just picking this delimiter and writing with writer. But when I open the file in notepad, "\t" is actually written in the file instead of TAB SPACE

I am closing the writer in other method., so no worry about closing the writer

Muhammad Ijaz
  • 181
  • 1
  • 4
  • 15
  • 2
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Apr 16 '13 at 09:30
  • 2
    @AndrewThompson why ?? isn't \t a valid java escape sequence ? – PermGenError Apr 16 '13 at 09:31
  • 3
    It really won't do this. I suspect the problem is in how you're viewing the file, which you haven't told us. – Jon Skeet Apr 16 '13 at 09:32
  • How are you verifying the contents? Are you actually viewing it, or just reading it back somehow? – NilsH Apr 16 '13 at 09:33
  • No, `"\\t` would print the actual text `"\t"`. What you want to write is the tab escape character. – NilsH Apr 16 '13 at 09:33
  • Are you initialising that writer? – Quetzalcoatl Apr 16 '13 at 09:33
  • 1
    I tried with a simple code `BufferedWriter writer = new BufferedWriter(new FileWriter(new File("c:\\ak\\1.txt"))); writer.write("\t"); writer.close();` and its writing `tab` in the file. – Apurv Apr 16 '13 at 09:34
  • 1
    @PermGenError Conceded. Should have stuck with the first comment only. :P – Andrew Thompson Apr 16 '13 at 09:35
  • @JonSkeet, I am viewing this file as text file in note pad – Muhammad Ijaz Apr 16 '13 at 09:54
  • @Apurv, when I write the file with hard coded "\t", it really does write tab space. But I am receiving the "\t" delimiter from web-end. I am just picking this delimiter and writing with writer. But when I open the file in notepad, "\t" is actually written in the file instead of TAB SPACE – Muhammad Ijaz Apr 16 '13 at 10:10
  • @Pomy You need to update your question with complete details, only then you can expect accurate answer. – Apurv Apr 16 '13 at 10:26
  • @Pomy Please share more details about the `getDelimiter()` method. There are good chances that this method is returning `\\t` instead of `\t`. You can also try printing the return value of this method on console. – Apurv Apr 16 '13 at 11:37
  • @Apurv, when I print getDelimiter(), it simply prints \t. But when I debug the value it shows me "\\t" – Muhammad Ijaz Apr 16 '13 at 12:43

6 Answers6

1

You do not initialize BufferedWriter object. Here is an example of how to write file in java using bufferedwriter

codeMan
  • 5,730
  • 3
  • 27
  • 51
  • 1
    I have just shown what I was actually doing and what acutlly happening. So it is obvious that I have initialized the BufferedWriter – Muhammad Ijaz Apr 16 '13 at 09:46
1

Just tried it as well.

Working fine! Check this how to use "tab space" while writing in text file post as you may need some configuration.

Post the whole method for this for more info

FileWriter fstream = new FileWriter("bot.txt", true); //true tells to append data.
BufferedWriter out = new BufferedWriter(fstream);

out.write("bbb" + "\t"+"aaa");
out.close();`
Community
  • 1
  • 1
Nidis
  • 145
  • 1
  • 10
1

Well, I am Quite SUre the getDelimiter() will return the "\t" Which are two characters.

You can verify it by getDelimiter().toCharArray();

Simple Solution is that use like following code snippet

if (getDelimiter().equalsIgnoreCase("\\t")) then use String.valueOf('\t')
Imran
  • 5,376
  • 2
  • 26
  • 45
0

Check this issue, maybe you are missing something before initializing the BufferedWriter

how to use "tab space" while writing in text file

Community
  • 1
  • 1
cyrat
  • 628
  • 1
  • 11
  • 20
0

Your writer definition is not complete, so we do not know which underlying stream/writer you use.

This example works for me:

BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(System.out));
writer.write("a");
writer.write("\t");
writer.write("b");
writer.close();

It prints a tab character between "a" and "b".

0xCAFEBABE
  • 5,576
  • 5
  • 34
  • 59
-3

You should call writer.close();

you do it?