目录
题目:*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的相同