1. 程式人生 > >java語言程式設計 第十七章 (17.12、17.13、17.14、17.15、17.16)

java語言程式設計 第十七章 (17.12、17.13、17.14、17.15、17.16)

程式小白,希望和大家多交流,共同學習
這裡寫圖片描述
這裡寫圖片描述
17.12

//將多個檔案合併
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;

public class CombineFiles 
{
    public static void main(String[] args)
    {
        if (args.length < 2
) { System.out.println("Useage: SourceFile1, sourceFile2...targetFile"); System.exit(1); } else { try { int length = args.length;//確定需要合併的原始檔的個數 File targetFile = new File(args[length - 1]);//建立最後的目標檔案
FileOutputStream output = new FileOutputStream(targetFile); FileInputStream input = null; for (int i = 0; i < length; i++) { File file = new File(args[i]); if (!file.exists()) { System.out
.println(args[i] + " isn't exist."); break; } else { int fileLength = (int)file.length();//每個要合併的原始檔的大小 input = new FileInputStream(args[i]);//輸入流確定為當前要合併的原始檔 byte[] info = new byte[fileLength]; input.read(info);//抓取要合併的檔案的所有內容 output.write(info);//寫入抓取的內容 input.close(); } } output.close(); } catch (FileNotFoundException ex)//輸出檔案是否存在 { System.out.println(ex); } catch (IOException ex) { System.out.println(ex); } } } } //java CombineFiles newSourceFile.0 newSourceFile.1 targetfile.txt

17.13

//合併檔案的GUI版本
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.layout.VBox;
import javafx.scene.layout.GridPane;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class CombineFileGUI extends Application
{
    private TextField tfFile = new TextField();
    private TextField tfNumber = new TextField();
    private Button btStart = new Button("Start");
    private Label result = new Label("get ready");

    @Override
    public void start(Stage primaryStage)
    {
        //主面板
        VBox pane = new VBox(10);
        pane.setPadding(new Insets(10, 10, 10, 10));
        pane.setAlignment(Pos.CENTER);
        //放文字框內容的面板
        GridPane gbPane = new GridPane();
        gbPane.setHgap(10);
        gbPane.setVgap(10);
        //新增文字框
        tfFile.setAlignment(Pos.BOTTOM_LEFT);
        tfNumber.setAlignment(Pos.BOTTOM_LEFT);
        gbPane.add(new Label("Enter a file: "), 0, 0);
        gbPane.add(tfFile, 1, 0);
        gbPane.add(new Label("Specify the number of smaller files: "), 0, 1);
        gbPane.add(tfNumber, 1, 1);
        result.setTextFill(Color.RED);
        result.setStyle("-fx-underline: true");
        //新增至主面板
        pane.getChildren().addAll(new Label("If the base file is named temp.txt with three prieces\n" +
            "temp.txt.1,temp.txt.2,temp.txt.3....are combined into temp.txt"),
            gbPane, btStart, result);

        //功能實現
        btStart.setOnAction(e -> {
            if (!tfFile.getText().equals("") && !tfNumber.getText().equals(""))
            {
                result.setText("Lodding");
                long start = System.currentTimeMillis();
                String fileName = tfFile.getText();
                int number = Integer.parseInt(tfNumber.getText());
                try
                {
                    combineFiles(fileName, number);
                }
                catch(FileNotFoundException ex)
                {
                    System.out.println(ex);
                }
                catch (IOException ex)
                {
                    System.out.println(ex);
                }
                long finish = System.currentTimeMillis();
                result.setText((finish - start) + " Millis");
            }
            else
                result.setText("TextFields can't be empty.");
        });

        Scene scene = new Scene(pane);
        primaryStage.setTitle("DivideFile");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    //合併多個檔案
    public void combineFiles(String fileName, int number) throws FileNotFoundException, IOException
    {
        File targetFile = new File(fileName);//建立最後的目標檔案
        FileOutputStream output = new FileOutputStream(targetFile);
        FileInputStream input = null;
        for (int i = 0; i < number; i++)
        {
            File file = new File(fileName + "." + i);

            int fileLength = (int)file.length();
            input = new FileInputStream(file);
            byte[] info = new byte[fileLength];
            input.read(info);//抓取要合併的檔案的所有內容
            output.write(info);//寫入抓取的內容
            input.close();
        }
        output.close();
    }

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

17.14

//將檔案進行加密
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class EncryptedFile
{
    public static void main(String[] args)
    {
        if (args.length != 2)
        {
            System.out.println("Useage: java EncryptedFile ASCIIFileName targetFileName");
            System.exit(1);
        }
        else
        {
            try
            {
                FileInputStream input = new FileInputStream(args[0]);
                FileOutputStream output = new FileOutputStream(args[1], true);
                int read = 0;
                while ((read = input.read()) != -1)
                {
                    output.write(read + 5);
                    System.out.println(read + " ");
                }
            }
            catch (IOException ex)
            {
                System.out.println(ex);
            }
        }
    }
}
//java EncryptedFile 文字.txt 文字加密.txt

17.15

//將檔案進行解密
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DecryptFile
{
    public static void main(String[] args)
    {
        if (args.length != 2)
        {
            System.out.println("Useage: java DecryptFile SourceFileName targetFileName");
            System.exit(1);
        }
        else
        {
            try
            {
                FileInputStream input = new FileInputStream(args[0]);
                FileOutputStream output = new FileOutputStream(args[1]);
                int read = 0;
                while ((read = input.read()) != -1)
                {
                    output.write(read - 5);
                }
            }
            catch (IOException ex)
            {
                System.out.println(ex);
            }
        }
    }
}
//java DecryptFile 文字加密.txt 文字解密.txt

17.16

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

public class CountASCII
{
    public static void main(String[] args)
    {
        if (args.length != 1)
        {
            System.out.println("Useage:java CountASCII sourceFileName");
            System.exit(1);
        }
        int[] count = new int[255];
        Arrays.fill(count, 0);
        try
        {
            FileInputStream input = new FileInputStream(args[0]);
            int read = 0;
            while ((read = input.read()) != -1)
            {
                //System.out.println(read);
                count[read]++;
            }
        }
        catch (IOException ex)
        {
            System.out.println(ex);
        }

        for (int i = 0; i < 255; i++)
        {
            System.out.println((char)i + " " + count[i]);
        }
    }
}
//java CountASCII AddressGUI.java