-8
  • I want the text file to be in a particular format which is really what I am getting at.

I am currently looking for a way to create a new text file that is created by using the values from an ArrayList. How do I create a text file that will allow me to have the format that I am looking for?

5
  • 2
    show the code that you tried Commented Mar 22, 2019 at 14:32
  • 1
    1. Search on google how to iterate over a List. 2. Search on google how to write to a file. Commented Mar 22, 2019 at 14:33
  • 1
    Possible duplicate of How to write an ArrayList of Strings into a text file? Commented Mar 22, 2019 at 14:33
  • There are plenty of ways how you can do it, show what you have tried. Commented Mar 22, 2019 at 14:33
  • "to have the format that I am looking for" Which format? Commented Mar 22, 2019 at 14:44

1 Answer 1

1

public static void createTextFileFromArrayList() {

    List<String> cities = new ArrayList<String>();
    cities.addAll(Arrays.asList("Boston", "Washington", "Irving", "Dallas"));

    //Get the file reference
    Path path = Paths.get("C:\\apps\\output.txt");

    //Use try-with-resource to get auto-closeable writer instance
    try (BufferedWriter writer = Files.newBufferedWriter(path)) {
        cities.forEach(city -> {
            try {
                writer.write(city);
                writer.write("\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.