Results 1 to 8 of 8
  1. #1
    Akirien is offline Member
    Join Date
    Feb 2012
    Posts
    18
    Rep Power
    0

    Default [SOLVED] Why the existing file is not overwritten?

    Hello.. I have this code. I'm wondering why the content of existing file is not overwritten.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Sorting{
    	
    	public static void main(String[]args) throws Exception{
    		
    		File file = new File("Sorting.txt");
    		
    		if (file.exists()){
    			
    			System.out.println("File already exists!");
    			System.exit(0);
    			
    		}
    		
    		PrintWriter output = new PrintWriter(file);
    		
    		for (int i = 0; i < 100; i++){
    			
    			int x = (int) (Math.random() * 100);
    			output.print(x);
    			output.print(" ");
    		}
    		output.close();
    		
    		Scanner readFile = null;
    		ArrayList<Integer> list = new ArrayList<Integer>();
    		
    		try {
    			readFile = new Scanner(new File("Sorting.txt"));
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
    		
    		while(readFile.hasNext()){
    			if (readFile.hasNextInt()) list.add(readFile.nextInt());
    			else readFile.next();
    		}
    		
    		Collections.sort(list);
    		
    		for (Integer y: list) System.out.println(y);
    		
    		
    	}
    }
    Last edited by Akirien; 10-03-2012 at 05:24 PM. Reason: Solved

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Akirien is offline Member
    Join Date
    Feb 2012
    Posts
    18
    Rep Power
    0

    Default Re: Why the existing file is not overwritten?

    Quote Originally Posted by Fubarable View Post
    Your program exits if the file already exists.
    Thank you for replying... How can I overwrite the content of the existing file?

  4. #4
    Akirien is offline Member
    Join Date
    Feb 2012
    Posts
    18
    Rep Power
    0

    Default Re: Why the existing file is not overwritten?

    Got it already.. I removed the System.exit(0);

    Thank you :)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why the existing file is not overwritten?

    Quote Originally Posted by Akirien View Post
    Thank you for replying... How can I overwrite the content of the existing file?
    By deleting the block of code that tests if the file exists and exits if so...

    Did you write this code yourself??

  6. #6
    Akirien is offline Member
    Join Date
    Feb 2012
    Posts
    18
    Rep Power
    0

    Default Re: Why the existing file is not overwritten?

    Quote Originally Posted by Fubarable View Post
    By deleting the block of code that tests if the file exists and exits if so...

    Did you write this code yourself??
    I referred from the book. And the sorting part, I modified from the web. Can I ask something. The System.exit(Integer) is for what? I know the program will exit. I mean the parameter.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why the existing file is not overwritten?

    Quote Originally Posted by Akirien View Post
    I referred from the book. And the sorting part, I modified from the web. Can I ask something. The System.exit(Integer) is for what? I know the program will exit. I mean the parameter.
    Good question, one that is well answered in the System entry in the API:
    The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
    So if the program is exiting normally -- not due to some error or exception, return (0) in the parameter. Otherwise return a non-0 number, one that will make sense to you when you review your code 6 months from now.

  8. #8
    Akirien is offline Member
    Join Date
    Feb 2012
    Posts
    18
    Rep Power
    0

    Default Re: Why the existing file is not overwritten?

    Quote Originally Posted by Fubarable View Post
    Good question, one that is well answered in the System entry in the API:


    So if the program is exiting normally -- not due to some error or exception, return (0) in the parameter. Otherwise return a non-0 number, one that will make sense to you when you review your code 6 months from now.
    Thank you for the explanation. A little bit confuse but trying to understand. Haha..

Similar Threads

  1. Writing to existing CSV file
    By tuxman in forum New To Java
    Replies: 8
    Last Post: 03-17-2012, 12:54 PM
  2. Replies: 0
    Last Post: 12-21-2011, 09:15 AM
  3. how to put existing file in ftp
    By trkece in forum Networking
    Replies: 0
    Last Post: 02-11-2011, 05:42 AM
  4. How tor write into Existing File
    By agajantorayev in forum New To Java
    Replies: 5
    Last Post: 08-17-2010, 01:03 PM
  5. Tomcat path overwritten after every restart
    By San2009 in forum New To Java
    Replies: 1
    Last Post: 08-10-2010, 02:58 PM

Posting Permissions

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