Hey C.C am back again and gonna post a simple tutorial. So today we can start with java's own file reader. We are going today to copy an existing files content to another file(empty). So let's get going.
Well that's it, and sorry for my poor description and English. That's all !Code:import java.io.*;//Here we are importing the in & out put streamer(io) import javax.swing.*;// importing the swing to get a simple user graphics public class copyfile { public static void main(String [] ang)throws IOException {// we are throwing away all exceptions String in = JOptionPane.showInputDialog( "What's the name of the file and its direction? ");//NOTE!, people that uses IDEs in windows environment might have problems locating the "path" to the file even if it it in the same folder as the java file. So if using a texteditor you just type in filename.txt and with an IDE path/filename.txt BufferedReader instrom = new BufferedReader(new FileReader(in));//the input stream reader String ut = JOptionPane.showInputDialog( "What is the name of the file you want to create and where? ");//same as before you type in where if using IDE and name, but if using a texteditor then you need only to type in the name of file. PrintWriter out = new PrintWriter(new BufferedWriter (new FileWriter(ut)));//the function to "understand" and write in the new file the text or numbers int radNR = 0;//the function of our reader while(true) {//while true it will proceed String rad = instrom.readLine();//reading every string in the file if(rad == null) {//if there is nothing it will break and print out into the new file break; } radNR++; out.println(radNR + ": " + rad);//here is the new file } out.close();//NOTE, this part is important, since you have to close the streams so you do it here. System.exit(0); } }
Nice
A few questions: What is the difference between:
[highlight=java]
PrintWriter out = new PrintWriter(new BufferedWriter (new FileWriter(ut)));
[/highlight]
and
[highlight=java]
PrintWriter out = new PrintWriter(new FileWriter ("file.txt"));
[/highlight]
What of I understand java and what I have learnt of it, PrintWriter is "function" to print writing thing,(either out or in) (I used BufferedWriter to add in everything). So I won't end up with nothing. To complete the buffering, I used newFileWriter(ut);
So that's how I used and why I used I guess, plus srry about my lame explanation.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks