Tuesday, April 8, 2014

Code snippet to write a file from Java

public static void main(String[] args) throws IOException {
OutputStreamWriter osw = null;
try{
//Example to write a file into file system
//Charset windows1252 = Charset.forName("windows-1252");
String filePath="D:\\temp\\";

filePath = filePath.concat(String.valueOf(new Date().getTime())).concat(".txt");
FileOutputStream fos = new FileOutputStream(filePath,false);
osw = new OutputStreamWriter(fos,"UTF-8");
osw.write("Sample");
osw.write("\uFEFF");
osw.write("File");
//osw.write(Charset.forName("UTF-8").encode("Sample"));
osw.close();
System.out.println("Success");
fos.close();

}
catch(Exception e)
{
System.out.println(e.getMessage());
osw.close();
}
}

No comments:

Post a Comment