[SOLVED]Can't write to file when using for loop

Discussion in 'Plugin Development' started by CRAZYxMUNK3Y, Jul 12, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    CRAZYxMUNK3Y

    For a plugin i am updating/adding on to i need to find a way to add all timezones into a file so server admins/owners can find it easier.

    This is my code;
    Code:java
    1.  
    2. FileWriter writer = null;
    3.  
    4. String[] tzID = TimeZone.getAvailableIDs();
    5. String nl = System.getProperty("line.separator");
    6. File timezoneFile = new File("C:\\timezones.txt");
    7. for (int i = 0; i < tzID.length; i++) {
    8. try{
    9. writer = new FileWriter(timezoneFile);
    10. writer.write(tzID + nl);
    11. }catch (Exception e){
    12. e.printStackTrace();
    13. }finally{
    14. try {
    15. if(writer != null){
    16. writer.close();
    17. }
    18. } catch (IOException e) {
    19. e.printStackTrace();
    20. }
    21. }
    22. }
    23.  


    But all that shows in the file is this;
    Code:
    [Ljava.lang.String;@5b86d4c1
    
    I am only running it directly from eclipse and not on a server so i don't know of any errors.

    What have i done wrong?
    Thanks
     
  2. try{
    writer = new FileWriter(timezoneFile);
    for (int i = 0; i < tzID.length; i++) {
    writer.write(tzID + nl);
    }
    }catch (Exception e){
    e.printStackTrace();
    }finally{
    try {
    if(writer != null){
    writer.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    Something like that ^
     
  3. Offline

    CRAZYxMUNK3Y

    Thanks for the reply, but it didn't work..
    It just repeated the same line([Ljava.lang.String;@5b86d4c1) 615 times
     
  4. replace
    Code:java
    1. writer.write(tzID + nl);

    whit
    Code:java
    1. writer.write(tzID[i] + nl);[/i]

    at the code I given your before
     
    CRAZYxMUNK3Y likes this.
  5. Offline

    CRAZYxMUNK3Y

    Thankyou! Worked perfectly.
     
Thread Status:
Not open for further replies.

Share This Page