0

Alright I am very new to Java and am trying to develop an application to teach myself how to use the language.

I have been copying and pasting the same few lines of code all over, and I know that there is a way to consolidate this into a function, but cannot quite figure it out.

 FileOutputStream fout4 = openFileOutput("building1hourly.txt", MODE_WORLD_READABLE);
 OutputStreamWriter osw4 = new OutputStreamWriter(fout4);
 osw4.write("" +iHourlyAfter);
 osw4.flush();
 osw4.close();

Now isn't there some type of way I could do something like this

public void writerFunction("What to write to file", "name stream", "name writer", "MODE"){insert above code here}
3
  • 4
    yes, just put all these lines inside your function and replace the actual values with the argument names. what's the problem ? Commented Mar 29, 2011 at 1:27
  • You already answered it yourself. Commented Mar 29, 2011 at 1:33
  • 1
    A better way to teach yourself the Java language is to do the Oracle Java tutorial, or read a good Java textbook. If you try to learn a language by leaping in and trying to develop something, you are liable to pick up a lot of misconceptions and bad habits. Commented Mar 29, 2011 at 1:37

2 Answers 2

4

Yes absolutely:

public void writeToFile(String fileName, String contents, int mode) throws IOException {
    FileOutputStream fout = openFileOutput(fileName, mode);
    OutputStreamWriter osw = new OutputStreamWriter(fout);
    osw.write(contents);
    osw.flush();
    osw.close();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I am just trying to make sure I have the concept down right. So if I have that function all I would need to do is: writeToFile(String fileName, String contents, int mode);
actually writeToFile("building1hourly.txt", "" + iHourlyAfter, MODE_WORLD_READABLE); would do the trick if what you mean is to call function (actually in Javaland we call them methods)
1

First of all, great job so far. Learning programming is just like learning math (except more fun), you can read about it all you want in a book, but you don't really understand concepts until you DO them. You're going about this the right way.

Now, to answer your question: Yes, you can encapsulate the process of writing to a file in a function. Let's call it writeToFile. You want to "call" this function by sending it arguments. The arguments are the information that the function needs to do its work.

There are two sides to a function: the declaration, and the invocation. Just like in math, you can define a function f(x), where f does something. For example: say I have the function f(x) = 2x - 4. That equation is what we call the function declaration, in that we are defining what f does, and you are defining the parameters that it accepts, namely a single value x. Then you want to apply that function on a certain value x, so you might do something like: f(4). This is the function invocation. You are invoking, or calling the function, and sending 4 as the argument. The code that invokes a function is called the caller.

Let's start with the declaration of the function that you want to build:

public void writeToFile (String data, String fileName)

This function defines two parameters in its signature; it expects a String containing the data you will write to the file, and the fileName to which we will write the data. The void means that this function does not return any data back to the caller.

The complete function, the body of which you provided in your post:

public void writeToFile (String data, String fileName){
    FileOutputStream fout4 = openFileOutput(fileName, MODE_WORLD_READABLE);
    OutputStreamWriter osw4 = new OutputStreamWriter(fout4);
    osw4.write("" +iHourlyAfter);
    osw4.flush();
    osw4.close();
}

Now you will want to call, or invoke this function from somewhere else in your code. You can do this like so:

writeToFile("stuff I want to write to a file", "myFile.txt");

2 Comments

This is very helpful, and makes more sense when I think of it in "f(4)" terms. Thanks for the explanation.
If you like programming, then get good at it and keep practicing. It pays very well ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.