First off I'm still pretty noobish with Java, so I imagine this can be answered rather easily.
I have a program that, when first launched, looks for a file called location.txt in a specified location (AppData folder on C drive) if this file does not exist, then it'll be created. The file will end up having ONLY a filepath that's selected from a JFileChooser.
I want my program to read the filepath in this text file so that I don't have to statically reference file locations. I'm now having a problem, though. Here's some code:(Don't mind the poor code spacing, stackoverflow is being difficult for me)
BufferedReader bufferedReader = new BufferedReader(fileReader); // creates the buffered reader for the file
java.util.List<String> lines = new ArrayList<String>(); //sets up an ArrayList (dynamic no set length)
String line = null;
try {
while ((line = bufferedReader.readLine()) != null) { // reads the file into the ArrayList line by line
lines.add(line);
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error 16: "+e.getMessage()+"\nPlease contact Shane for assistance.");
System.exit(1);
}
try {
bufferedReader.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error 17: "+e.getMessage()+"\nPlease contact Shane for assistance.");
System.exit(1);
}
String[] specifiedLocation = lines.toArray(new String[lines.size()]); // Converts the ArrayList into an Array.
String htmlFilePath = specifiedLocation + "\\Timeline.html";
File htmlFile = new File(htmlFilePath);
JOptionPane.showMessageDialog(null, specifiedLocation);
JOptionPane.showMessageDialog(null, htmlFilePath);
What I don't understand is why, when the message dialog for the specified location pops up, the filepath is there PERFECTLY. However, when the message dialog for the htmlFilePath pops up, it looks like this:
[Ljava.lang.String;@1113708\Timeline.html
Any help is very much appreciated!
EDIT: I figured it out.. slams head on desk I'm trying to get it to look at an array and not specifying which one. Bad code practice, I know, but the simple fix was to take this:
String htmlFilePath = specifiedLocation + "\Timeline.html";
to
String htmlFilePath = specifiedLocation[0] + "\Timeline.html";
Sorry for the silly post...
The toString
method of Array
s are not overrided. It will return you a String
representation of the array (which is what toString
in Object
does), which includes the type of the object (the array) + "@" + its hashcode.
If you want the output to be better, use Arrays.toString
instead. But that still gives you the [
s, so loops based ad-hoc solutions are probably better. Also, if you are concatenating the String
s, use StringBuilder
can be faster.
Also see (shameless promotion) my answer to another question.