+ Reply to Thread
Results 1 to 3 of 3

Thread: Tutorial, File Reader

  1. #1
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Tutorial, File Reader

    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.

    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);
    	}
    }
    Well that's it, and sorry for my poor description and English. That's all !

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Tutorial, File Reader

    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]

  4. #3
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Tutorial, File Reader

    Quote Originally Posted by chili5 View Post
    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. CAPTCHA Reader Help
    By BlaineSch in forum Java Help
    Replies: 6
    Last Post: 06-01-2010, 11:12 AM
  2. RSS Reader - Perl
    By debtboy in forum Linux Tutorials, Guides and Tips
    Replies: 2
    Last Post: 10-11-2009, 08:46 PM
  3. HTML Reader
    By xsonny in forum C# Programming
    Replies: 3
    Last Post: 07-13-2007, 05:16 AM
  4. Acrobat Reader
    By techni68 in forum Computer Software/OS
    Replies: 15
    Last Post: 01-04-2007, 05:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts