Results 1 to 8 of 8
- 10-03-2012, 05:11 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 18
- Rep Power
- 0
[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
-
Re: Why the existing file is not overwritten?
Your program exits if the file already exists.
- 10-03-2012, 05:19 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 18
- Rep Power
- 0
- 10-03-2012, 05:23 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 18
- Rep Power
- 0
Re: Why the existing file is not overwritten?
Got it already.. I removed the System.exit(0);
Thank you :)
-
Re: Why the existing file is not overwritten?
- 10-03-2012, 05:38 PM #6
Member
- Join Date
- Feb 2012
- Posts
- 18
- Rep Power
- 0
-
Re: Why the existing file is not overwritten?
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.The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
- 10-03-2012, 06:05 PM #8
Member
- Join Date
- Feb 2012
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Writing to existing CSV file
By tuxman in forum New To JavaReplies: 8Last Post: 03-17-2012, 12:54 PM -
solaris machine /tmp folder, File.exists() cant see the existing file.
By aragorn1905 in forum Advanced JavaReplies: 0Last Post: 12-21-2011, 09:15 AM -
how to put existing file in ftp
By trkece in forum NetworkingReplies: 0Last Post: 02-11-2011, 05:42 AM -
How tor write into Existing File
By agajantorayev in forum New To JavaReplies: 5Last Post: 08-17-2010, 01:03 PM -
Tomcat path overwritten after every restart
By San2009 in forum New To JavaReplies: 1Last Post: 08-10-2010, 02:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks