《Java语言程序设计(基础篇)》(第10版 梁勇 著)第十七章练习题答案 下载本文

}

catch (IOException ex) { ex.printStackTrace(); } } /**

* The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */

public static void main(String[] args) { launch(args); } }

17.14

import java.util.Scanner; import java.io.*;

public class Exercise17_14 {

public static void main(String[] args) throws Exception { Scanner input = new Scanner(System.in); System.out.print(\); File inputFile = new File(input.nextLine()); System.out.print(\); File outputFile = new File(input.nextLine()); try (

BufferedInputStream in = new BufferedInputStream( new FileInputStream(inputFile));

BufferedOutputStream output = new BufferedOutputStream( new FileOutputStream(outputFile)); ) {

int value;

while ((value = in.read()) != -1) { output.write(value + 5); } } } }

17.15

import java.util.Scanner; import java.io.*;

public class Exercise17_15 {

public static void main(String[] args) throws Exception { Scanner input = new Scanner(System.in);

System.out.print(\); File inputFile = new File(input.nextLine()); System.out.print(\); File outputFile = new File(input.nextLine()); try (

BufferedInputStream in = new BufferedInputStream( new FileInputStream(inputFile));

BufferedOutputStream output = new BufferedOutputStream( new FileOutputStream(outputFile)); ) {

int value;

while ((value = in.read()) != -1) { output.write(value - 5); } } } }

17.16

import java.io.*;

import java.util.Scanner;

public class Exercise17_16 {

public static void main(String[] args) throws Exception { Scanner input = new Scanner(System.in); System.out.print(\); String filename = input.nextLine();

int[] counts = new int[128];

try (

BufferedInputStream fileInput = new BufferedInputStream( new FileInputStream(new File(filename))); ) { int r;

while ((r = fileInput.read()) != -1 ) { counts[(byte)r]++; } }

System.out.printf(\, \, \); for (int i = 0; i < counts.length; i++) if (counts[i] != 0)

System.out.printf(\, i, counts[i]); } }

17.17

import java.io.*;

public class Exercise17_17 {

public static void main(String[] args) throws Exception { BitOutputStream output = new BitOutputStream(new File(\));

output.writeBit(\); output.close();

System.out.println(\); }

public static class BitOutputStream { private FileOutputStream output; private int value; private int count = 0;

private int mask = 1; // The bits are all zeros except the last one

public BitOutputStream(File file) throws IOException { output = new FileOutputStream(file); }

public void writeBit(char bit) throws IOException { count++;

value = value << 1;

if (bit == '1') value = value | mask;

if (count == 8) { output.write(value);

count = 0; } }

public void writeBit(String bitString) throws IOException { for (int i = 0; i < bitString.length(); i++) writeBit(bitString.charAt(i)); }

/** Write the last byte and close the stream. If the last byte is not full, right-shfit with zeros */

public void close() throws IOException { if (count > 0) {

value = value << (8 - count); output.write(value); }

output.close(); } } }

17.18

import java.io.*;

import java.util.Scanner;

public class Exercise17_18 {

public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); System.out.print(\); String filename = input.nextLine();

FileInputStream in = new FileInputStream(filename);

int value;

while ((value = in.read()) != -1) { System.out.print(getBits(value)); }

in.close(); }

public static String getBits(int value) {

String result = \;

int mask = 1;

for (int i = 7; i >= 0; i--) { int temp = value >> i; int bit = temp & mask; result = result + bit; }

return result; } }

17.19

import java.io.*;

import java.util.Scanner;

public class Exercise17_19 {

public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); System.out.print(\); String filename = input.nextLine();

FileInputStream in = new FileInputStream(filename);

int value;

while ((value = in.read()) != -1) {

System.out.print(getHex(getBits(value))); } }

/** Convert the 8-bit string to a 2-digit hex number */ public static String getHex(String bitString) { // Get the first half hex number

int value = (bitString.charAt(0) - '0') * 8 + (bitString.charAt(1) - '0') * 4 + (bitString.charAt(2) - '0') * 2 + (bitString.charAt(3) - '0') * 1;

String result = \ + toHexChar(value);

// Get the second half hex number

value = (bitString.charAt(4) - '0') * 8 + (bitString.charAt(5) - '0') * 4 +