| 1.124 Lecture 14 | 10/26/2000 |
Reader r = new InputStreamReader(System.in); // System.in is an example of an InputStream.
Likewise, it is possible to create a 16-bit Writer from an 8-bit OutputStream using the OutputStreamWriter class e.g.
Writer w = new OutputStreamWriter(System.out);
// System.out is an example of an OutputStream.
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try {
readText(args[0]);
}
catch (IOException e)
{
e.printStackTrace();
}
}
// This function will read data from an ASCII
text file.
public static void readText(String fileName)
throws IOException {
// First create a FileReader.
A Reader is a 16-bit input stream,
// which is intended
for all forms of character (text) input.
Reader reader = new FileReader(fileName);
// Now create a BufferedReader
from the Reader. This allows us to
// read in an entire
line at a time.
BufferedReader bufferedReader
= new BufferedReader(reader);
String nextLine;
while ((nextLine = bufferedReader.readLine())
!= null) {
// Next, we create a StringTokenizer from the line we have just
// read in. This permits the extraction of nonspace characters.
StringTokenizer tokenizer = new StringTokenizer(nextLine);
// We can now extract various data types as follows.
String companyName = tokenizer.nextToken();
int numberShares = Integer.parseInt(tokenizer.nextToken());
double sharePrice = Double.parseDouble(tokenizer.nextToken());
// Print the data out on the screen.
System.out.print(companyName + " has " + numberShares);
System.out.println(" million shares valued at $" + sharePrice);
// Close the file.
bufferedReader.close();
}
}
}
This program can be easily converted to read in data from the keyboard. Simply replace
Reader reader = new FileReader(fileName);
with
Reader = new InputStreamReader(System.in);
import java.io.*;
import java.util.*;
import java.text.*;
public class Main {
public static void main(String[] args) {
try {
writeText(args[0]);
}
catch (IOException e)
{
e.printStackTrace();
}
}
// This function will write data to an ASCII text
file.
public static void writeText(String fileName)
throws IOException {
// First create a FileWriter.
A Writer is a 16-bit output stream,
// which is intended
for all forms of character (text) output.
Writer writer = new FileWriter(fileName);
// Next create a PrintWriter
from the Writer. This allows us to
// print out other data
types besides characters and Strings.
PrintWriter printWriter
= new PrintWriter(writer);
// Now print out various
data types.
boolean b = true;
int i = 20;
double d = 1.124;
String str = "This is
some text.";
printWriter.print(b);
printWriter.print(i);
printWriter.print(d);
printWriter.println("\n"
+ str);
// This is an example
of formatted output. In the format string,
// 0 and # represent
digits. # means that the digit should not
// be displayed if it
is 0.
DecimalFormat df = new
DecimalFormat("#.000");
printWriter.println(df.format(200.0));
// 200.000
printWriter.println(df.format(0.123));
// .123
// This will flush the
PrintWriter's internal buffer, causing the
// data to be actually
written to file.
printWriter.flush();
// Finally, close the
file.
printWriter.close();
}
}
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
writeBinary(args[0]);
readBinary(args[0]);
}
catch (IOException e)
{
e.printStackTrace();
}
}
// This function will write binary data to a file.
public static void writeBinary(String fileName)
throws IOException {
// First create a FileOutputStream.
OutputStream outputStream
= new FileOutputStream(fileName);
// Now layer a DataOutputStream
on top of it.
DataOutputStream dataOutputStream
= new DataOutputStream(outputStream);
// Now write out some
data in binary format. Strings are written out
// in UTF format, which
is a bridge between ASCII and Unicode.
int i = 5;
double d = 1.124;
char c = 'z';
String str = "Some text";
dataOutputStream.writeInt(i);
// Increases file size by 4 bytes.
dataOutputStream.writeDouble(d);
// Increases file size by 8 bytes.
dataOutputStream.writeChar(c);
// Increases file size by 2 bytes.
dataOutputStream.writeUTF(str);
// Increases file size by 2+9 bytes.
// Close the file.
dataOutputStream.close();
}
// This function will read binary data from a
file.
public static void readBinary(String fileName)
throws IOException {
// First create a FileInputStream.
InputStream inputStream
= new FileInputStream(fileName);
// Now layer a DataInputStream
on top of it.
DataInputStream dataInputStream
= new DataInputStream(inputStream);
// Now read in data from
the binary file.
int i;
double d;
char c;
String str;
i = dataInputStream.readInt();
d = dataInputStream.readDouble();
c = dataInputStream.readChar();
str = dataInputStream.readUTF();
System.out.print("integer
" + i + " double " + d);
System.out.println("
char " + c + " String " + str);
// Close the file.
dataInputStream.close();
}
}