Sunday, April 05, 2009

Writing a file in Java

This code first creates a file (MyFile.txt) and add the data using DataOutputStream. DataOutputStream have special methods to write different type of data like writeInt() uses for integer, writeChars() uses for string.

import java.io.*;
public class FileOutput {
public static void main(String[] args) {
FileOutputStream fos;
DataOutputStream dos;
try {
File file= new File("C:\\MyFile.txt");
fos = new FileOutputStream(file);
dos=new DataOutputStream(fos);
dos.writeInt(2333);
dos.writeChars("Hello");
} catch (IOException e) {
e.printStackTrace();
}
}
}

source: java-tips.org

No comments:

Computers Add to Technorati Favorites Programming Blogs - BlogCatalog Blog Directory