Results 1 to 4 of 4
  1. #1
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Writing into a text file

    Please help, I am trying to write the contents of an arraylist to a text file but for some reason nothing happens. When I open the text file it is completely empty. Can someone please help me find out why this is. Many thanks in advance.


    Java Code:
    public void writeToFile(){
    		for (String s : linksArray){
    			try{
    			fileOut = new FileOutputStream ("newsDayLinks.txt");
    			new PrintStream(fileOut).println (s);
    			
    
    			}catch(IOException e){
    				System.err.println ("Unable to write to file");
    				System.exit(-1);
    			}			
    		}//for-each ends
    		try {
    			fileOut.close();
    		} catch (IOException e) {			
    			e.printStackTrace();
    		}
    	}//writeToFile method ends

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try initializing the writers before the loop, then in the loop just directly call println(s).

    Also, Are you using this method correctly?

  3. #3
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Sorry about that, it is actually working. There was a problem with my directory structure

  4. #4
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  2. writing to text file problem
    By blumdiggity in forum Networking
    Replies: 1
    Last Post: 02-26-2010, 02:43 PM
  3. writing to specific line in text file
    By mickmos in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 01:01 PM
  4. Writing To A Specific Text File Line
    By mokonji in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 08:13 PM
  5. writing text to file
    By notwist in forum New To Java
    Replies: 3
    Last Post: 04-25-2008, 04:20 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
  •