Archive:Java File IO: Difference between revisions
New page: File Input/Output is similar to the System.out and System.in that you already have begun using. The only difference is that you read from a file. =Files= Files are another type of variable... |
wikademia>Wikademia m moved Java File IO to Archive:Java File IO |
||
| (11 intermediate revisions by 8 users not shown) | |||
| Line 1: | Line 1: | ||
File | File input/output is quite similar to the System.in and System.out that you already have begun using In Java, files can be represented by the class java.io.File. | ||
=Files= | |||
== Files == | |||
Files are another type of variable. You can make a file like this: | Files are another type of variable. You can make a file like this: | ||
<pre>File f = new File("test.txt")</pre> | <pre>File f = new File("test.txt")</pre> | ||
This does not return an exception if the file doesn't exist. To check if it does: | |||
<pre>f.exists()</pre> | |||
This will return a boolean. | |||
{{ntnes}} | |||
To check if it is a file or directory: | |||
<pre>f.isFile() | |||
f.isDirectory() | |||
</pre> | |||
For a full list of methods you can use, go to http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html | |||
== File Output == | |||
To write to a file, you can use a FileWriter or a FileOutputStream. | |||
==FileWriter== | |||
This class writes strings or char[] to a file. To create one: | |||
<pre> | |||
FileWriter fw = new FileWriter("test.txt");//Create a filewriter writing to test.txt | |||
FileWriter fw = new FileWriter("test.txt", true);//Do not delete contents of file - append | |||
</pre> | |||
It is as simple to write. After you create a FileWriter just use: | |||
<pre> | |||
String s = "Testing"; | |||
//INITIALIZE FileWriter fw | |||
fw.write(s); | |||
</pre> | |||
FileWriter can also write in other formats. Using char[]: | |||
<pre> | |||
Char[] charAr=s.toCharArray();//A method in class String | |||
fw.write(charAr); | |||
</pre> | |||
Of course, you should only use this when you already have a character array. | |||
=== FileOutputStream === | |||
== File Input == | |||
=== Scanner === | |||
Scanner is a class that was made when it was decided by Sun that the java file IO API was too messy. Scanners make file IO ridiculously easy to say the least. To use Scanner, import the class from java.util: | |||
<pre> | |||
import java.util.Scanner; | |||
</pre> | |||
The scanner is a sort of wrapper class for a FileInputStream. The constructor of Scanner takes a FileInputStream object as a parameter, like so: | |||
<pre> | |||
Scanner ourScanner = new Scanner(new FileInputStream("ourfile.txt")); | |||
</pre> | |||
Scanner includes a number of member methods that make file IO easy. You can use a scanner to get the next item of a certain type from a stream with the methods next(), nextDouble, nextFloat, nextInt, etc. You can also determine if more of a certain type exist with the has*() methods and read and search for regexes. | |||
Some example code: | |||
<pre> | |||
FileInputStream istream = new FileInputStream(args[0]); | |||
Scanner ourScanner = new Scanner(istream); | |||
while(ourScanner.hasNext()) | |||
{ | |||
Double tmp = ourScanner.nextDouble(); | |||
System.out.println(tmp+"\n"); | |||
} | |||
</pre> | |||
[[Category:Java]] | |||
Latest revision as of 09:13, 12 June 2009
File input/output is quite similar to the System.in and System.out that you already have begun using In Java, files can be represented by the class java.io.File.
Files
Files are another type of variable. You can make a file like this:
File f = new File("test.txt")
This does not return an exception if the file doesn't exist. To check if it does:
f.exists()
This will return a boolean.
To check if it is a file or directory:
f.isFile() f.isDirectory()
For a full list of methods you can use, go to http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html
File Output
To write to a file, you can use a FileWriter or a FileOutputStream.
FileWriter
This class writes strings or char[] to a file. To create one:
FileWriter fw = new FileWriter("test.txt");//Create a filewriter writing to test.txt
FileWriter fw = new FileWriter("test.txt", true);//Do not delete contents of file - append
It is as simple to write. After you create a FileWriter just use:
String s = "Testing"; //INITIALIZE FileWriter fw fw.write(s);
FileWriter can also write in other formats. Using char[]:
Char[] charAr=s.toCharArray();//A method in class String fw.write(charAr);
Of course, you should only use this when you already have a character array.
FileOutputStream
File Input
Scanner
Scanner is a class that was made when it was decided by Sun that the java file IO API was too messy. Scanners make file IO ridiculously easy to say the least. To use Scanner, import the class from java.util:
import java.util.Scanner;
The scanner is a sort of wrapper class for a FileInputStream. The constructor of Scanner takes a FileInputStream object as a parameter, like so:
Scanner ourScanner = new Scanner(new FileInputStream("ourfile.txt"));
Scanner includes a number of member methods that make file IO easy. You can use a scanner to get the next item of a certain type from a stream with the methods next(), nextDouble, nextFloat, nextInt, etc. You can also determine if more of a certain type exist with the has*() methods and read and search for regexes.
Some example code:
FileInputStream istream = new FileInputStream(args[0]);
Scanner ourScanner = new Scanner(istream);
while(ourScanner.hasNext())
{
Double tmp = ourScanner.nextDouble();
System.out.println(tmp+"\n");
}