14 Replies Latest reply: Mar 8, 2007 6:17 PM by 807606 RSS

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

    807606
      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();
           }
      }