Contents
- ./DataIODemo.java
- ./FileTest.java
- ./InvoiceUsingText.java
./DataIODemo.java 1/3
[top][prev][next]
package examples;
import java.io.*;
/**
* Demonstrate use of DataInput/OutputStreams with merchandise invoices.
*
*/
public class DataIODemo {
/**
*
* @param args
* - not used in this program
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// I know I don't want this variable to change within this method, so
// made it final
final String FILENAME = "invoice.dat";
// stream to write the data out
DataOutputStream out = new DataOutputStream(new FileOutputStream(
FILENAME));
double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
int[] units = { 12, 8, 13, 29, 50 };
String[] descs = { "Java T-shirt", "Java Mug", "Java Beach Towel",
"Java Pin", "Java Key Chain" };
// write the data out
for (int i = 0; i < prices.length; i++) {
out.writeDouble(prices[i]);
out.writeChar('\t');
out.writeInt(units[i]);
out.writeChar('\t');
out.writeChars(descs[i]);
out.writeChar('\n');
}
out.close();
// read the data in again
DataInputStream in = new DataInputStream(new FileInputStream(FILENAME));
double price;
int unit;
StringBuilder desc;
double total = 0.0;
final String lineSepString = System.getProperty("line.separator");
char lineSep = lineSepString.charAt(0);
while (in.available() > 0) {
price = in.readDouble();
in.readChar(); // throws out the tab
unit = in.readInt();
in.readChar(); // throws out the tab
char chr;
desc = new StringBuilder(20);
while ((chr = in.readChar()) != lineSep)
desc.append(chr);
System.out.println("You've ordered " + unit + " units of " + desc
+ " at $" + price);
total = total + unit * price;
}
in.close();
// 2 ways to do the same thing:
System.out.printf("For a TOTAL of $%.2f\n", total);
// OR
System.out.format("For a TOTAL of $%.2f", total);
}
}
./FileTest.java 2/3
[top][prev][next]
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* Demonstrate using File objects and FileInputStreams in Java.
*
* @author Sara Sprenkle
*
*/
public class FileTest {
/**
* @param args
*/
public static void main(String[] args) {
// String basedir =
// "/Users/sprenkle/Documents/WLU/CS209/fall2015/workspace/11-exceptions_streams";
String basedir = ".";
// create a file that represents the current directory
// File f = new File(basedir + File.separator + "chicken.data");
File f = new File("chicken.data");
System.out.println("File is " + f.getAbsolutePath());
try {
FileInputStream fin = new FileInputStream(f);
while (fin.available() > 0) {
System.out.println(fin.read());
}
/* Consider how we'd write the code if we don't use the available() method:
* When would the loop stop?
* The loop would keep going, waiting for more input. That could be valid code,
* but more often, you probably want to stop looping when you run out of stuff to read,
* like from a file.
*/
/*
while( true ) {
int input = fin.read();
System.out.println(input);
}
*/
fin.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File notAFile = new File("/this/is/not/a/file");
displayInfo(f);
displayInfo(notAFile);
}
/**
* Displays info about the file
* @param myFile
*/
private static void displayInfo(File myFile) {
System.out.println(myFile.getAbsolutePath() + " is a file: "
+ myFile.isFile());
System.out.println(myFile.getAbsolutePath() + " is a directory: "
+ myFile.isDirectory());
}
}
./InvoiceUsingText.java 3/3
[top][prev][next]
package examples;
import java.io.*;
import java.util.Scanner;
/**
* Demonstrate use of PrintWriter/Scanner with merchandise invoices.
*
*/
public class InvoiceUsingText {
public static void main(String[] args) throws IOException {
final String FILENAME = "invoice.dat";
writeInvoiceToFile(FILENAME);
// read it in again
Scanner in = new Scanner(new FileReader(FILENAME));
double price;
int unit;
String desc;
double total = 0.0;
while (in.hasNext()) {
price = in.nextDouble();
unit = in.nextInt();
in.skip("\t"); // eat tab
desc = in.nextLine();
System.out.println("You've ordered " + unit + " units of " + desc
+ " at $" + price);
total = total + unit * price;
}
in.close();
// 1.5+ way
System.out.printf("For a TOTAL of $%10.2f\n", total);
// OR
System.out.format("For a TOTAL of $%.2f", total);
}
/**
* @param filename
* @throws FileNotFoundException
*/
public static void writeInvoiceToFile(final String filename)
throws FileNotFoundException {
// write the data out
PrintWriter out = new PrintWriter(filename);
double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
int[] units = { 12, 8, 13, 29, 50 };
String[] descriptions = { "Java T-shirt", "Java Mug", "Java Beach Towel",
"Java Pin", "Java Key Chain" };
for (int i = 0; i < prices.length; i++) {
out.print(prices[i]);
out.print('\t');
out.print(units[i]);
out.print('\t');
out.println(descriptions[i]);
}
out.close();
}
}
Generated by GNU Enscript 1.6.6.