ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/Printed HTMLs/SplitFilesFX.html

153 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>SplitFilesFX.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.ST2 {color: #9876aa}
.ST4 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST5 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/SplitFilesFX.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">io</span><span class="comment">.</span><span class="comment">DataInput</span><span class="comment">;</span>
<span class="literal">import</span> java.io.File;
<span class="literal">import</span> java.io.FileInputStream;
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="literal">import</span> javafx.application.Application;
<span class="literal">import</span> javafx.geometry.Pos;
<span class="literal">import</span> javafx.scene.Scene;
<span class="literal">import</span> javafx.scene.control.Alert;
<span class="literal">import</span> javafx.scene.control.Button;
<span class="literal">import</span> javafx.scene.control.Label;
<span class="literal">import</span> javafx.scene.control.ProgressBar;
<span class="literal">import</span> javafx.scene.control.TextField;
<span class="literal">import</span> javafx.scene.layout.BorderPane;
<span class="literal">import</span> javafx.scene.layout.GridPane;
<span class="literal">import</span> javafx.stage.FileChooser;
<span class="literal">import</span> javafx.stage.Stage;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> SplitFilesFX <span class="literal">extends</span> Application {
<span class="literal">static</span> File <span class="ST1">file</span> = <span class="literal">null</span>;
<span class="literal">final</span> FileChooser <span class="ST2">fileChooser</span> = <span class="literal">new</span> FileChooser();
<span class="literal">static</span> ProgressBar <span class="ST1">pb</span> = <span class="literal">new</span> ProgressBar();
<span class="literal">static</span> Alert <span class="ST1">alert</span> = <span class="literal">new</span> Alert(Alert.<span class="ST3">AlertType</span>.<span class="ST1">INFORMATION</span>);
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST4">start</span>(<span class="literal">final</span> Stage stage) <span class="literal">throws</span> Exception
{
<span class="ST2">fileChooser</span>.setTitle(<span class="string">&quot;</span><span class="string">Open File to Split...</span><span class="string">&quot;</span>);
BorderPane primaryBorderPane = <span class="literal">new</span> BorderPane();
BorderPane secondaryBoarderPane = <span class="literal">new</span> BorderPane();
GridPane textFieldGridPane = <span class="literal">new</span> GridPane();
<span class="ST1">pb</span>.prefWidthProperty().bind(stage.widthProperty());
Label infoLabel = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">If you split a file named tmp into 3 smaller files,</span><span class="literal">\n</span><span class="string"> the three smaller files are temp.txt.1, temp.txt.2, and temp.txt.3. </span><span class="string">&quot;</span>);
Label chooseFile = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Choose a file to split: </span><span class="string">&quot;</span>);
Button openFilePicker = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Choose...</span><span class="string">&quot;</span>);
Label splitCountLabel = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Enter the amount of files to split into: </span><span class="string">&quot;</span>);
TextField splitCount = <span class="literal">new</span> TextField();
Button run = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Start</span><span class="string">&quot;</span>);
textFieldGridPane.add(chooseFile, <span class="number">0</span>, <span class="number">0</span>);
textFieldGridPane.add(openFilePicker, <span class="number">1</span>, <span class="number">0</span>);
textFieldGridPane.add(splitCountLabel, <span class="number">0</span>, <span class="number">1</span>);
textFieldGridPane.add(splitCount, <span class="number">1</span>, <span class="number">1</span>);
secondaryBoarderPane.<span class="ST3">setAlignment</span>(run, Pos.<span class="ST1">CENTER</span>);
primaryBorderPane.setTop(infoLabel);
primaryBorderPane.setCenter(textFieldGridPane);
secondaryBoarderPane.setBottom(run);
secondaryBoarderPane.setTop(<span class="ST1">p</span><span class="ST1">b</span>);
primaryBorderPane.setBottom(secondaryBoarderPane);
openFilePicker.setOnAction(e -&gt; {
<span class="ST1">file</span> = <span class="ST2">fileChooser</span>.showOpenDialog(openFilePicker.getScene().getWindow());
});
run.setOnAction(e -&gt; {
<span class="literal">if</span> (<span class="ST1">file</span> == <span class="literal">null</span>) {
e.consume();
}
<span class="ST3">splitFile</span>(<span class="ST1">f</span><span class="ST1">ile</span>, Integer.<span class="ST3">parseInt</span>(splitCount.getText()));
});
Scene scene = <span class="literal">new</span> Scene(primaryBorderPane);
stage.setScene(scene);
stage.show();
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">splitFile</span>(File fileToSplit, <span class="literal">int</span> splitCount) {
<span class="literal">int</span> outputFileSize = (<span class="literal">int</span>) fileToSplit.length() / splitCount;
System.<span class="ST1">out</span>.println(<span class="string">&quot;</span><span class="string">output file size will be: </span><span class="string">&quot;</span> + outputFileSize);
<span class="literal">if</span> (outputFileSize &lt; <span class="number">0</span>) {
<span class="ST1">alert</span>.setTitle(<span class="string">&quot;</span><span class="string">Output files too large</span><span class="string">&quot;</span>);
<span class="ST1">alert</span>.setHeaderText(<span class="string">&quot;</span><span class="string">Output files too large</span><span class="string">&quot;</span>);
<span class="ST1">alert</span>.setContentText(<span class="string">&quot;</span><span class="string">Please increase the amount of files to split!</span><span class="string">&quot;</span>);
<span class="ST1">alert</span>.showAndWait();
<span class="literal">return</span>;
}
<span class="literal">double</span> progress = <span class="number">0</span>;
String outputPath = fileToSplit.getParent();
<span class="comment">// Open the original file</span>
<span class="literal">try</span> (FileInputStream dataIn = <span class="literal">new</span> FileInputStream(fileToSplit)) {
System.<span class="ST1">out</span>.println(<span class="string">&quot;</span><span class="string">Opening the source file!</span><span class="string">&quot;</span>);
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; splitCount; ++i) {
progress = (i * <span class="number">100</span>) / splitCount;
System.<span class="ST1">out</span>.println(progress);
<span class="ST1">pb</span>.setProgress(progress);
dataIn.mark(outputFileSize * (i + <span class="number">1</span>));
<span class="literal">try</span> (FileOutputStream dataOut = <span class="literal">new</span> FileOutputStream(outputPath + <span class="string">&quot;</span><span class="string">/</span><span class="string">&quot;</span> + fileToSplit.getName() + <span class="string">&quot;</span><span class="string">.</span><span class="string">&quot;</span> + (i + <span class="number">1</span>))) {
dataOut.write(dataIn.readNBytes(outputFileSize), <span class="number">0</span>, outputFileSize);
System.<span class="ST1">out</span>.println(<span class="string">&quot;</span><span class="string">Writing to </span><span class="string">&quot;</span> + outputPath + <span class="string">&quot;</span><span class="string">/</span><span class="string">&quot;</span> + fileToSplit.getName() + <span class="string">&quot;</span><span class="string">.</span><span class="string">&quot;</span> + (i + <span class="number">1</span>));
}
}
} <span class="literal">catch</span> (IOException ex) {
System.<span class="ST1">out</span>.println(ex);
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args)
{
<span class="ST3">launch</span>();
}
}
<span class="comment">/*</span>
<span class="comment"> */</span>
</pre></body>
</html>