![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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
__________________
|
|
|
|
|
|
#2 |
|
Expert Programmer
|
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. |
|
|
|
|
|
#3 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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 |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Apr 2006
Posts: 136
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#5 |
|
Expert Programmer
|
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();
}
} |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Apr 2006
Posts: 136
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#7 | |
|
Expert Programmer
|
Quote:
__________________
|
|
|
|
|
|
|
#8 | |
|
Expert Programmer
|
Quote:
![]() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|