《Java语言程序设计与数据结构》编程练习答案(第十七章)
英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition
17.1
import java.io.*;
public class book {
public static void main(String[] args)
throws Exception
{
File dst = new File("Exercise17_01.txt");
FileWriter fw = new FileWriter(dst,true);
try(
PrintWriter output = new PrintWriter(fw);){
for(int i=0;i<100;i++) {
output.print((int) (Math.random() * 100)+" ");
}
output.print('\n');
}
}
}
17.2
import java.io.*;
public class book {
public static void main(String[] args)
throws Exception
{
try(FileOutputStream musashi = new FileOutputStream("Exercise17_02.dat",true);)
{
for(int i=0;i<100;i++)
musashi.write((int)(Math.random()*100));
}
}
}
17.3
import java.io.*;
public class book {
public static void main(String[] args)
throws Exception
{
try(
FileInputStream yamato = new FileInputStream("Exercise17_02.dat");
){
int v=0;
while(yamato.read()!=-1)
v++;
System.out.print(v);
}
}
}
17.4
import java.util.*;
import java.io.*;
public class book {
public static void main(String[] args)
throws Exception
{
File yukikaze = new File(args[0]);
Scanner input = new Scanner(yukikaze);
String nagato="";
while(input.hasNext())
nagato+=input.nextLine();
input.close();
try(DataOutputStream shimakaze = new DataOutputStream(new FileOutputStream(args[1]));){
shimakaze.writeChars(nagato);
}
}
}
17.5
import java.util.*;
import java.io.*;
public class book {
public static void main(String[] args)
throws Exception
{
int[] PoW = {1,2,3,4,5};
Date KGV = new Date();
double DoY = 5.5;
try(
ObjectOutputStream Bismark = new ObjectOutputStream(new FileOutputStream("Exercise17_05.dat"));
){
Bismark.writeObject(PoW);
Bismark.writeObject(KGV);
Bismark.writeDouble(DoY);
}
try(
ObjectInputStream Tilpitz = new ObjectInputStream(new FileInputStream("Exercise17_05.dat"));
){
int[] ss= (int[])(Tilpitz.readObject());
Date jj=(Date)(Tilpitz.readObject());
double bb= Tilpitz.readDouble();
for(int i=0;i<ss.length;i++)
System.out.print(ss[i]+" ");
System.out.println('\n'+jj.toString());
System.out.print(bb);
}
}
}
17.8
public class book {
public static void main(String[] args)
throws Exception
{
RandomAccessFile eagle = new RandomAccessFile("Exercise17_08.dat","rw");
eagle.writeInt(1);
eagle.close();
RandomAccessFile victory = new RandomAccessFile("Exercise17_08.dat","rw");
int swordfish = victory.readInt();
swordfish++;
victory.setLength(0);
victory.writeInt(swordfish);
}
}
17.10
import java.util.*;
import java.io.*;
public class book {
public static void main(String[] args)
throws Exception
{
System.out.print("Enter the src: ");
Scanner input = new Scanner(System.in);
String jj = input.next();
System.out.print("Enter the num: ");
int num = input.nextInt();
RandomAccessFile src = new RandomAccessFile(jj,"r");
src.seek(0);
long llen = src.length()/num;
for(int i=1;i<=num;i++){
RandomAccessFile tmp = new RandomAccessFile(jj+"."+i,"rw");
for(long j=0;j<llen;j++){
int kk = src.read();
if(kk==-1)
break;
else
tmp.write(kk);
}
tmp.close();
}
src.close();
}
}
17.14
import java.util.*;
import java.io.*;
public class book {
public static void main(String[] args)
throws Exception
{
System.out.print("Enter the src and dst: ");
Scanner input = new Scanner(System.in);
String jj = input.next();
String mm = input.next();
RandomAccessFile src = new RandomAccessFile(jj,"r");
RandomAccessFile dst = new RandomAccessFile(mm,"rw");
src.seek(0);
for(int i=0;i<src.length();i++)
{
int tmp=src.read();
tmp+=5;
dst.write(tmp);
}
src.close();
dst.close();
}
}
17.15
import java.util.*;
import java.io.*;
public class book {
public static void main(String[] args)
throws Exception
{
System.out.print("Enter the src and dst: ");
Scanner input = new Scanner(System.in);
String jj = input.next();
String mm = input.next();
RandomAccessFile src = new RandomAccessFile(jj,"r");
RandomAccessFile dst = new RandomAccessFile(mm,"rw");
src.seek(0);
for(int i=0;i<src.length();i++)
{
int tmp=src.read();
tmp-=5;
dst.write(tmp);
}
src.close();
dst.close();
}
}
不写了,全书完