Reset author name to chosen name ✨
This commit is contained in:
@@ -1,256 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.mp5.files_chloefontenot;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.HPos;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.ColumnConstraints;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.RowConstraints;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class AddressBookFX extends Application {
|
||||
|
||||
static File addressBook = new File("AddressBookFX.dat");
|
||||
static int addressArrayPointer = 0;
|
||||
static ArrayList<AddressBookEntry> addressArray = new ArrayList<>();
|
||||
static TextField nameTextField = new TextField();
|
||||
static TextField streetTextField = new TextField();
|
||||
static TextField cityTextField = new TextField();
|
||||
static TextField stateTextField = new TextField();
|
||||
static TextField zipTextField = new TextField();
|
||||
static Label addressBookCounter = new Label("Items in address book:");
|
||||
static Label indexLabel = new Label();
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws Exception
|
||||
{
|
||||
getData();
|
||||
BorderPane primaryBorderPane = new BorderPane();
|
||||
GridPane textFieldGridPane = new GridPane();
|
||||
GridPane addressFieldPane = new GridPane();
|
||||
GridPane buttonFieldPane = new GridPane();
|
||||
|
||||
// Text fields / labels
|
||||
textFieldGridPane.add(new Label("Name"), 0, 0);
|
||||
textFieldGridPane.add(new Label("Street"), 0, 1);
|
||||
textFieldGridPane.add(new Label("City"), 0, 2);
|
||||
addressFieldPane.add(new Label("State"), 2, 0);
|
||||
addressFieldPane.add(new Label("ZIP"), 4, 0);
|
||||
textFieldGridPane.add(nameTextField, 1, 0);
|
||||
textFieldGridPane.add(streetTextField, 1, 1);
|
||||
textFieldGridPane.add(addressFieldPane, 1, 2);
|
||||
addressFieldPane.add(cityTextField, 1, 0);
|
||||
addressFieldPane.add(stateTextField, 3, 0);
|
||||
addressFieldPane.add(zipTextField, 5, 0);
|
||||
textFieldGridPane.getColumnConstraints().add(new ColumnConstraints(50));
|
||||
primaryBorderPane.setTop(textFieldGridPane);
|
||||
|
||||
// Buttons
|
||||
Button addButton = new Button("Add");
|
||||
Button firstButton = new Button("First");
|
||||
Button nextButton = new Button("Next");
|
||||
Button previousButton = new Button("Previous");
|
||||
Button lastButton = new Button("Last");
|
||||
Button updateButton = new Button("Update");
|
||||
buttonFieldPane.add(indexLabel, 0, 0);
|
||||
buttonFieldPane.add(addButton, 1, 0);
|
||||
buttonFieldPane.add(firstButton, 2, 0);
|
||||
buttonFieldPane.add(nextButton, 3, 0);
|
||||
buttonFieldPane.add(previousButton, 4, 0);
|
||||
buttonFieldPane.add(lastButton, 5, 0);
|
||||
buttonFieldPane.add(updateButton, 6, 0);
|
||||
buttonFieldPane.add(addressBookCounter, 7, 0);
|
||||
|
||||
primaryBorderPane.setBottom(buttonFieldPane);
|
||||
buttonFieldPane.setAlignment(Pos.CENTER);
|
||||
buttonFieldPane.setHgap(10);
|
||||
|
||||
addButton.setOnAction(e -> {
|
||||
addressArray.add(new AddressBookEntry());
|
||||
updateData();
|
||||
});
|
||||
firstButton.setOnAction(e -> {
|
||||
addressArrayPointer = 0;
|
||||
getEntry(addressArrayPointer);
|
||||
});
|
||||
nextButton.setOnAction(e -> {
|
||||
if (addressArrayPointer >= addressArray.size() - 1) {
|
||||
addressArrayPointer = 0;
|
||||
} else {
|
||||
addressArrayPointer++;
|
||||
}
|
||||
getEntry(addressArrayPointer);
|
||||
});
|
||||
previousButton.setOnAction(e -> {
|
||||
if (addressArrayPointer > 0) {
|
||||
addressArrayPointer--;
|
||||
} else {
|
||||
addressArrayPointer = addressArray.size() - 1;
|
||||
}
|
||||
getEntry(addressArrayPointer);
|
||||
});
|
||||
lastButton.setOnAction(e -> {
|
||||
addressArrayPointer = addressArray.size() - 1;
|
||||
getEntry(addressArrayPointer);
|
||||
});
|
||||
updateButton.setOnAction(e -> {
|
||||
AddressBookEntry entry = addressArray.get(addressArrayPointer);
|
||||
entry.setName(nameTextField.getText());
|
||||
entry.setCity(nameTextField.getText());
|
||||
entry.setStreet(streetTextField.getText());
|
||||
entry.setCity(cityTextField.getText());
|
||||
entry.setState(stateTextField.getText());
|
||||
entry.setZip(zipTextField.getText());
|
||||
updateData();
|
||||
});
|
||||
|
||||
// init fields with data from first entry
|
||||
getEntry(0);
|
||||
|
||||
Scene scene = new Scene(primaryBorderPane);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
public static AddressBookEntry getEntry(int index)
|
||||
{
|
||||
AddressBookEntry entry = addressArray.get(index);
|
||||
indexLabel.setText("Current index: " + index);
|
||||
addressBookCounter.setText("Items in address book: " + addressArray.size());
|
||||
nameTextField.setText(entry.getName());
|
||||
streetTextField.setText(entry.getStreet());
|
||||
cityTextField.setText(entry.getCity());
|
||||
stateTextField.setText(entry.getState());
|
||||
zipTextField.setText(entry.getZip());
|
||||
return entry;
|
||||
}
|
||||
|
||||
public static void updateData()
|
||||
{
|
||||
try (ObjectOutputStream fileStream = new ObjectOutputStream(new FileOutputStream(addressBook))) {
|
||||
fileStream.writeObject(addressArray);
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void getData()
|
||||
{
|
||||
if (!addressBook.exists()) {
|
||||
addressArray.add(new AddressBookEntry());
|
||||
updateData();
|
||||
} else {
|
||||
try (ObjectInputStream fileStream = new ObjectInputStream(new FileInputStream(addressBook))) {
|
||||
addressArray = (ArrayList<AddressBookEntry>) fileStream.readObject();
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
launch();
|
||||
}
|
||||
}
|
||||
|
||||
class AddressBookEntry implements Serializable {
|
||||
|
||||
private String name;
|
||||
private String street;
|
||||
private String city;
|
||||
private String state;
|
||||
private String zip;
|
||||
|
||||
public AddressBookEntry()
|
||||
{
|
||||
this.name = "Enter a name here.";
|
||||
this.street = "Enter a street here.";
|
||||
this.city = "Enter a city here.";
|
||||
this.state = "Enter a state here.";
|
||||
this.zip = "Enter a zip here.";
|
||||
}
|
||||
|
||||
public AddressBookEntry(String name, String street, String city, String state, String zip)
|
||||
{
|
||||
this.name = name;
|
||||
this.street = street;
|
||||
this.city = city;
|
||||
this.state = state;
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
public String getZip()
|
||||
{
|
||||
return zip;
|
||||
}
|
||||
|
||||
public void setZip(String zip)
|
||||
{
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
public String getState()
|
||||
{
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(String state)
|
||||
{
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getCity()
|
||||
{
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city)
|
||||
{
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getStreet()
|
||||
{
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street)
|
||||
{
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.chloefontenot.mp5.files_chloefontenot;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.layout.StackPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
|
||||
/**
|
||||
* JavaFX App
|
||||
*/
|
||||
public class App extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) {
|
||||
var javaVersion = SystemInfo.javaVersion();
|
||||
var javafxVersion = SystemInfo.javafxVersion();
|
||||
|
||||
var label = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
|
||||
var scene = new Scene(new StackPane(label), 640, 480);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.mp5.files_chloefontenot;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Alert.AlertType;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.stage.FileChooser;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class CombineFilesFX extends Application {
|
||||
|
||||
static ArrayList<File> files = new ArrayList<File>();
|
||||
final FileChooser fileChooser = new FileChooser();
|
||||
|
||||
@Override
|
||||
public void start(final Stage stage) throws Exception {
|
||||
fileChooser.setTitle("Open File to Split...");
|
||||
BorderPane primaryBorderPane = new BorderPane();
|
||||
GridPane textFieldGridPane = new GridPane();
|
||||
|
||||
Label infoLabel = new Label("Point me at the files that were output by SplitFilesFX.java.");
|
||||
Label chooseFile = new Label("Choose files to combine: ");
|
||||
Button openFilePicker = new Button("Choose...");
|
||||
Button run = new Button("Start");
|
||||
textFieldGridPane.add(chooseFile, 0, 0);
|
||||
textFieldGridPane.add(openFilePicker, 1, 0);
|
||||
primaryBorderPane.setAlignment(run, Pos.CENTER);
|
||||
|
||||
primaryBorderPane.setTop(infoLabel);
|
||||
primaryBorderPane.setCenter(textFieldGridPane);
|
||||
primaryBorderPane.setBottom(run);
|
||||
|
||||
openFilePicker.setOnAction(e -> {
|
||||
// When we initially get the files from the file chooser, it returns an unsortable list. Because of this, we need to create a new List with its data.
|
||||
files.addAll(fileChooser.showOpenMultipleDialog(openFilePicker.getScene().getWindow()));
|
||||
// The file picker appears to return files in a random order, so we need to sort them by file name.
|
||||
Collections.sort(files, new Comparator<File>() {
|
||||
public int compare(File o1, File o2) {
|
||||
return extractInt(o1.getName()) - extractInt(o2.getName());
|
||||
}
|
||||
|
||||
String findMatch(String s) {
|
||||
Pattern findNum = Pattern.compile("\\d+$");
|
||||
Matcher match = findNum.matcher(s);
|
||||
while (match.find()) {
|
||||
return match.group();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
int extractInt(String s) {
|
||||
String num = findMatch(s);
|
||||
System.out.println(num);
|
||||
// return 0 if no digits found
|
||||
return num.isEmpty() ? 0 : Integer.parseInt(num);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
run.setOnAction(e -> {
|
||||
if (files == null) {
|
||||
e.consume();
|
||||
}
|
||||
combineFiles(files);
|
||||
});
|
||||
|
||||
Scene scene = new Scene(primaryBorderPane);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
public static void combineFiles(List<File> filesToCombine) {
|
||||
String outputPath = filesToCombine.get(0).getParent();
|
||||
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + "reconstructed_" + filesToCombine.get(0).getName().substring(0, (filesToCombine.get(0).getName().length() - 2)))) {
|
||||
System.out.println("Writing to " + outputPath + "/" + "reconstructed_" + filesToCombine.get(0).getName().substring(0, (filesToCombine.get(0).getName().length() - 2)));
|
||||
for (File file : filesToCombine) {
|
||||
try (FileInputStream dataIn = new FileInputStream(file)) {
|
||||
System.out.println("Opening the source file " + file.getName() + "!");
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead = -1;
|
||||
|
||||
while ((bytesRead = dataIn.read(buffer)) != -1) {
|
||||
dataOut.write(buffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
Alert alert = new Alert(AlertType.INFORMATION);
|
||||
alert.setTitle("Success!");
|
||||
alert.setContentText("Successfully combined the files!");
|
||||
|
||||
alert.showAndWait();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch();
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.mp5.files_chloefontenot;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.RandomAccessFile;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Exercise17_01 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
try (RandomAccessFile fileIO = new RandomAccessFile("Exercise17_01.txt", "rw")) {
|
||||
int random = 0;
|
||||
System.out.println("Writing data to file...");
|
||||
for (int i = 1; i < 100 + 1; ++i) {
|
||||
random = (int) (Math.random() * 9) + 1;
|
||||
System.out.print(random + " ");
|
||||
if (i != 0 && i % 10 == 0) {
|
||||
System.out.println();
|
||||
}
|
||||
fileIO.writeInt(random);
|
||||
}
|
||||
System.out.println("Wrote to the file successfully!");
|
||||
System.out.println("File contents:");
|
||||
fileIO.seek(0);
|
||||
int readIterator = 1;
|
||||
while (true) {
|
||||
try {
|
||||
System.out.print(fileIO.readInt() + " ");
|
||||
if (readIterator != 0 && readIterator % 10 == 0) {
|
||||
System.out.println();
|
||||
}
|
||||
++readIterator;
|
||||
} catch (EOFException e) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.mp5.files_chloefontenot;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Exercise17_03 {
|
||||
|
||||
static int fileSize = Math.abs((int) (Math.random() * 1024));
|
||||
|
||||
public static void writeData()
|
||||
{
|
||||
try (FileOutputStream fileWrite = new FileOutputStream("Exercise17_03.dat")) {
|
||||
// Write a unspecified number of integers into the file.
|
||||
// Must be positive!
|
||||
int randInt = 0;
|
||||
for (int i = 0; i < fileSize; ++i) {
|
||||
randInt = (int) (Math.random() * 10);
|
||||
fileWrite.write(randInt);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
System.out.println("Wrote data to the file!");
|
||||
}
|
||||
|
||||
public static int[] readData()
|
||||
{
|
||||
int[] fileData = new int[fileSize];
|
||||
try (FileInputStream fileRead = new FileInputStream("Exercise17_03.dat")) {
|
||||
// Read the data back
|
||||
int dataIterator = 0;
|
||||
int dataStream = 0;
|
||||
while (fileRead.available() > 0) {
|
||||
dataStream = fileRead.read();
|
||||
fileData[dataIterator++] = dataStream;
|
||||
System.out.print(dataStream + " ");
|
||||
if ((dataIterator + 1) % 10 == 0) {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
return fileData;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println("Ints to write: " + fileSize);
|
||||
writeData();
|
||||
int[] fileData = readData();
|
||||
// Sum the digits
|
||||
int sum = 0;
|
||||
for (int i: fileData) {
|
||||
sum += i;
|
||||
}
|
||||
System.out.println("\nThe sum of the integers in the file is: " + sum);
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.mp5.files_chloefontenot;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.Serializable;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Exercise17_05 {
|
||||
public static void main(String[] args) {
|
||||
File dataFile = new File("Exercise17_05.dat");
|
||||
if(!dataFile.exists()) {
|
||||
try (ObjectOutputStream fileStream = new ObjectOutputStream(new FileOutputStream(dataFile))) {
|
||||
fileStream.writeObject(new DataContainer());
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
}
|
||||
DataContainer data = null;
|
||||
try (ObjectInputStream fileStream = new ObjectInputStream(new FileInputStream(dataFile))) {
|
||||
data = (DataContainer) fileStream.readObject();
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
// Now print out the data!
|
||||
System.out.println("We got the data from the file!");
|
||||
System.out.println(data.toString());
|
||||
}
|
||||
}
|
||||
|
||||
class DataContainer implements Serializable {
|
||||
int[] intArray = {1, 2, 3, 4};
|
||||
Date currentDate = new Date();
|
||||
double doubleMoment = 5.5;
|
||||
|
||||
public int[] getIntArray()
|
||||
{
|
||||
return intArray;
|
||||
}
|
||||
|
||||
public Date getCurrentDate()
|
||||
{
|
||||
return currentDate;
|
||||
}
|
||||
|
||||
public double getDoubleMoment()
|
||||
{
|
||||
return doubleMoment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String intString = "[";
|
||||
for (int i = 0; i < intArray.length - 1; ++i) {
|
||||
intString += intArray[i];
|
||||
if (i == (intArray.length - 2)) {
|
||||
intString += "]";
|
||||
} else {
|
||||
intString += ", ";
|
||||
}
|
||||
}
|
||||
return "DataContainer{" + "intArray=" + intString + ", currentDate=" + currentDate + ", doubleMoment=" + doubleMoment + '}';
|
||||
}
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.mp5.files_chloefontenot;
|
||||
|
||||
import static com.chloefontenot.mp5.files_chloefontenot.SplitFilesFX.alert;
|
||||
import static com.chloefontenot.mp5.files_chloefontenot.SplitFilesFX.file;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.stage.FileChooser;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class HexEditorFX extends Application {
|
||||
|
||||
final FileChooser fileChooser = new FileChooser();
|
||||
static File file = null;
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws Exception {
|
||||
Label label = new Label("Open a file...");
|
||||
Button openFileButton = new Button("Choose...");
|
||||
TextArea editorWindow = new TextArea();
|
||||
Button saveFileButton = new Button("Save");
|
||||
|
||||
editorWindow.setWrapText(true);
|
||||
|
||||
BorderPane bp = new BorderPane();
|
||||
GridPane openFileGridPane = new GridPane();
|
||||
|
||||
openFileGridPane.add(label, 0, 0);
|
||||
openFileGridPane.add(openFileButton, 1, 0);
|
||||
|
||||
bp.setTop(openFileGridPane);
|
||||
bp.setCenter(editorWindow);
|
||||
bp.setBottom(saveFileButton);
|
||||
bp.setAlignment(saveFileButton, Pos.CENTER);
|
||||
|
||||
openFileButton.setOnAction(e -> {
|
||||
file = fileChooser.showOpenDialog(openFileButton.getScene().getWindow());
|
||||
byte[] data = null;
|
||||
System.out.println("Getting data from fileand encoding it as hex...");
|
||||
try (FileInputStream dataIn = new FileInputStream(file)) {
|
||||
data = dataIn.readAllBytes();
|
||||
} catch (FileNotFoundException ex) {
|
||||
ex.printStackTrace();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
String dataString = encodeHexString(data);
|
||||
editorWindow.setText(dataString);
|
||||
});
|
||||
|
||||
saveFileButton.setOnAction(e -> {
|
||||
if (file == null) {
|
||||
alert.setTitle("No file selected!");
|
||||
alert.setHeaderText("No file selected!");
|
||||
alert.setContentText("You have to open a file first, silly!");
|
||||
alert.showAndWait();
|
||||
}
|
||||
System.out.println("Re-encoding hex back into a byte array...");
|
||||
|
||||
try (FileOutputStream dataOut = new FileOutputStream(file)) {
|
||||
byte[] bytesToSave = decodeHexString(editorWindow.getText());
|
||||
dataOut.write(bytesToSave);
|
||||
} catch (FileNotFoundException ex) {
|
||||
ex.printStackTrace();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
alert.setTitle("Invalid Hex!");
|
||||
alert.setContentText("Invalid hex entered into the text box, unable to save.");
|
||||
alert.showAndWait();
|
||||
}
|
||||
alert.setTitle("Save successful!");
|
||||
alert.setContentText("File saved successfully.");
|
||||
alert.showAndWait();
|
||||
});
|
||||
|
||||
Scene scene = new Scene(bp);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
|
||||
}
|
||||
|
||||
private int toDigit(char hexChar) {
|
||||
int digit = Character.digit(hexChar, 16);
|
||||
if (digit == -1) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid Hexadecimal Character: " + hexChar);
|
||||
}
|
||||
return digit;
|
||||
}
|
||||
|
||||
public byte hexToByte(String hexString) {
|
||||
int firstDigit = toDigit(hexString.charAt(0));
|
||||
int secondDigit = toDigit(hexString.charAt(1));
|
||||
return (byte) ((firstDigit << 4) + secondDigit);
|
||||
}
|
||||
|
||||
public byte[] decodeHexString(String hexString) {
|
||||
if (hexString.length() % 2 == 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid hexadecimal String supplied.");
|
||||
}
|
||||
byte[] bytes = new byte[hexString.length() / 2];
|
||||
for (int i = 0; i < hexString.length(); i += 2) {
|
||||
bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public String byteToHex(byte num) {
|
||||
char[] hexDigits = new char[2];
|
||||
hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);
|
||||
hexDigits[1] = Character.forDigit((num & 0xF), 16);
|
||||
return new String(hexDigits);
|
||||
}
|
||||
|
||||
public String encodeHexString(byte[] byteArray) {
|
||||
StringBuffer hexStringBuffer = new StringBuffer();
|
||||
for (int i = 0; i < byteArray.length; i++) {
|
||||
hexStringBuffer.append(byteToHex(byteArray[i]));
|
||||
}
|
||||
return hexStringBuffer.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.mp5.files_chloefontenot;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ProgressBar;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.stage.FileChooser;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class SplitFilesFX extends Application {
|
||||
static File file = null;
|
||||
final FileChooser fileChooser = new FileChooser();
|
||||
static ProgressBar pb = new ProgressBar();
|
||||
static Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
||||
@Override
|
||||
public void start(final Stage stage) throws Exception
|
||||
{
|
||||
fileChooser.setTitle("Open File to Split...");
|
||||
BorderPane primaryBorderPane = new BorderPane();
|
||||
BorderPane secondaryBoarderPane = new BorderPane();
|
||||
GridPane textFieldGridPane = new GridPane();
|
||||
|
||||
pb.prefWidthProperty().bind(stage.widthProperty());
|
||||
|
||||
Label infoLabel = new Label("If you split a file named tmp into 3 smaller files,\n the three smaller files are temp.txt.1, temp.txt.2, and temp.txt.3. ");
|
||||
Label chooseFile = new Label("Choose a file to split: ");
|
||||
Button openFilePicker = new Button("Choose...");
|
||||
Label splitCountLabel = new Label("Enter the amount of files to split into: ");
|
||||
TextField splitCount = new TextField();
|
||||
Button run = new Button("Start");
|
||||
textFieldGridPane.add(chooseFile, 0, 0);
|
||||
textFieldGridPane.add(openFilePicker, 1, 0);
|
||||
textFieldGridPane.add(splitCountLabel, 0, 1);
|
||||
textFieldGridPane.add(splitCount, 1, 1);
|
||||
secondaryBoarderPane.setAlignment(run, Pos.CENTER);
|
||||
|
||||
primaryBorderPane.setTop(infoLabel);
|
||||
primaryBorderPane.setCenter(textFieldGridPane);
|
||||
|
||||
secondaryBoarderPane.setBottom(run);
|
||||
secondaryBoarderPane.setTop(pb);
|
||||
|
||||
primaryBorderPane.setBottom(secondaryBoarderPane);
|
||||
|
||||
openFilePicker.setOnAction(e -> {
|
||||
file = fileChooser.showOpenDialog(openFilePicker.getScene().getWindow());
|
||||
});
|
||||
|
||||
run.setOnAction(e -> {
|
||||
if (file == null) {
|
||||
e.consume();
|
||||
}
|
||||
splitFile(file, Integer.parseInt(splitCount.getText()));
|
||||
});
|
||||
|
||||
Scene scene = new Scene(primaryBorderPane);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
public static void splitFile(File fileToSplit, int splitCount) {
|
||||
int outputFileSize = (int) fileToSplit.length() / splitCount;
|
||||
System.out.println("output file size will be: " + outputFileSize);
|
||||
if (outputFileSize < 0) {
|
||||
alert.setTitle("Output files too large");
|
||||
alert.setHeaderText("Output files too large");
|
||||
alert.setContentText("Please increase the amount of files to split!");
|
||||
|
||||
alert.showAndWait();
|
||||
return;
|
||||
}
|
||||
|
||||
double progress = 0;
|
||||
String outputPath = fileToSplit.getParent();
|
||||
// Open the original file
|
||||
try (FileInputStream dataIn = new FileInputStream(fileToSplit)) {
|
||||
System.out.println("Opening the source file!");
|
||||
for (int i = 0; i < splitCount; ++i) {
|
||||
progress = (i * 100) / splitCount;
|
||||
System.out.println(progress);
|
||||
pb.setProgress(progress);
|
||||
dataIn.mark(outputFileSize * (i + 1));
|
||||
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + fileToSplit.getName() + "." + (i + 1))) {
|
||||
dataOut.write(dataIn.readNBytes(outputFileSize), 0, outputFileSize);
|
||||
System.out.println("Writing to " + outputPath + "/" + fileToSplit.getName() + "." + (i + 1));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
launch();
|
||||
}
|
||||
}
|
||||
/*
|
||||
|
||||
*/
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.chloefontenot.mp5.files_chloefontenot;
|
||||
|
||||
public class SystemInfo {
|
||||
|
||||
public static String javaVersion() {
|
||||
return System.getProperty("java.version");
|
||||
}
|
||||
|
||||
public static String javafxVersion() {
|
||||
return System.getProperty("javafx.version");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user