FileWriter
FileWriter's job here is to create a character-based file. The output is wrapped (buffered) in BufferedWriter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.io.*; public class WritingTextFile1 { public static void main(String[] args){ String st1="Java is a general-purpose computer programming language."; String st2="Java was originally developed by James Gosling."; FileWriter fw=null; try{ fw=new FileWriter("textfile.txt"); }catch(IOException e){ } BufferedWriter bw=new BufferedWriter(fw); try{ bw.write(st1,0,st1.length()); bw.newLine(); bw.write(st2,0,st2.length()); }catch(IOException e){ } try{ bw.close(); }catch(IOException e){ } } } |
bw.write(st1,0,st1.length());
String st1 is written to the text file. The second argument is the starting point and the third or the last argument is the numbers of character written. In other words, this code write all the characters of the string.
bw.newLine();
This inserts a newline to the text.
bw.write(st2,0,st2.length());
The second string st2 is written to the text file.
Adding new content to an existing file
To prevent the existing content from being replaced by new content, the text file is opened in append mode. This is accomplished by passing true to the second argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.io.*; public class WritingTextFile1b { public static void main(String[] args){ String st="Sun relicensed most of its Java technologies under the GNU General Public License."; FileWriter fw=null; try{ fw=new FileWriter("textfile.txt",true); }catch(IOException e){ } BufferedWriter bw=new BufferedWriter(fw); try{ bw.newLine(); bw.write(st,0,st.length()); }catch(IOException e){ } try{ bw.close(); }catch(IOException e){ } } } |
RandomAccessFile
This example shows how to write text to a file using RandomAccessFile, a class which can perform read and write operations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.io.*; public class WritingTextFile2 { public static void main(String[] args){ String st1="Java is concurrent, class-based, and object-oriented."; String st2="The language derives much of its syntax from C and C++."; String st3="The code can run on all platforms without recompilation."; RandomAccessFile raf=null; try{ raf=new RandomAccessFile("textfile.txt","rw"); }catch(IOException e){ } try{ raf.seek(0); raf.writeBytes(st1); //writeBytes(String s) raf.writeBytes("\r\n"); //newline raf.writeBytes(st2); raf.writeBytes("\r\n"); //newline raf.writeBytes(st3); }catch(IOException e){ } try{ raf.close(); }catch(IOException e){ } } } |
raf=new RandomAccessFile("textfile.txt","rw");
The text file is opened in "rw" (read write) mode.
raf.seek(0);
Set the file pointer in the beginning of the text file.
raf.writeBytes(st1);
String st1 is written to the the text file.
raf.writeBytes("\r\n");
Insert newline. And so on...
Adding new content to an existing file
To add new data, move the pointer to the end of the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.io.*; public class WritingTextFile2b { public static void main(String[] args){ String st="The latest version is Java 8, which is supported for free by Oracle."; RandomAccessFile raf=null; try{ raf=new RandomAccessFile("textfile.txt","rw"); }catch(IOException e){ } try{ raf.seek(raf.length()); raf.writeBytes("\r\n"); //newline raf.writeBytes(st); //writeBytes(String s) }catch(IOException e){ } try{ raf.close(); }catch(IOException e){ } } } |
raf.seek(raf.length());
Move the file pointer to the end of the file.
No comments:
Post a Comment