Results 1 to 5 of 5
  1. #1
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default writing into file is not complete

    hello
    I have problem with reading and writing the files
    I need to do calculation to obtain data and then writing this data into file, the data is large.
    the data is saved in vector so I need to write each element of vector in line in the file.
    the each elemtn of vector contains 97 numbers.
    my problem is that it writes the first elemnt of vector and it writes second element but I don't know why in the third line the third element of vector is not complete it writes only 50 numbers
    for the fourth it writes the complete element but for the fifth it writes also some of numbers in that element and so

    I have tested my program for small data and it works fine but I don't know what is the problem

    thanks

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

    Default

    Your code has a bug in it. I'm not sure how we can guess what this bug is based solely on your description above.
    Last edited by Fubarable; 04-14-2010 at 12:32 AM.

  3. #3
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Show the code

  4. #4
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default

    the code is below
    Java Code:
    static void Reordering_solution(FileWriter writer_rordering_out,File reordering, FileWriter writer_solutions){
    	 try { 
        for (int y=0; y<vector_SA.size()-1; y++){
    		 not_visited.clear();
    	    nex_min_sw_same_dir(vect_ordered_temp,next_point);
    	     
    	   if (vect_min.size()==1){
    		   test_after_filling_ordering.add(vect_min.get(0).toString());
    		   _2nd_min_element=vect_min.get(0).toString();
    	    	vect_ordered_temp.removeElement(next_point);
    	    	next_point=_2nd_min_element; }
    	   
    	   
    	    if (vect_min.size()>1){
    	     
    		   for (int k=0;k<vect_min.size();k++){
    			   vect_min_s= vect_min.get(k).toString();
    			   if (first_vect_min!=null && vect_min_s.equals(first_vect_min)){
    				       for (int m=k+1;m< vect_min.size();m++)
    					   not_visited.add(vect_min.get(m).toString());}
    			   
    			   if (first_vect_min==null ){
    				   not_visited.add(vect_min.get(k).toString());}}
    		   
    		        if (not_visited.size()==0){
    		        	y=vector_SA.size()-1;
    		            vect_min_s= null;}
    		        
    		        if (not_visited.size()!=0)
    		          vect_min_s=not_visited.get(0).toString();
                    
    		  
    		   
    		   if (not_visited.size()==vect_min.size()){
    		  for (int h=0; h<not_visited.size();h++){
    		  for (int g = 0; g < test_after_filling_ordering.size(); g++) {
    				writer_rordering_out.write(test_after_filling_ordering.get(g).toString());
    				writer_rordering_out.write("\r\n");}
    		    
    		        writer_rordering_out.write("Empty");
    		        writer_rordering_out.write("\r\n");
    				writer_rordering_out.write(not_visited.get(h).toString());
    				writer_rordering_out.write("\r\n");
     				writer_rordering_out.write("Empty");
     				writer_rordering_out.write("\r\n");
     			
     		  for (int g = 0; g < vect_ordered_temp.size(); g++) {
     			    writer_rordering_out.write(vect_ordered_temp.get(g).toString());
     				writer_rordering_out.write("\r\n");}
     		 
    		        writer_rordering_out.write("Empty"); 
    		        writer_rordering_out.write("\r\n");}}
    	               
    		
    	   	     test_after_filling_ordering.add(vect_min_s);
    		     _2nd_min_element=vect_min_s;
    	         vect_ordered_temp.removeElement(next_point);
    	         next_point=_2nd_min_element;	
    	        }
    
       if ( test_after_filling_ordering.size()==vector_SA.size()){
    	   y=vector_SA.size()-1;
         System.out.println();
    	 System.out.println(" The Reordering string is :  ");
    	 System.out.println(test_after_filling_ordering);
    	 System.out.println();
    	 
    	 for (int g = 0; g < test_after_filling_ordering.size(); g++) {
    		    writer_solutions.write(test_after_filling_ordering.get(g).toString());
    		    writer_solutions.write("\r\n");}
    	        writer_solutions.write("Empty");
    	        writer_solutions.write("\r\n");}    
          	  
     	}// end for (i) loop
    	
    	
    	
       
       writer_rordering_out.close();
    
        test_after_filling_ordering.clear();
        vect_ordered_temp.clear();
       copy_min.clear();
       test_pattern1.clear();
       test_pattern2.clear();
       test_pattern3.clear();
       
        Scanner inFile_77= new Scanner(new FileInputStream(reordering));
        BufferedReader reader_reordering_sub = new BufferedReader(new FileReader(reordering));
        line_reodering = null;
        line_reodering = reader_reordering_sub.readLine();
        line_reodering=inFile_77.nextLine();
        
        	for (int j=0; j< 200;j++){
        	    if (!line_reodering.contains("Empty") && inFile_77.hasNextLine()){
        	    	test_pattern1.add(line_reodering);
        			line_reodering=inFile_77.nextLine();}
        		if (line_reodering.contains("Empty")){
    				j=200;}
    		   }
        	
        	
        	
    		line_reodering=inFile_77.nextLine();
    	   if (!line_reodering.contains("Empty") && inFile_77.hasNextLine()){
    		   test_pattern2.add(line_reodering);
    		line_reodering=inFile_77.nextLine();}
    	   
    	   
    	   
    		line_reodering=inFile_77.nextLine();
    		
    		for (int j=0; j< 200;j++){
    			if (!line_reodering.contains("Empty") && inFile_77.hasNextLine()){
    				test_pattern3.add(line_reodering);
    			line_reodering=inFile_77.nextLine();}
    			if (line_reodering==("Empty")){
    				j=200;}
    			   }
      
    		
    		
    	  if (!inFile_77.hasNextLine())
    		  return;
    	  
         line_reodering=inFile_77.nextLine();
    	 for (int j=0; j< 200;j++){
     	    if (!line_reodering.contains("Empty") && inFile_77.hasNextLine()){
     	    	test_after_filling_ordering.add(line_reodering);
     			line_reodering=inFile_77.nextLine();}
     		if (line_reodering.contains("Empty")){
    				j=200;}
    		   }
     	
    		
    	
    		line_reodering=inFile_77.nextLine();
    	   if (!line_reodering.contains("Empty") && inFile_77.hasNextLine()){
    		   copy_min.add(line_reodering);
    		line_reodering=inFile_77.nextLine();}
    	
    	   System.out.println(" the copy min is "+ copy_min);
    
    		line_reodering=inFile_77.nextLine();
    		
    		for (int j=0; j< 200;j++){
    			if (!line_reodering.contains("Empty") && inFile_77.hasNextLine()){
    				vect_ordered_temp.add(line_reodering);
    			line_reodering=inFile_77.nextLine();}
    			if (line_reodering==("Empty")){
    				j=200;}
    			   }
    		
    		
              reader_reordering_sub.close();
    		 
    		  
     if ( test_after_filling_ordering!=null && copy_min!=null && vect_ordered_temp!=null){
    		
    	 File re= new File("reordering11");
    		 FileWriter writer_rodering_2 = new FileWriter("reordering11");
    	System.out.println(" the test pattern 1 is "+ test_pattern1);
    			  for (int g = 0; g < test_after_filling_ordering.size(); g++) {
    				  writer_rodering_2.write(test_after_filling_ordering.get(g).toString());
    				  writer_rodering_2.write("\r\n");}
    			      writer_rodering_2.write("Empty");
    			      writer_rodering_2.write("\r\n");
    			      writer_rodering_2.write(copy_min.get(0).toString());
    			      writer_rodering_2.write("\r\n");
    			     writer_rodering_2.write("Empty");
    			      writer_rodering_2.write("\r\n");
    	 		   for (int g = 0; g < vect_ordered_temp.size(); g++) {
    	 			   writer_rodering_2.write(vect_ordered_temp.get(g).toString());
    	 			   writer_rodering_2.write("\r\n");}
    	 			   writer_rodering_2.write("Empty"); 
    	 			   writer_rodering_2.write("\r\n");
    	
    	while (inFile_77.hasNextLine()){
    		 line_reodering=inFile_77.nextLine();
    		 writer_rodering_2.write(line_reodering);
    		 writer_rodering_2.write("\r\n");}
    
    		    	   
    		next_point=test_after_filling_ordering.get(test_after_filling_ordering.size()-1).toString();
    		if (test_pattern1.equals(test_after_filling_ordering)){
    		first_vect_min=test_pattern2.get((test_pattern2.size()-1)).toString();
    		}
    		if (!test_pattern1.equals(test_after_filling_ordering))
    			first_vect_min=null;
    		
    
    		Reordering_solution(writer_rodering_2 ,re, writer_solutions);}
          writer_solutions.close();
        	  return;
    
    	   } catch (FileNotFoundException e) {
    		      e.printStackTrace();
    		   } catch (IOException e) {
    		    e.printStackTrace();
    		   } 
    	
    }
    thanks

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

    Default

    I'm not sure of the others, but I find this code very difficult to read due to bad formatting. You would do well to correct the formatting and to refactor this huge method so it consists of several smaller testable methods. Also, you might look to creating a small test program that does nothing but writes a large vector to a file, and then if it works, keep adding to this small program until it does what you want the above to do, or til it breaks, and then if it breaks, post the small compilable program here. This is called creating an SSCCE and is described more fully in the link below in my signature.

    Best of luck.

Similar Threads

  1. Replies: 1
    Last Post: 02-27-2010, 02:29 AM
  2. Replies: 9
    Last Post: 10-20-2009, 11:52 AM
  3. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  4. Replies: 9
    Last Post: 07-10-2008, 05:52 PM
  5. Writing to a file (at the end)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:22 AM

Posting Permissions

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