CSE/IT 213 - File I/O: New Mexico Tech
CSE/IT 213 - File I/O: New Mexico Tech
/**
* holds the first coordinate of a point (x,y)
*/
private double x;
InputStream
An abstract class. One method it defines is
public abstract int read() throws IOException
Classes that extend InputStream implement this method.
Return -1 if end of input is reached.
For instance, FileInputStream extends InputStream and
implements the read method. Reading one byte at time
from a file.
System.in is a field in System defined as public static
final InputStream in. This provides input from the
stdin.
3
OutputStream
Like InputStream, OutputStream is an abstract class.
One method it expects it subclasses to implement is
abstract void write(int b), which writes one byte at a
time.
fos.write(byteArray[2]);
fos.close();
FileInputStream fis = new FileInputStream(file);
int filesize = (int) file.length();
byte[] newByteArray = new byte[filesize];
fis.read(newByteArray);
for (int i = 0; i < filesize; i++)
System.out.print(newByteArray[i] + " ");
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
1 2 3 5 6 7 8 9 10 3
The file created here is a binary file.
$ less test.txt
"test.txt" may be a binary file. See it anyway?
tukey:Junk scott$ hexdump test.txt
0000000 01 02 03 05 06 07 08 09 0a 03
000000a
fos.close();
FileInputStream fis = new FileInputStream(file);
int filesize = (int) file.length();
byte[] newByteArray = new byte[filesize];
fis.read(newByteArray);
for (int i = 0; i < filesize; i++) {
System.out.print((char) newByteArray[i] + " ");
System.out.print(newByteArray[i] + " ");
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
A 65 B 66 C 67 a 97 b 98 c 99 C 67
This produces a text file
$ file test.txt
test.txt: ASCII text, with no line terminators
tukey:Junk scott$ hexdump test.txt
0000000 41 42 43 61 62 63 43
0000007
Wont get very far with just inputting and writing bytes....
However, wont be able to use just a single class for
I/O.
Java divides I/O classes based on functionality.
Some classes can read bytes from files, like FileInputStream
and FileOutputStream. Other classes cant directly access files, but can organize bytes into data types such as
DataOutputStream and DataInputStream. As a programmer it is your responsibility to combine the two.
import java.io.*;
public class Junk {
private static int i;
public static void main(String[] args) {
try {
File file = new File("spoonful.mp3");
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
int i = dis.readInt();
System.out.println(Integer.toBinaryString(i));
9
System.out.println(i);
dis.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
1001001010001000011001100000010
1229206274
b1
cf
51
cd
23
80
8a
37
66
35
cc
5c
fa
6c
d6
da
30
c2
8c
6c
3f
3f
3f
3f
3f
e3
e6
eb
ea
e1
6e
c3
a4
a5
04
17
f9
91
50
4a
0e
a8
22
00
f8
97
58
0b
23
08
e9
89
13
dd
19
4f
f3
93
b6
8d
0.6502569643838811
0.6071887288119714
0.21320412286579105
0.7114227569584827
0.7668463312437199
0.8638387360484664
0.8381051074715216
0.8326797487961162
0.2954540580607048
11
Text Files
FileOutputStream and DataOutputStream create binary files.
Binary files are files that are encoded in binary format,
a sequence of binary digits
Text file are easier for humans to read and edit.
To write out ASCII, use PrintWriter which only has two
(overloaded) methods print and println.
PrintWriter wraps File and FileWriter.
Use FileWriter as it operates on character data, rather
than bytes as FileOutputStream does.
12
import java.io.*;
public class Junk {
public static void main(String[] args) {
int i;
try {
File file = new File("test.txt");
FileWriter fos = new FileWriter(file);
PrintWriter pw = new PrintWriter(fos);
pw.println("Four score and seven years ago");
for (i = 0; i < 10; i++)
pw.print(i + " ");
pw.println();
13
$ less test.txt
Four score and seven years ago
0 1 2 3 4 5 6 7 8 9
to be or not to be...
14
e.printStackTrace();
}
BufferedReader bufRead = new BufferedReader(fr);
String str = null;
ArrayList<Integer> volume = new ArrayList<Integer>(1000);
ArrayList<Float> open = new ArrayList<Float>(1000);
ArrayList<Float> close = new ArrayList<Float>(1000);
ArrayList<Float> min = new ArrayList<Float>(1000);
ArrayList<Float> max = new ArrayList<Float>(1000);
try {
bufRead.readLine(); //skip over first line
while(true) {
if(!bufRead.ready())
break;
str = bufRead.readLine();
//create vectors for open close min max
String data[] = str.split(";");
volume.add(Integer.parseInt(data[2]));
open.add(Float.parseFloat(data[3]));
close.add(Float.parseFloat(data[4]));
min.add(Float.parseFloat(data[5]));
max.add(Float.parseFloat(data[6]));
}
} catch (IOException e) {
=
=
=
=
=
=
=
=
0.724025
0.72425
0.724175
0.72407496
0.724075
0.7242
0.72419995
0.724125
Buffered Input
BufferedReader, BufferedWriter, BufferedInputStream, and
BufferedOutputStream classes add a data buffer to the
stream. Buffers increase the efficiency of reads and
writes. Reading or writing to a hard disk is an expensive operation.
Without buffering, calls to read() or readLine() cause
bytes to be read from a file, converted into characters,
and then returned, each and every time the call is made.
This can be very inefficient. Calls that use a buffer
will read more data than they need into a buffer, and
read() or readline() will read from the buffer not the
underlying file. The buffer handles the refilling of data
as necessary.
17
Writing Objects
One way to store objects is to write them to a file.
To dump objects to a file use ObjectOutputStream
To read objects from a file use ObjectInputStream
To perform Object I/O you need to implement the
Serializable interface, an interface which declares no
methods. So no need to do anything for your class
except say YourClass implements Serializable.
Object serialization converts an object to a stream of
bytes so the state of an object can be saved in a file
and later retrieved.
18
import java.io.*;
public class PointTest
{
(3.4804772126529935, 9.022468366930763)
(1.115180624647354, 6.661355545294354)
(8.31500257018606, 4.511527449160546)
(7.535963930182138, 3.433276558445134)
(0.8471097037889497, 0.6407571229156239)
(7.98692779668948, 5.196972324932716)
(9.859089403088605, 6.481831922153297)
(9.949606432837793, 5.4348905050162655)
(9.954775484312973, 4.718560591371585)
(3.1619529963846125, 2.374910118125977)
20
File Management
The class File gives you access to the file system.
public File(String pathname) creates a new File instance,
converting the string into an abstract pathname.
An abstract pathname has two components:
1) An optional system-dependent prefix string, such as
a disk-drive specifier, / for the UNIX root directory,
or drive letter : \\ for a Microsoft Windows machine
and
22
Another constructor for File will let you create a File instance from an abstract pathname and a child abstract
pathname. File(String parent, String child)
demo
doc
java
junk
junk.c
size.c
...
There are other methods such as listFiles() which just
lists the files in a directory.