Click here to Skip to main content
16,021,449 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Java
import java.io.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
public class tryy
{
    public static void main(String args[])throws IOException
    {
File file=new File("D:\\fold\\myfile.txt");
 PrintWriter writer= new PrintWriter(file, "UTF-8");
String p="fold";
      File dir=new File(p);
      File[]  files=dir.listFiles();
            for(File f : files)
              if(f.isFile())
                 { 
StringBuilder sb=new StringBuilder();

              BufferedReader  ins=null;
             ins=new BufferedReader(new FileReader(f));
               String line;
              writer.print("[");
           writer.print(f.getName());
           writer.println("_DINAMALAR_DATE]\n");
            while((line=ins.readLine())!=null)
                     {
                            writer.println(line);
writer.println(sb.toString());
System.out.println(line);
}
}

   writer.close();
     String inputFile="D:\\fold\\myfile.txt";
String fle=inputFile; 
BufferedReader br = new BufferedReader(new FileReader(inputFile));
 
         String line=null;
         StringBuilder sb = new StringBuilder();
         int count=1;
{
 while((line = br.readLine())!= null){              
     if(line.startsWith("[")){
                    if(sb.length()!=0){
                        File file1= new File(line+".txt");
                        
                            PrintWriter writer1 = new PrintWriter(file1, "UTF-8");
                            
                            writer1.println(line);
                            writer1.println(sb.toString());
                             writer1.close();
                  sb.delete(0, sb.length());

                        count++;
                    }
                    continue;
                }
                sb.append(line);
 }
     }
    }

}
Posted
Updated 14-Dec-15 19:43pm
v2
Comments
Member 12200805 15-Dec-15 1:36am    
Exception in thread "main" java.lang.NullPointerException
at tryy.main(tryy.java:15)
getting this error while compiling...any solutions plz
Suvendu Shekhar Giri 15-Dec-15 1:45am    
Share the complete exception.
Member 12200805 15-Dec-15 3:07am    
thank you sir ..i got the output ....but respective file contents changing between multiple files ...where i hava to change the code to get the file content correctly?

1 solution

I don't know Java but you should inspect these lines:
Java
String p="fold";
File dir=new File(p);
File[] files=dir.listFiles();
    for(File f : files)
        if(f.isFile())

According to your comment, the execption is thrown in the last line: ryy.main(tryy.java:15)

The passed path "fold" looks suspect and I assume that dir will not be initialised to an existing directory. Then your files variable will be NULL:
public File[] listFiles()

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

If this abstract pathname does not denote a directory, then this method returns null.

In the following for loop f will become NULL too and the next command f.isFile() (in line 15) will throw the exception.
 
Share this answer
 
Comments
Member 12200805 15-Dec-15 3:41am    
got the output ..thank you ..sir

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