Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: file output

  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    file output

    hello, im having trouble with a code, this is about file out put and copying lines from one file to two different ones, basically im given a file with about 100 lines of numbers and on some of the lines contain the word attack. my job is to create a program that lets the user choose the file to scan and then indicate two more files, one for the attack lines to go and one for the clean lines to go by copying them. im new to java and this is what i have so far:

    Code:
     import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;
    import javax.swing.JFileChooser;
    
    
    public class FileChooserDemo 
    {
       public static void main(String[] args) throws IOException
       {
          JFileChooser chooser = new JFileChooser();
          Scanner in = null;
          if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
          {
             File selectedFile = chooser.getSelectedFile();
             in = new Scanner(selectedFile);
             int lineNumber = 1;
             final int MAX_LINES = 100;
             PrintWriter out = new PrintWriter("corrupt.txt");
             out = new PrintWriter("clean.txt");
                while (in.hasNextLine() && lineNumber <= MAX_LINES)
                {
                    String line = in.nextLine();
                    System.out.println(lineNumber + ": " + line);
                    lineNumber++; 
                    if(line.contains("ATTACK"))
                    {
                       out.print(line);
                       //??
                    
                    }
                    else
                    {
                        out.print(line);
                        //??
                    }
                }         
                 if (in.hasNextLine()) 
                 { 
                     System.out.println("..."); 
                 }
          }
       }
    basically this so far lets the user choose the file and opens it up and displays it. i have looked up some sites online on similar stuff but i still find myself lost from what i was taught.thanks!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The biggest problem I see right off the bat is you only have one output source: clean.txt. Since you'll be writing to two files, you should use two output sources so you can choose which to write down.
    Don't forget to close all of the streams when complete.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    The biggest problem I see right off the bat is you only have one output source: clean.txt. Since you'll be writing to two files, you should use two output sources so you can choose which to write down.
    Don't forget to close all of the streams when complete.
    aye yes my bad i have fixed that i think the biggest problem I'm having now is copying the lines with attack into the corrupt file...sorry I just started learning java.

  • #4
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it is not scanning all the lines iv tried a while loop but then it doesn't even scan the first line.
    Code:
    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;
    import javax.swing.JFileChooser;
    
    /**
       This program shows the first nine lines of a file that was
       selected with a file chooser.
    */
    public class FileChooserDemo 
    {
       
       public static void main(String[] args) throws IOException
       {
          JFileChooser chooser = new JFileChooser();
          Scanner in = null;
          if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
          {
             File selectedFile = chooser.getSelectedFile();
             in = new Scanner(selectedFile);
             int lineNumber = 1;
             final int MAX_LINES = 100;
             boolean done = true;
             PrintWriter attack  = new PrintWriter("corrupt.txt");
             PrintWriter clean = new PrintWriter("clean.txt");
             while (in.hasNextLine() && lineNumber <= MAX_LINES)
             {
                String line = in.nextLine();
                System.out.println(lineNumber + ": " + line);
                lineNumber++;
    
                    if(line.contains("attack"))
                    {
                        
    
                        attack.printf(line);
                        attack.close();
                        
                        
                        
                    }
                    else
                    {
                        
                        clean.printf(line);
                        clean.close();
                        
                        
                    } 
                    
             }
             
             if (in.hasNextLine()) 
             { 
                 System.out.println("..."); 
             }
             
          }
          
       }
    }

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That looks like it would work not badish.
    Except. . . you may be closing those streams a tad early. Think of the indenting as a tree; you want to close the streams on the same branch you have opened them on.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •