Click here to Skip to main content
16,021,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import java.io.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
public class project1{
public static void main(String[] args)throws FileNotFoundException, UnsupportedEncodingException, IOException {
    BufferedReader input;
String title;
File intermediate = new File("myfile1.txt");
    PrintWriter output = new PrintWriter(intermediate, "UTF-8");
    String p = "hh";
    File dir = new File(p);
   File[] files = dir.listFiles();
    for (File f : files) {
      if (f.isFile()) {
 input = new BufferedReader(new FileReader(f));
title = String.format("[%s\n_DATE]", f.getName());
 output.println(title);
        System.out.println(title);
        while ((title = input.readLine()) != null) {
          output.println(title);
          System.out.println(title);
        }
        input.close();
      }
    }
    output.close();
    input = new BufferedReader(new FileReader(intermediate));
    int count = 0;
    output = null;
    while ((title = input.readLine()) != null) {
      if (title.startsWith("[")) {
        if (output != null) output.close();
int index =title.indexOf (".");
 title= title.substring (0, index);
title = title.replace("["," ");
        output = new PrintWriter(new File(title +".txt" ), "UTF-8");
output.print("[");
output.print(title);

        count++;
        continue;
      }
      output.println(title);
    }
    input.close();
  }
}
Posted
Updated 22-Dec-15 22:16pm
v2
Comments
Member 12200805 23-Dec-15 0:40am    
this program run successfully in comment prompt but shows error in netbeans...
for (File f : files) {
---in this line..
phil.o 23-Dec-15 2:47am    
A basic debug session will bring you the light.
[no name] 23-Dec-15 4:11am    
Which line of code is line 27?
Member 12200805 23-Dec-15 4:29am    
line 27: for (File f : files) {
[no name] 23-Dec-15 5:16am    
So it sounds like files is null. You need to find out why; most likely because there is no directory named hh in the current path. You must always check the values returned by library calls to see if your code did what you expected. Do not assume that it will always work.

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