Je crée un éditeur de texte en tant que projet parallèle et, pour le moment, je me bats en enregistrant et en chargeant la lettre, la couleur et la décoration du texte; en d'autres termes, je ne peux sauvegarder qu'un texte en clair. Ma question est, comment puis-je sauvegarder et charger tout cela?Sauvegarde de la police à partir d'un volet texte java
private void Edit() {//This is the method where the program edits the text
StyledDocument doc = this.tpText.getStyledDocument();
Style estilo = this.tpText.addStyle("miEstilo", null);
StyleConstants.setForeground(estilo, colour); //Color
StyleConstants.setFontFamily(estilo, fontLetter);//Letter
StyleConstants.setFontSize(estilo, size);//Size
StyleConstants.setBold(estilo, bold);//Bold
StyleConstants.setItalic(estilo, italics);//Italics
StyleConstants.setUnderline(estilo, underline);//Underline
doc.setCharacterAttributes(this.tpText.getSelectionStart(), this.tpText.getSelectionEnd() - this.tpText.getSelectionStart(), this.tpText.getStyle("miEstilo"), true);//The only text that will be edited is the one that the user highlights
}
private void comboxFontsActionPerformed(java.awt.event.ActionEvent evt) {//This is one of the methods in which the program edits the text
this.fontLetter = (String) this.comboxFonts.getSelectedItem();
Edit();
}
private void mibtnSaveAsActionPerformed(java.awt.event.ActionEvent evt) {//Save as... menu item button
int saveResult = fileSelect.showSaveDialog(null);
if (saveResult == fileSelect.APPROVE_OPTION) {
saveFile(fileSelect.getSelectedFile(), this.tpText.getText());
this.mibtnSave.setEnabled(true);
}
}
public void saveFile(File file, String contents) {//Save File
BufferedWriter writer = null;
String filePath = file.getPath();
try {
writer = new BufferedWriter(new FileWriter(filePath));
writer.write(contents);
writer.close();
this.tpText.setText(contents);
currentFile = file;
} catch (Exception e) {
}
}
public void openFile(File file) {//Load File
if (file.canRead()) {
String filePath = file.getPath();
String fileContents = "";
if (filePath.endsWith(".txt")) {
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()) {
fileContents += sc.nextLine();
}
sc.close();
} catch (FileNotFoundException e) {
}
this.tpText.setText(fileContents);
currentFile = file;
} else {
JOptionPane.showMessageDialog(null, "Only .txt files are supported.");
}
} else {
JOptionPane.showMessageDialog(null, "Could not open file...");
}
}
Je pensais à cela aussi, mais voici le problème: disons que le texte est "bonjour monde" la partie "bonjour" est en gras alors que "monde" est en italique. "** hello ** _world_" Je veux savoir comment puis-je sauvegarder et charger le texte comme ça. – RMorazan
Ok laissez-moi vous suggérer quelque chose mon ami .. lorsque tout utilisateur essaie de faire un style de texte enregistrer ce style dans une chaîne, puis l'écrire dans un fichier juste à côté du texte brut enregistré. exemple: lorsque le bouton gras ou italique est cliqué pour faire bonjour gras et monde italique votre chaîne devrait être quelque chose comme: "ligne X col Y mot 'bonjour' gras" où «X et Y» sont la position de la mot dans le texte –