انا عندى كلاس كنت عامله بستخدمه فى اى مشروع لما اكون محتاج احفظ الحاجة اللى انا كاتبها,كنت عامل Notepad قبل كده وعملت الكلاس ده وشغال كويس لكن انا شغال فى مشروع تانى دلوقتى وحطيت الكلاس ده فى المشروع اللى انا شغال فيه لقيته بيحفظ الكلام اللى انا كاتبه برضه لكن من غير المسافات قلت ارجع اشوف الNotepad لقيته بيحفظ الكلام بالمسافات ومش عارف المشكله فين مع انى فى المشروع اللى انا شغال فيه Interface بتاعه زى Notepad بالظبط ,بجد مش عارف المشكلة فين
import java.io.*;
import javax.swing.*;
import java.awt.*;
public class Saving extends JFrame {
private JFileChooser fileChooser;
private int result;
protected File filename;
protected FileWriter output;
public Saving() {
filename=new File("intial.txt");
}//end Saving Constructor
public void saveFile()
{
fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
result=fileChooser.showSaveDialog( this );
if( result==fileChooser.CANCEL_OPTION )
return;
filename=fileChooser.getSelectedFile();
// display error if invalid
if ( filename == null ||filename.getName().equals( "" ) )
JOptionPane.showMessageDialog( this,"Invalid File Name", "Invalid File Name",
JOptionPane.ERROR_MESSAGE );
openStream();
}//end saveFile method
//method to Open Stream
public void openStream()
{
try {
output = new FileWriter( filename );
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog( this,"Error Opening File", "Error",
JOptionPane.ERROR_MESSAGE );
} catch (IOException ex) {
JOptionPane.showMessageDialog( this,"Error Opening File", "Error",
JOptionPane.ERROR_MESSAGE );
}
}
public int getResult()
{
return result;
}
//To Know if the File exist
public boolean isExist()
{
return filename.exists();
}//end isExist method
//method to closefile
public void closeFile()
{
try {
output.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog( this,"Error Closing File", "Error",
JOptionPane.ERROR_MESSAGE );
}
}
}//end Saving Class