java语言程序设计 第十七章 (17.12、17.13、17.14、17.15、17.16)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cheng_cuo_tuo/article/details/79879486

程序小白,希望和大家多交流,共同学习
这里写图片描述
这里写图片描述
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
展开阅读全文

没有更多推荐了,返回首页