1

looked through a lot of similar subjects, couldn't find something that works for me :/

All I want to do is to write every element of an array to a text file.

    Arrays.toString(array);

That code gives me every element in one line and also brackets around the whole list of elements. Thats not what I want.

The elements of the array are only text. For example: this is a test this is testline number 2

The array here is btw already a string array. String[] array = new String[5];

My code so far is following:

    public void dateiausgeben() throws NullPointerException {
    try {

        FileWriter fw = new FileWriter("neueDatei.txt");
        BufferedWriter bw =  new BufferedWriter(fw);

        for (int i = 0; i < array.length;i++) {
            //String str = array[i].toString();
            //System.out.println("array 0 ="+array[0]+"\n array 1 ="+array[1]+"\n array 2= "+array[2]+"\n array 3="+array[3]);

            bw.write(array[i].toString());

        }
        bw.close();
    }
    catch (IOException e){
        e.printStackTrace();
    }

}

I tried also other stuff out. I also want to keep it as simple as possible.

Stack Trace

    Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Unknown Source)
at Kapitel3_3.readfile.dateiausgeben(readfile.java:61)
at Kapitel3_3.DateiKopierer.main(DateiKopierer.java:15)
2
  • What is the output you would like to receive? Something like this: System.out.println("array 0 ="+array[0]+"\n array 1 ="+array[1]? Do you need those element names 'array 0='? or do you need to remove braces Arrays.toString(array);? Commented Aug 20, 2014 at 0:18
  • If my array elements would be "blub" "this is a test" and "mordor". Then I want thos elements written as they are, each in a new line. Arrays.toString(array) though has an output like this : [blub,this is a test,mordor] which adds commas and brickets and its not in a new line. Hope you get what I mean since my english is not perfect. Commented Aug 20, 2014 at 0:25

1 Answer 1

2
bw.write(array[i].toString());

Change that to

bw.write(array[i]);

and add

bw.newLine();

after that.

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

8 Comments

That should solve the new line problem. But the code throws me an execption and doesn't write the content of my array in the new file. Exeception is : java.lang.NullPointerException
(1) That's a new question; (2) you would have to provide a stack trace, but really (3) you should be able to sort out your own NPEs. Maybe an array element is null? You should really just remove the .toString() call.
Yes, the last to array elements are "null". I wanted to figure that out too after I worked out the problem right now. But maybe the null array element is the reason for my actual problem. I will do some tests. If I remove the .toString() call it throws my another exception saying : java.io.Writer.write(Unknown Source)
Oh come on. You can't get anywhere without naming the exception and posting the stack trace.
You were right, with a manually filled array, it works perfectly, even without the .toString() call. I guess I have have to figure out, why my Filereader in the other part of my program, writes "null" in some elements. Thanks a lot. I will open a diffrent thread if I cant figure it out by myself.
|

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.