Results 1 to 2 of 2
  1. #1
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Angry Writing To A File

    Hi,

    I am trying to write to a text file in java but whenever I input data it gets deleted. The code I am going to post is a very small sample (just the IO component) but basically the purpose of the IO is whenever someone types something on the cell of my Calendar JTable it saves there and the entry is written to the file.
    Java Code:
    public class CalendarIO {
    
    	static String key;
    	static// static String value;
    	Integer a = 1;
    	static Hashtable<String, String> hash;
    
    	public static void read() {
    
    		// Hashtable hash,String key,String value
    
    		try {
    
    			BufferedReader in = new BufferedReader(new FileReader(
    					"/Users/eric.manget/Desktop/Calendar.txt"));
    			String str;
    
    			String str1;
    			String str2;
    			hash = new Hashtable<String, String>();
    			while ((str = in.readLine()) != null) {
    				int k = str.indexOf(';');
    				if (k > 0) {
    					str1 = str.substring(0, k);
    					str2 = str.substring(k + 1);
    
    					// a = a + 1;
    					// parse str into 2 parts:
    					// first is date, which is our key
    					// separated by ;
    					// second part is the entry, which is our value
    					hash.put(str1, str2);
    				}
    
    			}
    
    		} catch (FileNotFoundException e) {
    
    		} catch (IOException e) {
    
    		}
    
    	}
    
    	public static Hashtable<String, String> getHash() {
    		if (hash == null)
    			return new Hashtable<String, String>();
    		else
    			return hash;
    	}
    
    	public static void write() {
    		try {
    			
    			BufferedWriter out = new BufferedWriter(new FileWriter("/Users/eric.manget/Desktop/Calendar.txt",true));
    			FileOutputStream fop=new FileOutputStream("/Users/eric.manget/Desktop/Calendar.txt");
    			   Enumeration k = hash.keys();
    			    while (k.hasMoreElements()) {
    			      String key = (String) k.nextElement();
    			      String output = key + ";" + (String)hash.get(key);
    			      System.out.println(output);
    			      out.write(output);
    			      out.close();
    			      fop.write(output.getBytes());
    			      fop.close();
    			      
    			    }
    			
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    			
        }
    
    }

  2. #2
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Why do you need out AND fop?
    Java Code:
    			BufferedWriter out = new BufferedWriter(new FileWriter("/Users/eric.manget/Desktop/Calendar.txt",true));
    			FileOutputStream fop=new FileOutputStream("/Users/eric.manget/Desktop/Calendar.txt");
    The close() method should be outside the while loop. The file should be closed AFTER the while loop has finished writing everything to the file.
    Java Code:
    			    while (k.hasMoreElements()) {
    			      String key = (String) k.nextElement();
    			      String output = key + ";" + (String)hash.get(key);
    			      System.out.println(output);
    			      out.write(output); 			      
    			    }
                           out.close();

Similar Threads

  1. reading a file and writing to a file....help!!!!
    By java_prgr in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 07:53 PM
  2. Replies: 1
    Last Post: 02-27-2010, 02:29 AM
  3. Replies: 9
    Last Post: 10-20-2009, 11:52 AM
  4. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  5. Replies: 9
    Last Post: 07-10-2008, 05:52 PM

Tags for this Thread

Posting Permissions

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