1

I have created the following to code to display a file contents to a text area - and it is successful:

BufferedReader in = null;
try {
    in = new BufferedReader(new FileReader("C:\\Users\\emily\\Documents\\Parts.txt"));
    String str;
    while ((str = in.readLine()) != null) {
        jTextArea1.append("\n"+str);
    }
} catch (IOException e) {
} finally {
    try { in.close(); } catch (Exception ex) { }
}

My problem is that I need each line in an array. The idea for my proof of concept is that I would be able to enter the command :

jTextArea1 = Arrays.toString(fileArray);
The output should then be: [Part1, Part2, Part3]

In spite of my scouring of the internet, I can't seem to accomplish this. Can anyone tell me how to load these values into an Array (fileArray) instead of writing them into the jTextArea?

2
  • Are you open to adding libraries such as OpenCSV or Commons IO? Commented Dec 1, 2015 at 20:14
  • This is the first time I've worked with Java - kinda got thrown into it when someone in Engineering quit, so I'm not sure what that means, but if it helps, I'm open to anything... Commented Dec 1, 2015 at 20:54

2 Answers 2

2

The newer classes Path and Files will do.

Path path = Paths.get("C:\\Users\\emily\\Documents\\Parts.txt");
try {
    List<String> lines = Files.readAllLines(path, Charset.defaultCharset());
    String[] array = lines.toArray(new String[lines.size()]);
    ...
} catch (IOException e) {
    //Logger.getLogger(getClass().getName()).log(Level.SEVERE,
    //                                           path.toString(), e);
    System.err.println("Could not read file: " + e.getMessage());
}

Best would be to use just the List.


Add the handling of a possible IOException, probably when the file could not be read.

Sign up to request clarification or add additional context in comments.

4 Comments

This seems like it would be easiest, but I get the following error when I try to complie: "Uncompilable source code - type java.awt.List does not take parameters"
You must import java.util.List - you got the java.awt.List. See List
Uncompilable source code - unreported exception java.io.IOException; must be caught or declared to be thrown ??? (Apologies - this is the first time I've worked with Java at my office..)
Figured it out! I was trying to display the string...not the array :) Works perfectly! Thank you!
0

You can try this:

BufferedReader in = null;
try {
    in = new BufferedReader(new FileReader("C:\\Users\\emily\\Documents\\Parts.txt"));
    String str;
    ArrayList<String> temp = new ArrayList<String>();
    String[] output;
    while ((str = in.readLine()) != null) {
        temp.add(str);
    }
    output = temp.toArray(new String[temp.size()]);
} catch (IOException e) {
} finally {
    try { in.close(); } catch (Exception ex) { }
}

2 Comments

So how would I show the array in the jTextArea? I tried: jTextArea2=Arrays.toString(output); but it doesn't work. IDE suggests creating the local variable "output"...
@user3508197 output is probably not accessible in the scope you are trying to access it in...Make it a global variable.

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.