Hi,
I am computer science student. I recently started learning java language. When I reading java book there is "filewriter" concept I unable to understand. So if you have any knowledge about this please help me.
Hi,
I am computer science student. I recently started learning java language. When I reading java book there is "filewriter" concept I unable to understand. So if you have any knowledge about this please help me.
It is a type of class. Filewriter creates a new file and give you chance to write into that file.
Following is the summary of construction:
FileWriter(File newfile)
This creates FileWriter object.It is not useful as compared to PrintWriter class.
FileWriter class extends the OutputStreamWriter. It is one of the user friendly class for writing character in the file. This class accept character encoding. FileWriter class also accepts byte-buffer size. For this construction you have to create OutputStreamWriter for FileOutputStream. FileWriter is usually made for writing characters using streams.
Following is the example of filewriter:
Code:// Demonstrate FileWriter. import java.io.*; class FileWriter { public static void main(String args[]) throws Exception { String s = "Day after tomorrow\\n" + " all world will destroy\\n" + " and no one live"; char b[] = new char[s.length()]; source.getChars(0, s.length(), b, 0); FileWriter F0 = new FileWriter("file1.txt"); for (int p=0; p < b.length; p += 2) { F0.write(b[p]); } f0.close(); FileWriter F1 = new FileWriter("file2.txt"); F1.write(b); F1.close(); FileWriter F2 = new FileWriter("file3.txt"); F2.write(b,b.lengthbuffer. length/4,b.length/4); F2.close(); } }
Code:import java.io.FileWriter; import java.io.IOException; public class MC { public static void main(String[] a1) { FileWriter w; try { w = new FileWriter("amol.txt"); char c = 'Q'; w.write(c); w.close(); } catch (IOException e) { e.printStackTrace(); } } }
Bookmarks