Results 1 to 5 of 5
  1. #1
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default Output to a text file

    Hi guys,
    I know how to output to a text file like the following:

    FileOutputStream out;
    PrintStream p;
    out = new FileOutputStream("myfile.txt");
    p = new PrintStream( out );
    p.println ("This is written to a file");
    but how do I output my linkedList to a text file?

    thanks :)

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, in this code you just write a string into a text file. So what you have to do is get the linked list elements as a string.

    Did you read more about class methods in LinkedList?

    Java Code:
    public Object get(int index)

  3. #3
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    you can iterate through your list then print it

    example:

    List<String> nameList = new LinkedList();

    namelist.add("name1");
    namelist.add("name2");
    namelist.add("name3");

    for (String name : nameList)
    {
    p.println (name);
    }
    It's easy to write a code that computers can understand...
    ... the challenge is to write a code that humans can understand

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    Thanks for your message.
    The thing is that I am not using java's list. I have my own Linked list class.
    I'm sorry if my question may be stupid but I'm not familiar with working with files at all!
    shouldn't this work now?
    FileOutputStream out;
    PrintStream p;
    out = new FileOutputStream("myfile.txt");
    p = new PrintStream( out );
    p.theList.displayList();

    theList is an object of my class List and displayList() is a method that goes through all the list and prints it all.

    I appreciate your help on this :)

Similar Threads

  1. Replies: 3
    Last Post: 02-16-2009, 09:20 PM
  2. Replies: 7
    Last Post: 11-19-2008, 02:09 PM
  3. Centering text of output
    By dch414 in forum New To Java
    Replies: 2
    Last Post: 10-02-2008, 10:08 PM
  4. Text Output for a library
    By dream_noir in forum Advanced Java
    Replies: 0
    Last Post: 04-14-2008, 05:31 AM
  5. Simply output the result to a text file.
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •