Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import java.io.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
public class Test {
public static void main(String[] args)throws FileNotFoundException, UnsupportedEnco
dingException, IOException
{
BufferedReader input;
String line;
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));
line = String.format("[%s_\nDINAMALAR_
DATE]", f.getName());
output.println(line);
System.out.println(line);
while ((line = input.readLine()) != null)
{
output.println(line);
System.out.println(line);
}
input.close();
}

output.close();
input = new BufferedReader(new FileReader(
intermediate));
int count = 0;
output = null;
while ((line = input.readLine()) != null)
{
if (line.startsWith("["))
{
if (output != null) output.close();
output = new PrintWriter(new File(line + ".txt"), "UTF-8");
output.println(line);
count++;
continue;
}
output.println(line);
}
input.close();
}}}
Posted
Comments
Richard MacCutchan 20-Dec-15 10:16am    
Where? Please do not just dump a lot of unformatted code and expect people to guess what the problem is, and where it occurs.
Member 12218847 20-Dec-15 10:44am    
Sorry......I am trying to read multiple files and to get respective file names inside the file and to display the output directly on a new text documents..
1) line = String.format("[%s_\nDINAMALAR_
DATE]", f.getName());
---From this line i tried to paste the file name inside the respective file---
2) output = new PrintWriter(new File(line + ".txt"), "UTF-8");
---this line i used to give name for newly created files---
The newly created file name comes along with [ ..as i mentioned in the 1)
i want [ inside the file but not in the name of the file
Richard MacCutchan 20-Dec-15 11:30am    
Then you need to parse out the square brackets from that text. Look at the documentation for String.split.
Member 12218847 20-Dec-15 12:08pm    
ok sir....thank you...

1 solution

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