Write to file from ByteBuffer via channel output is whitespace in Netbeans

/*

* WriteAString.java

*

* Created on May 13, 2007, 10:17 AM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package StreamExamples;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.FileNotFoundException;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

/**

*

* @author sdwolf

*/

public class WriteAString {

/** Creates a new instance of WriteAString */

public WriteAString() {

}

public static void main(String[] args) {

String phrase = new String("Garbage in, Garbage out\n");

//String phrase1 = "Garbage in, Garbage out\n"; // test

String dirname = "C:/Beg Java Stuff";

String filename = "charData.txt";

File dir = new File(dirname);

//Now check out the directory

if(!dir.exists()) {

if(!dir.mkdir()) {

System.out.println("Cannot create directory: " + dirname);

System.exit(1);

}

} else if (!dir.isDirectory()) {

System.out.println(dirname + " is not a directory");

System.exit(1);

}

//Create the file stream

File aFile = new File(dir, filename);

FileOutputStream outputFile = null;

try {

outputFile = new FileOutputStream(aFile, true);

System.out.println("File Stream created successfully");

} catch (FileNotFoundException e) {

e.printStackTrace (System.err);

}

//Create the file output stream channel and the buffer

FileChannel outChannel = outputFile.getChannel();

ByteBuffer buf = ByteBuffer.allocate(1024);

System.out.println("New buffer: position = " + buf.position()

+ "\tLimit = " + buf.limit() + "\tCapacity = "

+ buf.capacity());

//Load the data into the buffer

for (char ch : phrase.toCharArray()) {

buf.putChar(ch);

System.out.print(ch);

}

System.out.println("Buffer after loading: position = " + buf.position()

+ "\tLimit = " + buf.limit() + "\tCapacity = "

+ buf.capacity());

//Write the file

try {

outChannel.write(buf); //Write the buffer to a file channel

outputFile.close();//Close the O/P file and channel

System.out.println("Buffer contents written to file");

} catch (IOException e) {

e.printStackTrace (System.err);

}

}

}

[2509 byte] By [inabsentiaa] at [2007-11-27 4:20:46]
# 1
You forgot to post the question.
jsalonena at 2007-7-12 9:27:52 > top of Java-index,Java Essentials,New To Java...
# 2

Yes....I guess I did. Sorry. This code creates the directory and file just fine. Each time I run it the file updates by approximately 1KB. When I open the file it is all whitespace. Tried Notepad, Word and Edit Pad Liite...all the same. I ran this program from NetBeans. What is going wrong here? Thank you....

inabsentiaa at 2007-7-12 9:27:52 > top of Java-index,Java Essentials,New To Java...
# 3

You have to flip() the buffer after putting stuff in it.

http://java.sun.com/developer/technicalArticles/releases/nio/

Edit: yup, that's it.. A buffer is kind of like a magnetic tape: when you record ("put") the write head moves on the tape, and you have to rewind ("flip") to play back ("get") what you have recorded.//Load the data into the buffer

for (char ch : phrase.toCharArray()) {

buf.putChar(ch);

System.out.print(ch);

}

buf.flip(); // add this

System.out.println("Buffer after loading: position = " + buf.position()

+ "\tLimit = " + buf.limit() + "\tCapacity = "

+ buf.capacity());

actually flip() does more than just rewind the tape, it also sets a mark on where you were left

jsalonena at 2007-7-12 9:27:52 > top of Java-index,Java Essentials,New To Java...
# 4
thanks jsalonen. I tried that and it worked. I will take a look at the link you supplied...thx again.
inabsentiaa at 2007-7-12 9:27:52 > top of Java-index,Java Essentials,New To Java...
# 5
You're welcome!
jsalonena at 2007-7-12 9:27:52 > top of Java-index,Java Essentials,New To Java...