Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 27th, 2006, 1:23 AM   #1
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 595
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
How do I read a file?

I was wondering exactly what I need to do (import... classes... methods) to read text from an external file. I honestly have no idea where to start so any help is great Thanks
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old May 27th, 2006, 1:27 AM   #2
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
http://www.programmingforums.org/for...ad.php?t=10008

It was 2 posts down man. If you really have no idea, at least make sure you kow the basics of Java first.
Booooze is offline   Reply With Quote
Old May 27th, 2006, 1:27 AM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Here is an example (uncompiled, so there might be syntactical errors)

import java.io.*;
import java.util.*;

public static void main(String [] args) throws IOException
{
	 Scanner myFile = new Scanner(new File("filename.txt")) // creates a Scanner object to read from file filename.txt
	 /* use the appropriate Scanner methods here to read from your file*/
}

Scanner methods can be found here: http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old May 27th, 2006, 1:27 AM   #4
Toro
Hobbyist Programmer
 
Toro's Avatar
 
Join Date: Apr 2006
Posts: 136
Rep Power: 0 Toro is an unknown quantity at this point
you would import java.io and javax.swing packages. File is the class that contains information about a file. JFileChooser is a dialog where you can open and save files.

[PHP]
//getting files graphically
JFileChooser chooser = new JFileChooser();
File file;
int result = chooser.showOpenDialog(null);
if( result == JFileChooser.APPROVE_OPTION)
file = chooser.getSelectedFile();
[/PHP]
This is how would you get a File using the "JFileChooser" class, located in the "javax.swing.JFileChooser". "File" is located in "java.io.File"

EDIT: "Core Java 2: Fundamentals, 7th Edition" by Cay Horstman has a great explaination on I/O classes. I recommend you check that out.
Toro is offline   Reply With Quote
Old May 27th, 2006, 1:30 AM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 904
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Here is some example code to get you started. I believe this is the preferred way to read/write files in Java. This code reads the first line from file.in and writes it to file.out.

import java.io.*;

public class IOTest {
  public static void main (String [] args) throws IOException {
    
    RandomAccessFile f = new RandomAccessFile("file.in", "r");
    String firstLine = f.readLine();
    f.close();
    
    PrintWriter out = new PrintWriter(
            new BufferedWriter(
            new FileWriter("file.out")));
    out.println(firstLine);
    out.close();
    
  }
}
titaniumdecoy is offline   Reply With Quote
Old May 27th, 2006, 1:32 AM   #6
Toro
Hobbyist Programmer
 
Toro's Avatar
 
Join Date: Apr 2006
Posts: 136
Rep Power: 0 Toro is an unknown quantity at this point
YEAH! I forgot to mention that when using I/O classes, most of them throw exceptions like IOExpetion, FileNotFoundException, and etc. A good reference or concept of Exception Handling in Java is a must for using the I/O Classes.

Props at titaniumdecoy for that good reminder, lol.
Toro is offline   Reply With Quote
Old May 27th, 2006, 1:35 AM   #7
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 595
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Quote:
Originally Posted by Booooze
http://www.programmingforums.org/for...ad.php?t=10008

It was 2 posts down man. If you really have no idea, at least make sure you kow the basics of Java first.
I did know it was 2 posts down because that is what sparked my curiosity. I did not want to interupt that thread with my own questions. I have found that to be rude and people don't appreciate it. Thanks for the help
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old May 27th, 2006, 2:31 AM   #8
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
Quote:
Originally Posted by crawforddavid2006
I did know it was 2 posts down because that is what sparked my curiosity. I did not want to interupt that thread with my own questions. I have found that to be rude and people don't appreciate it. Thanks for the help
Agreed. Don't interupt with your own questions, but it seemed like you had the same question. Owell, you have plenty of resources now
Booooze is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC