-1

i want results saved in a file and using recursive method which rewrite the object and nothing save. if i try to declare the printwriter globaly then it give me the error "Default constructor cannot handle exception type UnsupportedEncodingException thrown by implicit super constructor. Must define an explicit constructor" here is the code

public void lengthFind() {
    try {

        writer = new PrintWriter("Results.txt", "UTF-8");
        fileStreamTest obj = new fileStreamTest();
        Scanner s = new Scanner(new File(path));
        int max = 0;

        while (s.hasNextFloat()) {

            int d = (int) s.nextFloat();

            // it filters the time stamp
            if (d <= 5000) {
                String code = obj.GolombCoding(d, dival);

                // finding Max length
                if (code.length() > max) {
                    max = code.length();
                }
            }

        }

        // Dividend Limit check or increase the Dividend
        if (dival == 10) {
            writer.println("Divident has reached it Limit !");
            i++;
            // update the file name
            path = "D:/File Compression/Data/low_freq/low_freq/house_1/channel_"
                    + i + ".dat";
            dival = 10;

        } else {
            dival = dival + 10;
            writer.print("Dividen:" + dival);
        }
        writer.print("Max Length of File " + i + ": " + max);
        writer.println("Path Of the File : " + path);

        int counter = 1;
        // recall the method
        writer.println("Method Call Counter" + counter);

        lengthFind();

    } catch (IOException e) {
        writer.close();
        System.out.println("There is no more File");
        System.exit(0);

    }
Waqas
  • 315
  • 1
  • 6
  • 21
  • And what is obscure exactly? Have you had a look at the [javadoc of `UnsupportedEncodingException`](http://docs.oracle.com/javase/7/docs/api/java/io/UnsupportedEncodingException.html)? What are you calling "declare globally"? – fge Jul 13 '13 at 19:58
  • by declare globally i mean that i make printwriter object in Class not in method – Waqas Jul 13 '13 at 20:03
  • So, in a static initializer? – fge Jul 13 '13 at 20:06
  • yes and that gives me UnsupportedEncodingException so i can not find a way to write the results in a file. if i try to make object in class it gives the exception and if i make object in method it overwrite the file every time because the method is recursive. – Waqas Jul 13 '13 at 20:10
  • I am sorry, but that does not make it anymore clear in the slightest. Start from the beginning, explain what you want to achieve, how you have tried to achieve and where it fails – fge Jul 13 '13 at 20:24

1 Answers1

1

Look at the javadoc

Throws:

  1. FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file

  2. SecurityException - If a security manager is present and checkWrite(file.getPath()) denies write access to the file

  3. UnsupportedEncodingException - If the named charset is not supported

You need to deal with the Exception which the constructor might throw . If you want to declare the PrintWriter as static then you need to use static initializer block.

static PrintWriter pw ;
static {
   try{
     pw = new PrintWriter("Results.txt", "UTF-8");
     }
     catch(IOException e){
        e.printStackTrace();
        throw new RuntimeException(e);  
    }
}
Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164