Reputation: 85
I build this code with the help of this site (mostly copy pest). It's working for me, but I have some questions. The code is:
package dic;
import java.io.*;
import java.util.*;
public class MainDic {
private List<String> lines = new ArrayList<String>();
public String[] readOriginalFile(String filename) throws IOException
{
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
bufferedReader.close();
return lines.toArray(new String[lines.size()]);
}
public static void main(String[] args) throws IOException {
MainDic call = new MainDic();
String filename = "dictionaryTXT.txt";
String[] lines = call.readOriginalFile(filename);
System.out.println("______ORIGINAL DOCUMENT______\n");
for (String line : lines) {
System.out.println(line);
}
}
}
I don't understand these lines:
public String[] readOriginalFile(String filename) throws IOException
return lines.toArray(new String[lines.size()]);
String[] lines = call.readOriginalFile(filename);
Upvotes: 1
Views: 73
Reputation: 2321
1) A method signature, returning a String array
called readOriginalFile
with a parameter of the file name to be read. It throws
an IOException
if there is any error in processing the file, instead of catching
any potential exceptions
2) Takes the input line, converts it into a String array
and returns it from the method
3) Calls the method defined in 1
, taking the String array
that is returned in question 2
and assigns it to a new String array
Does that clarify?
Considering this is a "copy pest" job, I think you should look at the language basics... such as a HelloWorld example. Start at the sun tutorials
http://docs.oracle.com/javase/tutorial/getStarted/index.html
And
http://docs.oracle.com/javase/tutorial/java/index.html
Upvotes: 1
Reputation: 14286
I don't understand these lines:
- public String[] readOriginalFile(String filename) throws IOException
- return lines.toArray(new String[lines.size()]);
- String[] lines = call.readOriginalFile(filename);
Well let's break these down starting with:
1.
public String[] readOriginalFile(String filename) throws IOException
This is just the header for the method readOriginalFile
. It is a public
method meaning everyone can see it. It returns an array of String (String[]
). And it may throw
an exception called IOException
. Read more about that here.
2.
return lines.toArray(new String[lines.size()]);
This takes the File
object called lines
and turns it the lines of the file into an array of Strings (String[]
).
3.
String[] lines = call.readOriginalFile(filename);
This is just creating an instance of a String array by calling the method written above (readOriginalFile
).
I highly recommend reading a Java methods tutorial.
Upvotes: 2