Java/File Input Output/Buffer Stream
Содержание
Reading from a Binary File with BufferedInputStream
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.NumberFormat;
public class ReadBinaryFile {
public static void main(String[] args) throws Exception{
NumberFormat cf = NumberFormat.getCurrencyInstance();
File file = new File("product.dat");
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
boolean eof = false;
while (!eof) {
Product movie = readMovie(in);
if (movie == null)
eof = true;
else {
String msg = Integer.toString(movie.year);
msg += ": " + movie.title;
msg += " (" + cf.format(movie.price) + ")";
System.out.println(msg);
}
}
in.close();
}
private static Product readMovie(DataInputStream in) {
String title = "";
int year = 0;
double price = 0.0;
try {
title = in.readUTF();
year = in.readInt();
price = in.readDouble();
} catch (EOFException e) {
return null;
} catch (IOException e) {
System.out.println("I/O Error");
System.exit(0);
}
return new Product(title, year, price);
}
}
class Product {
public String title;
public int year;
public double price;
public Product(String title, int year, double price) {
this.title = title;
this.year = year;
this.price = price;
}
}
Use buffered input.
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
class BufferedInputStreamDemo {
public static void main(String args[]) throws IOException {
String s = "This is a © copyright symbol " + "but this is © not.\n";
byte buf[] = s.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(buf);
BufferedInputStream f = new BufferedInputStream(in);
int c;
boolean marked = false;
while ((c = f.read()) != -1) {
switch (c) {
case "&":
if (!marked) {
f.mark(32);
marked = true;
} else {
marked = false;
}
break;
case ";":
if (marked) {
marked = false;
System.out.print("(c)");
} else
System.out.print((char) c);
break;
case " ":
if (marked) {
marked = false;
f.reset();
System.out.print("&");
} else
System.out.print((char) c);
break;
default:
if (!marked)
System.out.print((char) c);
break;
}
}
}
}
Use BufferedInputStream and BufferedOutputStream to copy byte array
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Main {
public static void main(String[] args) throws Exception {
String fromFileName = "from.txt";
String toFileName = "to.txt";
BufferedInputStream in = new BufferedInputStream(new FileInputStream(fromFileName));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(toFileName));
byte[] buff = new byte[32 * 1024];
int len;
while ((len = in.read(buff)) > 0)
out.write(buff, 0, len);
in.close();
out.close();
}
}
Writing to a Binary File
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
public class WriteBinaryFile {
public static void main(String[] args) throws Exception {
Product[] movies = new Product[10];
movies[0] = new Product("W", 1946, 14.95);
movies[1] = new Product("T", 1965, 12.95);
movies[2] = new Product("Y", 1974, 16.95);
movies[3] = new Product("R", 1975, 11.95);
movies[4] = new Product("S", 1977, 17.95);
movies[5] = new Product("P", 1987, 16.95);
movies[6] = new Product("G", 1989, 14.95);
movies[7] = new Product("A", 1995, 19.95);
movies[8] = new Product("Game", 1997, 14.95);
movies[9] = new Product("F", 2001, 19.95);
DataOutputStream out = openOutputStream("movies.dat");
for (Product m : movies)
writeMovie(m, out);
out.close();
}
private static DataOutputStream openOutputStream(String name) throws Exception {
DataOutputStream out = null;
File file = new File(name);
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
return out;
}
private static void writeMovie(Product m, DataOutputStream out) throws Exception {
out.writeUTF(m.title);
out.writeInt(m.year);
out.writeDouble(m.price);
}
}
class Product {
public String title;
public int year;
public double price;
public Product(String title, int year, double price) {
this.title = title;
this.year = year;
this.price = price;
}
}