• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

A simple java program for file operation.

Sorry a newbie question....
I have an exercise folder and inside that 2 java programs as follows.....

First one is merge.java as follows,

import java.io.*;
import exc.*;
class merge
{

      public static void main(String[] args) throws excep1
      {
            System.out.println("Exercise for merging file1 and file2 into result !");
            
            try
            {
                  FileOutputStream fos = new FileOutputStream("result.txt", true);
                  DataOutputStream dos = new DataOutputStream(fos);

                  FileInputStream fis1 = new FileInputStream("file1.txt");
                  DataInputStream dis1 = new DataInputStream(fis1);
                  String s = "";
                  int count = 0;
                  while ((s = dis1.readLine()) != null)
                  {
                        s = s + "\n";
                        count++;
                  }
                  if (count >= 5)
                        
                  {
                        dos.writeBytes(s);
                        s="";
                        count=0;
                  }
                  else

                  {
                        throw new excep1("Less than 5 lines not allowed in file1");
                  }

            FileInputStream fis2 = new FileInputStream("file2.txt");
            DataInputStream dis2 = new DataInputStream(fis2);
                  while((s = dis2.readLine()) != null)
                  {
                        s = s + "\n";
                        count++;
                  }
                  if (count >= 5)
                        
                  {
                        dos.writeBytes(s);
                        s="";
                        count=0;
                  }
                  else

                  {
                        throw new excep1("Less than 5 lines not allowed in file2");
                  }

            }

            catch (Exception e)
            {
                  e.printStackTrace();
            }
      }
}


And another excep1.java file....

package exc;
public class excep1 extends Exception
{
      public excep1(Exception e)
      {
            e.printStackTrace();
      }

}


So what I want is.... just throw my user defined exception when less than 5 lines and print it in excep1 class. Remember I dont want system exception to be printed, rather just to print my message alone !

I compiled using "javac *.java" and I'm getting compile errors. Anything wrong in the program? Please correct it if I'm wrong.

Thanks.


0
gopikrish
Asked:
gopikrish
  • 5
  • 4
1 Solution
 
hoomanvCommented:
The .java file has to be within the correct directory structure, defined by
its package, for compilation

so excep1.java must be in directory exc
and then just javac Merge.java
0
 
hoomanvCommented:
also you need to add these lines to excep1

      public excep1(String msg) {
            super(msg);
      }

because in the code you are attempting to construct it by String parameters
0
 
gopikrishAuthor Commented:
Ok fine... On execution the following error is shown,

Exercise for merging file1 and file2 into result !
java.lang.NullPointerException
        at java.io.DataOutputStream.writeBytes(Unknown Source)
        at merge.main(merge.java:27)

Anything wrong ?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
gopikrishAuthor Commented:
Well anyone can help me please?
0
 
hoomanvCommented:
import java.io.*;
import exc.*;
class merge
{

     public static void main(String[] args) throws excep1 {
          System.out.println("Exercise for merging file1 and file2 into result !");
         
          try {
                     StringBuilder content = new StringBuilder();
                     String line;
               int count = 0;
                    
               FileOutputStream fos = new FileOutputStream("result.txt", true);
               DataOutputStream dos = new DataOutputStream(fos);

               FileInputStream fis1 = new FileInputStream("file1.txt");
               DataInputStream dis1 = new DataInputStream(fis1);
               
               while ((line = dis1.readLine()) != null) {
                              content.append(line);
                    content.append("\r\n");
                    count++;
               }
                     dis1.close();
                    
               if (count >= 5) {
                    dos.writeBytes(content.toString());
                              content.delete(0, content.length());
                    count=0;
               }
               else {
                              dos.close();
                    throw new excep1("Less than 5 lines not allowed in file1");
               }

                    FileInputStream fis2 = new FileInputStream("file2.txt");
                    DataInputStream dis2 = new DataInputStream(fis2);
               while((line = dis2.readLine()) != null) {
                    content.append(line);
                    content.append("\r\n");
                    count++;
               }
                     dis2.close();
                    
               if (count >= 5) {
                    dos.writeBytes(content.toString());
                    count=0;
               }
               else {
                              dos.close();
                    throw new excep1("Less than 5 lines not allowed in file2");
               }

                        dos.close();
          }
          catch (Exception e) {
               e.printStackTrace();
          }
     }
}
0
 
hoomanvCommented:
1)
> java.lang.NullPointerException

it was because of this line
while ((s = dis1.readLine()) != null)
at the last line s will become null
also you have to store the read lines some where else than s because s will be changed in each call to s = dis1.readLine()
I changed it to StringBuilder as it is more efficient than just saying s += line + "\n";

2)
make sure you close the open OutputStreams when you dont need them any more

0
 
gopikrishAuthor Commented:
Nice !
Now its writing to result.txt

But when my file1 is having 4 lines and when I executed my program, the following is getting displayed....

Exercise for merging file1 and file2 into result !
Less than 5 lines not allowed in file1
exc.excep1
        at merge.main(merge.java:37)

I just want my message alone to be printed and dont want system message to be printed. That is I want "Less than 5 lines not allowed in file1" and not "exc.excep1
        at merge.main(merge.java:37)"

My excep1 class is as follows....
package exc;
public class excep1 extends Exception
{
      public excep1(String msg) {
                    System.out.println(msg);
  }
}

How to make it please ?
Thanks.
0
 
hoomanvCommented:
you dont need to throw any exception for this purpose
a Sysmte.out.println("Less than 5 lines not allowed in file2"); would be enough
but if you are forced to throw exception
just change this line
e.printStackTrace();
to
System.out.println(e.getMessage());
0
 
gopikrishAuthor Commented:
ok thanks a lot !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now