2

So Im trying to get a basic reader going so that I can work with files for an authentication process later.

The problem I am having is that I get an error on my BufferedReader line that causes my try function to throw an illegal start exception and it wont run. Eclipse is showing me an error on the semicolon at the end of the br declaration and says I should be putting a { but I can't see why that would be neccessary.

BufferedReader br = new BufferedReader(new FileReader("Assign4.txt"));

I have tried to put that there but it breaks the entire try section.

package main;

import java.io.*;

public class file_interface
{
    BufferedWriter wr = new BufferedWriter(new FileWriter("target.txt"));
    BufferedReader br = new BufferedReader(new FileReader("Assign4.txt"));

        try 
        {
            int count = 1;  
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();            
            while (line != null) 
            {
                sb.append(count++);
                sb.append(line);
                sb.append("\n");
                wr.write(line);
                line = br.readLine();    
            }
        } 
        catch (IOException e) 
        {
            System.err.println("Error: " + e);
        }

        finally
        {
            br.close();
            wr.close();
        }
}
}
Taylor
  • 405
  • 3
  • 8
  • 20

1 Answers1

6

Any Java sentence must be inside a method. This code is not.

The fact that BufferedWriter wr = new BufferedWriter(new FileWriter("target.txt")); works is because is declared as a default field (no scope mark was given) in your file_interface class and is being initialized. It is similar to do:

public class file_interface {
    BufferedWriter wr;
    public file_interface() {
         wr = new BufferedWriter(new FileWriter("target.txt"));
    }
}

Just create a method to hold your logic:

public class file_interface {
    public void foo() {
        //your code goes here...
        //now this is not a field but a variable in the method
        BufferedWriter wr = new BufferedWriter(new FileWriter("target.txt"));
        BufferedReader br = new BufferedReader(new FileReader("Assign4.txt"));
        //rest of your code...
        try  {
             //...
        } catch (...) {
             //...
        }
        //...
    }
}

Then just call your method in your client class. For example, a class with the main method:

public class AMainClass {
    public static void main(String[] args) {
        file_interface fi = new file_interface();
        fi.foo();
    }
}

Another example, a class with another method:

public class SomeClientClass {
    public void bar() {
        file_interface fi = new file_interface();
        fi.foo();
    }
}

Note: You should also follow the Java Naming Conventions, so you should rename file_interface by FileInterface or even MyFileUtil since interface word sounds more to declare an, uhm, interface.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 1
    Or inside a static block. – atk Nov 20 '13 at 17:53
  • Maybe instead of `foo` better suggestion would be `main` method (unless `foo` returns something). Anyway +1. – Pshemo Nov 20 '13 at 17:54
  • @atk that would be great if you want to shoot at your feet. – Luiggi Mendoza Nov 20 '13 at 17:54
  • @LuiggiMendoza: non-sequitor. Java allows you to write code in static blocks, so that you can do stuff when loading a class definition. You may personally dislike static blocks, but they definitely have a use. – atk Nov 20 '13 at 17:55
  • 1
    @atk the fact that you can doesn't mean you should. – Luiggi Mendoza Nov 20 '13 at 17:57
  • @LuiggiMendoza: do you have an actual objection to using static blocks? Or do you simply prefer tilting at windmills and strawmen? I never said that they are useful for all purposes, but that they do have their uses. – atk Nov 20 '13 at 18:05
  • 1
    A static block would not be an appropriate solution to the OP's application which is basically copying a file. A static block is a great thing you can do sometimes but not an answer to this question. – Radiodef Nov 20 '13 at 18:08
  • Yeah that made sense, it was the fact that the first one didn't throw an error that messed me up. Thanks – Taylor Nov 20 '13 at 18:12
  • @atk I have no need to answer to your comment when there's a Q/A here that solves this: [Java: when to use static methods](http://stackoverflow.com/q/2671496/1065197). – Luiggi Mendoza Nov 20 '13 at 19:09
  • @LuiggiMendoza: thank you for your answer to my comment as well as the link to the discussion :) – atk Nov 20 '13 at 20:08