1

Sorry if this question has been asked before, but I'm new to coding and I can't find an answer online because I don't know the theory well enough to know how to describe what I'm looking for.

Basically, I want to know if theres a way I can initialize a variable/macro that I can tie to this long try statement, so instead of writing THIS every time I want to read my file

System.out.println("filler");
System.out.println("filler");
try {
    FileReader reader = new FileReader("MyFile.txt");
    BufferedReader bufferedReader = new BufferedReader(reader);

    String line;

    while ((line = bufferedReader.readLine()) != null) {
        System.out.println(line);
    }
    reader.close();

} catch (IOException e) {
    e.printStackTrace();
}
System.out.println("filler");
System.out.println("filler");

I can just write something like..

System.out.println("filler");
System.out.println("filler");
Read1
System.out.println("filler");
System.out.println("filler");
hensing1
  • 187
  • 13
  • 1
    This is usually provided by your IDE. In NetBeans you would define this in Tools -> Options -> Editor -> Code Templates –  Feb 01 '19 at 07:15
  • 4
    Are there any restrictions that prevent you from making it a normal method? – king_nak Feb 01 '19 at 08:45

2 Answers2

1

As @king_nak suggests, use a method.

public void readFile(String path) {
    try {
        FileReader reader = new FileReader(path);
        BufferedReader bufferedReader = new BufferedReader(reader);

        String line;

        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();

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

And then you can do what you wanted:

    System.out.println("filler");
    readFile("MyFile.txt") // call the method
    System.out.println("filler");
Soutzikevich
  • 991
  • 3
  • 13
  • 29
  • 1
    Just a small observation here... A **method** is not a **function**, and a function is not a method. Please refer to [What's the difference between a method and a function](https://stackoverflow.com/a/155655/8075923) for a detailed answer. Please edit your solution, so that it contains correct information. – Soutzikevich Feb 01 '19 at 14:48
0

In Java, MACROS are called constants instead, and are initialised with the final keyword.

For having a String constant for example:

final String str = "Hello World!";

What you need here, is a good ol' fashioned Java method.

You need to declare it outside your main method, in a class of your choosing. What the following method will do, is that it will read a file and add each line of the file to a list (an ArrayList more specifically).

Each element of the ArrayList is one line of text, read from the file.

Note: This method is quite advanced, since it utilises streams to achieve what was described above. If you use this, then please spend some time to understand it first! Otherwise, I would suggest you don't use this as a beginner. (You can read the documentation for for Reading, Writing and Creating files).

public ArrayList<String> readLines (String filename){
    ArrayList<String> lines = null;

    // Get lines of text from file as a stream.
    try (Stream<String> stream = Files.lines(Paths.get(filename))){
        // convert stream to a List-type object
        lines = (ArrayList<String>)stream.collect(Collectors.toList());
    }
    catch (IOException ioe){
        System.out.println("Could not read lines of text from the file..");
        ioe.printStackTrace();
    }

    return lines;
}

Then you can use the method like so:

ArrayList<String> lines = null; //Initialise an ArrayList to store lines of text.

System.out.println("filler");
lines = readLines("/path/myFile.txt");
System.out.println(lines.get(0)); //Print the first line of text from list
System.out.println("filler");

lines = readLines("/path/myOtherFile.txt");

for( String str : lines )
    System.out.println(str); //Will print every line of text in list

Here is the link for the documentation for java.nio.Files.

Soutzikevich
  • 991
  • 3
  • 13
  • 29
  • Thank you for explaining the terminology! I feel like I have a much better grasp on some of the things I'm reading now :p. Ultimately though I think you are right in saying that this is too advanced for beginners. Even if it's the most proper way to do this I think I'll need to do a lot more research before I'm comfortable utilizing a method like this. – Whackpoobrain28 Feb 01 '19 at 23:57
  • @Whackpoobrain28 No worries, you will get there eventually. Practice makes perfect! There are some great beginner tutorials for Java [here](https://docs.oracle.com/javase/tutorial/). I suggest you go through them, as they'll help you improve your skills quickly – Soutzikevich Feb 02 '19 at 01:13