Click here to Skip to main content
14,836,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code in this i m gettng o/p ,evrythng if fine but i was unable to print output to a file.Can u help me where was the mistake actually

Java
import java.io.*;
import java.util.*;
public class MainClass {
    public static void main(String[] args) throws Exception {
        int flag=0,cindex=0;;
        String fname=null,first=null,second=null;
        TreeMap tm=new TreeMap();
        File f=new File("D:\\final.txt");
        f.createNewFile();
        PrintWriter pw=new PrintWriter(new FileWriter(f));
        File f1=new File("D:\\trnstons");
        String line=null;
        Map hm2;
        Set s2;
        String lkeys=null,lvalues=null;
        int c=0;
        //BufferedReader br1;

        f1.mkdir();
        String s[]=f1.list();
        //System.out.println("TOTAL COUNT OF FILE::"+s.length);
        int fcount=s.length;
 
        for(String s1:s)
        { 
            File f2=new File("D:\\trnstons\\"+s1);
            BufferedReader br1=new BufferedReader(new FileReader(f2));
            System.out.println( " "+f2.getName());
            //pw.println("===========================FILE NAME IS::====================="+f2.getName());
            line=br1.readLine();
	   
            while(line!=null)
            {
                while((line.equals("SUCCESS")))
                {
                    line=br1.readLine();
                    while(line!=null)
                    {
                        //System.out.println(line);
                        cindex=line.indexOf(",");
                        if(cindex > (-1))
                        {
                            first=line.substring(0,cindex);  
                            second=line.substring(cindex+1); 
                            tm.put(second,first);
                        }
                        //pw.print(line);
                        line=br1.readLine();
		  }
                    ////
                    flag=1;
                    break;
               }
               if(flag==1) 
               break;
	      line=br1.readLine();
	
	  }
	  /////
	  //System.out.println(tm);
      }
  
      //the values in tm are sorted according to values.Now for printing in file as k,v pairs we need to conver to LinkedHashMap
      hm2=new LinkedHashMap(tm);
  
      s2=hm2.entrySet();
      Iterator i=s2.iterator();
      //System.out.println("the values in the tm object are");
      while(i.hasNext())
      {
          Map.Entry me=(Map.Entry)i.next();
          lkeys= (String)me.getKey();/*as the getKey and GetValue returns key and value in the String form,convert in to ArrayList
(it implements RandonAccess(I),so its good at retriveing  rather that any other List  */
          lvalues= (String)me.getValue();
 
          System.out.print(lvalues);
          System.out.print(",");
          System.out.println(lkeys);
          ///trying to print in file 
          pw.print(lvalues);
          pw.print(",");
          pw.println(lkeys);
	 
      } 
   }
}
Posted
Updated 11-Oct-11 4:48am
v2

Java isn't my forte, but I think you either need an extra \ or one less \ here..

Java
or(String s1:s)
        {
            File f2=new File("D:\\trnstons\\"+s1);


(Specifically, the last double \\)
   
v2
I would say, that you should at one point flsh your writer and close the filewriter...

Also declare the FileWriter as variable:

// old: PrintWriter pw=new PrintWriter(new FileWriter(f));
FileWriter fw = new FileWriter(f);
PrintWriter pw=new PrintWriter(fw);

// ...

try {
pw.flush();
pw.close();
fw.flush();
fw.close();
} catch (IOException e) {
   System.err.println("Error writing to file:" + e.getMessage());
}


This should be done with any file, when you finished writing to it. To get unfinished content before ending the write, just use the flush()-Method to get content to Disk...

Hope this helps,
Cheers, Arndt
   
v2
Comments
udayz999 12-Oct-11 0:11am
   
Hey Arndt thanx for ur response,in above program while submitting ur snippet error is coming at f.close() like [close() undefined for the file type] if i left that line,code is working but only first line of the OUTPUT is printing in output
ie in final.txt file
Arndt Faulhaber 12-Oct-11 6:00am
   
Sorry, I was a bit inexact - you also have to flush and cloase the FileWriter, not the File - (got somewhat confused with C, sorry)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900