Support Forums

Full Version: I created a virus or what ? [ LOL ]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 !
That's nothing like a virus, you can find the definition here; http://en.wikipedia.org/wiki/Virus

But hey, it would be petty funny, yet pointless at the same time. Good luck on getting to your goal.
(02-03-2012, 02:48 PM)BreShiE Wrote: [ -> ]That's nothing like a virus, you can find the definition here; http://en.wikipedia.org/wiki/Virus

But hey, it would be petty funny, yet pointless at the same time. Good luck on getting to your goal.


Yeah I know its not a virus breshie bro common i dont need wikipedia for that Non . Its just like a funny virus Roflmao and my goal is to remove this funny crap Roflmao help me out please bro Sad
When your java opens it, you havnt shown it how to create a new .txt file exactly the same.
This is definitely not even near a virus.
(02-03-2012, 02:48 PM)BreShiE Wrote: [ -> ]That's nothing like a virus, you can find the definition here; http://en.wikipedia.org/wiki/Virus

But hey, it would be petty funny, yet pointless at the same time. Good luck on getting to your goal.

It does however constitute as malware.
"Malware, short for malicious software, is software designed to disrupt computer operation, gather sensitive information, or gain unauthorized access to computer systems. While it is sometimes software, it can also appear in the form of script or code. Malware is a general term used to describe any kind of software or code specifically designed to exploit a computer, or the data it contains, without consent. The expression is a general term used by computer professionals to mean a variety of forms of hostile, intrusive, or annoying software."
Well... It just seems to run on someone else's computer.
Haha. Its not a virus. Its a glitch in your programming.
Firstly, the saving mechanism you stated above will obviously replace the text in the file. You need to open the file in your program and then make changes to it. Whenever you stream out data into a file through java, it will replace the whole file. But if you still want that method then do the following changes in your program.

On clicking the "save" button on the Save dialog, use FileReader to read the existing lines in the text file. Store those into a string array. Then rewrite them into the file using FileWriter. Then take the text from the TextArea and write them into the using FileWriter again.

That's it. If you still have problems with it just reply here. And do note that I get a NullPointerException at function loadFile(). Check that out too.

Best of luck with the coding. Smile

And I just realized that this topic was bumped *sigh*
(03-24-2012, 02:49 AM)aHardyX Wrote: [ -> ]Haha. Its not a virus. Its a glitch in your programming.
Firstly, the saving mechanism you stated above will obviously replace the text in the file. You need to open the file in your program and then make changes to it. Whenever you stream out data into a file through java, it will replace the whole file. But if you still want that method then do the following changes in your program.

On clicking the "save" button on the Save dialog, use FileReader to read the existing lines in the text file. Store those into a string array. Then rewrite them into the file using FileWriter. Then take the text from the TextArea and write them into the using FileWriter again.

That's it. If you still have problems with it just reply here. And do note that I get a NullPointerException at function loadFile(). Check that out too.

Best of luck with the coding. Smile

And I just realized that this topic was bumped *sigh*

Its hard for me to know, posts on the first page can be months old Confused