Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Getting data from a .dat file,and putting new data into a txt file

807606Mar 8 2007 — edited Mar 8 2007
i need help doing something that im pretty sure should be simple enough.

i have .dat file, and it has some ints, i write a program that takes those ints, and sorts them, simple yes, this is just for the sake of testing.

now that i have the itns sorted, i want to get those sorted ints and put them into a .txt file directly

my teacher suggested a printwriter, but i haven't been able to get it to work

heres my code

any suggestins on how to do this, i need the information to be saved onto an empty .txt or .dat file, or for it to create a .txt and put the info in there, whichever works.
thanks,
import java.io.*;
import java.util.*;
import java.text.*;
import static java.lang.System.*;
import static java.lang.Integer.*;
import static java.lang.Double.*;
import static java.lang.Character.*;
import static java.util.Collections.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;

public class sort
{
	public void run()throws Exception
	{
		Scanner file=new Scanner(new File("asdsad.dat"));
		PrintWriter print = new PrintWriter(new File("doc.txt"));
		int run=file.nextInt();
		file.nextLine();   //do not delete
		
		int[] nums = new int[run];		
		
		for(int i=0; i<run; i++)
		{
			nums[i] = file.nextInt();	
		}
		Arrays.sort(nums);

		for(int x = 0 ; x < run ; x++){
			print.write(nums[x]);
		}
		
		
		
				
	}
	
	public static void main(String[] args)throws Exception
	{
		sort a=new sort();
		a.run();
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 5 2007
Added on Mar 8 2007
14 comments
1,717 views