Java语言程序设计基础篇_编程练习题*17.13 (带GUI的组合文件工具)

目录

题目:*17.13 (带GUI的组合文件工具)

代码示例 

结果展示 


题目:*17.13 (带GUI的组合文件工具)

改写编程练习题17.12使之带有GUI,如图1721b所示

可以使用编程练习题17.11的GUI代码和编程练习题17.12的程序代码:

Java语言程序设计基础篇_编程练习题**17.11 (带GUI的分割文件工具)-CSDN博客

Java语言程序设计基础篇_编程练习题*17.12 (组合文件)-CSDN博客

  • 代码示例 
package chapter_17;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class 编程练习题17_13GUIcombinedFile extends Application{
	private Label lbInfo;
	private TextField tfFile;
	private TextField tfNum;
	@Override
	public void start(Stage primaryStage) throws Exception {
		lbInfo = new Label("If the base file is named temp.txt with three pieces,\n"
				+ "temp.txt.1, temp.txt.2,and temp.txt.3 are combined into temp.txt");
		tfFile = new TextField();
		tfNum = new TextField();
		Label lbFile = new Label("Enter a file:");
		lbFile.setContentDisplay(ContentDisplay.RIGHT);
		Label lbNum = new Label("Specify the number of smaller files:");
		lbNum.setContentDisplay(ContentDisplay.RIGHT);
		Button btStart = new Button("Start");
		
		GridPane gridPane = new GridPane();
		gridPane.add(lbFile, 0, 0);
		gridPane.add(tfFile, 1, 0);
		gridPane.add(lbNum, 0, 1);
		gridPane.add(tfNum, 1, 1);
		VBox vBox = new VBox(5);
		vBox.setAlignment(Pos.CENTER);
		vBox.getChildren().addAll(lbInfo, gridPane, btStart);
		
		btStart.setOnMouseClicked(e ->{
			try{
				String[] list = tfFile.getText().split(" ");
				for(String s:list)
					System.out.println(s);
				BTstart(list);
			
			}catch (IOException ex) {
				ex.printStackTrace();
				// TODO: handle exception
			}
		});
		
		Scene scene = new Scene(vBox);
		primaryStage.setTitle("编程练习题17_13GUIcombinedFile");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	public static void main(String[] args) {
		Application.launch(args);
	}
	public void BTstart(String[] args) throws FileNotFoundException,IOException{
		int length = args.length;
		for(int i = 0;i < length-1;i++) {
			DataOutputStream output = new DataOutputStream(new FileOutputStream(args[length-1],true));
			DataInputStream input = new DataInputStream(new FileInputStream(args[i]));
			int bytes = input.read();
			while (bytes != -1) {
				output.write((char)bytes);
				bytes = input.read();
			}
			output.close();
			input.close();
		}
		System.out.println("Done!");
	}
}
  • 结果展示 

src/Text/Exercise17_12_1.txt src/Text/Exercise17_12_2.txt src/Text/Exercise17_12_3.txt src/Text/Exercise17_12_All.txt

文件之间用空格区分 

文件中的结果和17.12的相同

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值