Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with reading and writing toa files

 
Sanny kumar
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

i m having problem with reading the large file and splitting the file inot multiple files..

the sample large file as follows:
================================
the number is
the file number name is a1.txt
823
3232
232
233
3232
end number
the number is
the file number name is a2.txt
823
3232
232
233
3232
end number
the number is
the file number name is a3.txt
823
3232
232
233
3232
end number
the number is
the file number name is a4.txt
823
3232
232
233
3232
end number
the number is
the file number name is a5.txt
823
3232
232
233
3232
end number

i want to create separate files reading the file.ex:in the above sample file it should create files if the line starts with the "number is--" and ends with "end number.." like this i want to create file reading the complete large file..
i m able to read the file but i m not able to write into files...
sample code as follos:

[JCE: Added code tags]
[ August 31, 2006: Message edited by: Joe Ess ]
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean you are "not able to write"? Do you get an exception? Do you get the output you expect?
You should be aware that the DataInputStream.readLine() method is deprecated.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you have the syntax for reading a file a line at a time ok. The test for something about Billing doesn't match the input file. Is that copied from another program?

As you read through the lines what tells you that you have reached the end of one file's worth of data? When you see that line, close the output.

What tells you that you have reached the start of a new file? When you see that line, open a new output file. You might have to read one more input line to get the output filename before opening the output.

Does that help at all? See if you can apply those ideas to your code and post again. Use the "CODE" button below the editor to preserve the indentation in your code. That makes it much easier for us to read.
 
Sanny kumar
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

i m having problem with reading the large file and splitting the file inot multiple files..

the sample large file as follows:
================================
the number is
the file number name is a1.txt
823
3232
232
233
3232
end number
the number is
the file number name is a2.txt
823
3232
232
233
3232
end number
the number is
the file number name is a3.txt
823
3232
232
233
3232
end number
the number is
the file number name is a4.txt
823
3232
232
233
3232
end number
the number is
the file number name is a5.txt
823
3232
232
233
3232
end number

i want to create separate files reading the file.ex:in the above sample file it should create files if the line starts with the "number is--" and ends with "end number.." like this i want to create file reading the complete large file..
i m able to read the file but i m not able to write into files...
sample code as follos:
import java.io.*;

public class Pintest {

public static void main (String[] args) throws Exception {
Pintest f = new Pintest();
String pfile=args[0];
f.readMyFile(pfile);
}



void readMyFile(String file) {

try {
BufferedReader in = new BufferedReader(new FileReader(file));
String str;
int count=2;

while ((str = in.readLine()) != null) {
count++;
if(str.startsWith("the number is")){
int cnt=count+2;
BufferedWriter out = new BufferedWriter(new FileWriter("pin"+count+".pin"));
System.out.println(str);
in.readLine();
out.write(str);
}


}
in.close();
} catch (Exception e) {
}
}

}


here i m getting only one line output i.e.."the number is" in the every generate file..

thanks in advance
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try declaring your output stream before the loop. Hook it up to a new file every time "the number is" comes along.

Let me know if that is enough to go on.
 
Sanny kumar
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,

Thanks for the code..i tried but i m unable to create multiple files.
the number is
the file number name is a1.txt
823
3232
232
233
3232
end number
the number is
the file number name is a2.txt
823
3232
232
233
3232
end number
the number is
the file number name is a3.txt
823
3232
232
233
3232
end number
the number is
the file number name is a4.txt
823
3232
232
233
3232
end number
the number is
the file number name is a5.txt
823
3232
232
233
3232
end number


my requirement is i want to create separate files if the string starts from "the number is" and ends with "end number" its one file..
ex:
the number is
the file number name is a5.txt
823
3232
232
233
3232
end number
this is one file with the file name a5.txt..vice versa i want to read the complete file and create separate files if the string starts with "the number is" and string ends with "end number"..

thanks in advance
San
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The logic I showed will write a file per block as you want, but it won't have the right filenames. That's because you need to get the filename from the 2nd line in the block, not the first. You could do some monkeying about to read two lines to get the name and then write two lines but that sounds kinda kludgey. You might write to a temp name (see File for a method to get a temp name), capture the name you want from the 2nd line and rename temp to real name after closing.

Try making some code that matches my logic suggestion and see if we can fix things up from there.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic