Created
January 19, 2017 12:33
-
-
Save Sadwyn/6f71bc9155a4539178e0423d9c2b8647 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.event.*; | |
import javafx.event.Event; | |
import javafx.fxml.FXML; | |
import javafx.scene.Node; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.ProgressBar; | |
import javafx.scene.input.DragEvent; | |
import javafx.scene.input.Dragboard; | |
import javafx.scene.input.TransferMode; | |
import javafx.stage.FileChooser; | |
import java.awt.*; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.RandomAccessFile; | |
import java.util.ArrayList; | |
import java.util.regex.Pattern; | |
public class Controller { | |
@FXML | |
public ProgressBar progressBar; | |
private Desktop desktop = Desktop.getDesktop(); | |
@FXML | |
public Label fileNameLabel; | |
private static RandomAccessFile file; // поток чтения запси | |
private FileChooser chooser = new FileChooser(); | |
private static File myFile; // загружаемый файл | |
public static void setFile(RandomAccessFile file) { | |
Controller.file = file; | |
} | |
public void onClickAdd(ActionEvent actionEvent) { | |
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(".TXT", "*.txt")); | |
if (myFile != null) | |
chooser.setInitialDirectory(myFile.getParentFile()); | |
myFile = chooser.showOpenDialog(((Node) actionEvent.getSource()).getScene().getWindow()); | |
try { | |
setFile(new RandomAccessFile(myFile, "rw")); | |
fileNameLabel.setText(myFile.getName()); | |
} catch (FileNotFoundException e) { | |
fileNameLabel.setText("Ошибка загрузки файла"); | |
} | |
} | |
public void onEncode(ActionEvent actionEvent) throws IOException { | |
if (file != null) { | |
Runtime.getRuntime().exec("taskkill /IM notepad.exe"); | |
int counter = 0; | |
int read; | |
file = new RandomAccessFile(myFile, "rw"); | |
String[] binary = new String[(int) file.length()]; | |
while ((read = file.read()) != -1) { | |
binary[counter++] = Integer.toBinaryString(read); | |
} | |
file.setLength(0); | |
for (String s : binary) { | |
file.seek(file.length()); | |
file.writeBytes(s + " "); | |
} | |
file.close(); | |
if (myFile.exists()) | |
desktop.open(myFile); | |
} | |
} | |
public void onDecode(ActionEvent actionEvent) throws IOException { | |
Runtime.getRuntime().exec("taskkill /IM notepad.exe"); | |
Pattern p = Pattern.compile(" "); | |
if (file != null) { | |
ArrayList<String> list = new ArrayList<>(); | |
file = new RandomAccessFile(myFile, "rw"); | |
String line; | |
while ((line = file.readLine()) != null) { | |
String[] strings = p.split(line); | |
for (String str : strings) { | |
list.add(str); | |
} | |
} | |
file.setLength(0); | |
int cursorPos = 0; | |
for (String str : list) { | |
file.seek(cursorPos++); | |
file.write(Integer.parseInt(str, 2)); | |
} | |
file.close(); | |
if (myFile.exists()) | |
desktop.open(myFile); | |
} | |
} | |
public static void initialize() { | |
Main.getScene().setOnDragOver(new EventHandler<DragEvent>() { | |
@Override | |
public void handle(DragEvent event) { | |
Dragboard db = event.getDragboard(); | |
if (db.hasFiles()) | |
event.acceptTransferModes(TransferMode.COPY); | |
else event.consume(); | |
} | |
}); | |
Main.getScene().setOnDragDropped(new EventHandler<DragEvent>() { | |
@Override | |
public void handle(DragEvent event) { | |
Dragboard db = event.getDragboard(); | |
boolean success = false; | |
if (db.hasFiles()) { | |
success = true; | |
myFile = db.getFiles().get(0); | |
try { | |
setFile(new RandomAccessFile(myFile, "rw")); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
event.setDropCompleted(success); | |
event.consume(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment