Witam wszystkich jako ze jest to mój pierwszy post na tym forum. Podczas pisania prostego edytora tekstu napotkałem na problem, mianowicie przy zapisywaniu pliku tekstowego z poziomu programu (który składa się z kilku kolumn tekstu) po ponownym odczycie linie tekstu znajdują się wszystkie w jednym wierszu... tak jakby program nie zapisywał informacji o końcu wiersza, tylko ładował wszystko do jednej linijki :/ Będę bardzo wdzięczny za wskazówkę jak to poprawić.
Pozdrawiam [browar]
package io;
import javax.swing.*;
import java.io.*;
/**
*
* @author student
*/
public class Okno extends javax.swing.JFrame {
/** Creates new form Okno */
public Okno() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Wczytaj");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton2.setText("Zapisz");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2MouseClicked(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
JFileChooser wybierz=new JFileChooser();
int powrot=wybierz.showOpenDialog(this);
if(powrot==JFileChooser.APPROVE_OPTION){
File wybrany=wybierz.getSelectedFile().getAbsoluteFile();
int i;
FileInputStream fin;
try{
fin=new FileInputStream(wybrany.getAbsolutePath());
}
catch(Exception e){
System.out.println("Błąd podczas otwierania pliku");
return;
}
try{
do{
i=fin.read();
if(i!=-1) this.jTextArea1.setText(this.jTextArea1.getText()+(char) i);
}while(i!=-1);
}
catch(Exception e){
}
}
}
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
JFileChooser wybierz=new JFileChooser();
int powrot=wybierz.showSaveDialog(this);
if(powrot==JFileChooser.APPROVE_OPTION){
try{
FileOutputStream plik=new FileOutputStream(wybierz.getSelectedFile().getAbsolutePath());
String a=this.jTextArea1.getText();
for(int i=0;i<a.length();i++){
plik.write((int) a.charAt(i));
}
}
catch(Exception e){
System.out.println("Błąd podczas zapisu");
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Okno().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}