Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I created a virus or what ? [ LOL ]
#1
Iam just a beginner in java and i have been working on my project which is some kind of "File System " and now one funny thing happened Roflmao

whenever iam writing to a file it erases all the text of the file and replaces it with the text written on the text area . try it ->


Code:
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;

import javax.swing.event.*;


public class fine extends JFrame implements ActionListener{
    
    private JMenuBar mu = new JMenuBar();
    private JMenu me = new JMenu("File");
    private JMenuItem muit = new JMenuItem("new");
    private JMenuItem muit2 = new JMenuItem("open");
    private JMenuItem muit3 = new JMenuItem("save");
    private JTextArea tf = new  JTextArea(20 , 20);
    
    String s;
    
    public fine(){
        
        super("the title");
        setLayout(new FlowLayout());
        getContentPane().setBackground(Color.GREEN);
        me.add(muit);
        me.add(muit2);
        me.add(muit3);
        mu.add(me);
        add(mu);
    
        
        JScrollPane scroller = new JScrollPane(tf);  
        JScrollBar bar = new JScrollBar();  
        scroller.add(bar);
        
        add(tf);
        
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(150,100);
        
        
        
        muit2.addActionListener(this);
        muit3.addActionListener(this);
    }
    
    public void actionPerformed(ActionEvent ev){
        
        
        
         s = ev.getActionCommand();
        
        if(s == "new"){
            
            String k = null;
            tf.append(k);
        }
        
        
         if(s == "save"){

                JFileChooser chooser = new JFileChooser();
                
                chooser.showSaveDialog(this);  
                loadFile2(chooser.getSelectedFile());
         }
        if(s =="open"){
            
            JFileChooser chooser = new JFileChooser();
            
            chooser.showOpenDialog(this);  
            loadFile(chooser.getSelectedFile());  
        }
    }
    
        private void loadFile(File file) {  
        
            try {  
                
                FileInputStream fi2 = new FileInputStream(file);
                BufferedReader reader = new BufferedReader(new FileReader(file));  
                String line = null;  
                while ((line = reader.readLine()) != null) {  
                tf.append(line);
                }  
                reader.close();  
      
            } catch(Exception ex) {  
                System.out.println("couldn't read the card file");  
                ex.printStackTrace();  
    }
            
    
              
                    
               }
        
               public void loadFile2(File file){
                  
                  
              
               try {
                   FileOutputStream fo = new FileOutputStream(file);
              
                   BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                              
                      writer.write(tf.getText());
                
                  
                  writer.close();
               }
               catch(Exception e){
               }
              
        }
    public static void main(String args[]){
        
        fine fi = new fine();
        
    }
}






copy the source code run it on netbeans/eclipse
there will be a "TextArea" write anything on it like "blablabla".

then click on the file menu -> then "save" you will get a savedialog box . Open any ".txt" file from your system then click on "save" .
Close the program now check your .txt file your text will be replaced by the text you have just written on the textarea . the text wont be added it will replace the existing text .


ok now wtf ?

i just want to write it on the file rather than replacing the text

what should i do please help me guys !
Reply


Messages In This Thread
I created a virus or what ? [ LOL ] - by prince76 - 02-03-2012, 02:44 PM
RE: I created a virus or what ? [ LOL ] - by Grin - 03-22-2012, 01:59 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)