目录
程序清单17_5TestObjectOutputStream.java
程序清单17_6TestObjectInputStream.java
程序清单17_7TestObjectStreamForArray.java
程序清单17_8TestRandomAccessFile.java
程序清单17_1TextFilestream.java
package chapter_17;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class 程序清单17_1TextFilestream {
public static void main(String[] args)throws IOException{
try(
FileOutputStream output = new FileOutputStream("src/Text/temp.dat");
){
for(int i = 1; i<= 10;i++) {
output.write(i);
}
try(
FileInputStream input = new FileInputStream("src/Text/temp.dat")
){
int value;
while((value = input.read()) != -1)
System.out.print(value+" ");
}
}
}
}
输出结果
1 2 3 4 5 6 7 8 9 10
程序清单17_2TextDataStream.java
package chapter_17;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class 程序清单17_2TextDataStream {
public static void main(String[] args)throws IOException {
try(
DataOutputStream output =
new DataOutputStream(new FileOutputStream("src/Text/temp.dat"));
){
output.writeUTF("John");
output.writeDouble(85.5);
output.writeUTF("Jim");
output.writeDouble(185.5);
output.writeUTF("George");
output.writeDouble(105.25);
}
try(
DataInputStream input =
new DataInputStream(new FileInputStream("src/Text/temp.dat"));
){
System.out.println(input.readUTF()+" "+input.readDouble());
System.out.println(input.readUTF()+" "+input.readDouble());
System.out.println(input.readUTF()+" "+input.readDouble());
}
}
}
输出结果
John 85.5
Jim 185.5
George 105.25
程序清单17_3DetectEndOfFile.java
package chapter_17;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class 程序清单17_3DetectEndOfFile {
public static void main(String[] args) {
try {
try(
DataOutputStream output =
new DataOutputStream(new FileOutputStream("test.dat"));
){
output.writeDouble(4.5);
output.writeDouble(43.25);
output.writeDouble(3.2);
}
try(
DataInputStream input =
new DataInputStream(new FileInputStream("test.dat"));
){
while(true)
System.out.println(input.readDouble());
}
} catch (EOFException ex) {
System.out.println("All data were read");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
输出结果
4.5
43.25
3.2
All data were read
程序清单17_4Copy.java
package chapter_17;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class 程序清单17_4Copy {
public static void main(String[] args) throws IOException{
if(args.length !=2 ) {
System.out.println("Usage:java Copy sourceFile targetfile");
System.exit(1);
}
File sourceFile = new File(args[0]);
if(sourceFile.exists()==false) {//这里相对原文有修改
//if(!sourceFile.exists()){//原文 两个都可以使用
System.err.println("Source file "+args[0]+
" does not exist");
System.exit(2);
}
File targetFile = new File(args[1]);
if(targetFile.exists()) {
System.err.println("Target file "+args[1]+
" already exist");
System.exit(2);
}
try(
BufferedInputStream input =
new BufferedInputStream(new FileInputStream(sourceFile));
BufferedOutputStream output =
new BufferedOutputStream(new FileOutputStream(targetFile));
){
int r ,numberOfBytesCopied = 0;
while((r = input.read()) != -1) {
output.write((byte)r);
numberOfBytesCopied++;
}
System.out.println(numberOfBytesCopied+" bytes copied");
}
}
}
结果
程序清单17_5TestObjectOutputStream.java
package chapter_17;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class 程序清单17_5TestObjectOutputStream {
public static void main(String[] args) throws IOException{
try(
ObjectOutputStream output =
new ObjectOutputStream(new FileOutputStream("src/Text/object.dat"));
){
output.writeUTF("John");
output.writeDouble(85.5);
output.writeObject(new java.util.Date());
}
}
}
程序清单17_6TestObjectInputStream.java
package chapter_17;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class 程序清单17_6TestObjectInputStream {
public static void main(String[] args)throws ClassNotFoundException,IOException{
try(
ObjectInputStream input =
new ObjectInputStream(new FileInputStream("src/Text/object.dat"));
){
String name = input.readUTF();
double score = input.readDouble();
java.util.Date date = (java.util.Date)(input.readObject());
System.out.println(name + " " + score + " " + date);
}
}
}
输出结果
John 85.5 Fri Aug 23 10:14:41 CST 2024
程序清单17_7TestObjectStreamForArray.java
package chapter_17;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class 程序清单17_7TestObjectStreamForArray {
public static void main(String[] args) throws ClassNotFoundException ,IOException{
int[] numbers = {1, 2, 3, 4, 5};
String[] strings = {"John", "Susan", "Kim"};
try(
ObjectOutputStream output = new ObjectOutputStream(new
FileOutputStream("src/Text/array.dat",true))
){
output.writeObject(numbers);
output.writeObject(strings);
}
try(
ObjectInputStream input = new ObjectInputStream(new
FileInputStream("src/Text/array.dat"))
){
int[] newNumbers = (int[])(input.readObject());
String[] newStrings = (String[])(input.readObject());
for(int i = 0;i < newNumbers.length;i++)
System.out.print(numbers[i]+" ");
System.out.println();
for(int i = 0;i < newStrings.length;i++)
System.out.print(newStrings[i]+" ");
}
}
}
输出结果
1 2 3 4 5
John Susan Kim
程序清单17_8TestRandomAccessFile.java
package chapter_17;
import java.io.IOException;
import java.io.RandomAccessFile;
public class 程序清单17_8TestRandomAccessFile {
public static void main(String[] args) throws IOException{
try(
RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw")
){
inout.setLength(0);
for(int i = 0;i < 200;i++)
inout.writeInt(i);
System.out.println("Current file length is "+inout.length());
inout.seek(0);
System.out.println("The first number is "+inout.readInt());
inout.seek(1*4);
System.out.println("The second number is "+inout.readInt());
inout.seek(9*4);
System.out.println("The tenth number is "+inout.readInt());
inout.writeInt(555);
inout.seek(inout.length()); //Move the file pointer to the end
inout.writeInt(999);
System.out.println("The new length is "+inout.length());
inout.seek(10*4);
System.out.println("The eleventh number is "+inout.readInt());
}
}
}
输出结果
Current file length is 800
The first number is 0
The second number is 1
The tenth number is 9
The new length is 804
The eleventh number is 555