aspose file tools*
The moose likes I/O and Streams and the fly likes problem with reading and writing toa  files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "problem with reading and writing toa  files" Watch "problem with reading and writing toa  files" New topic
Author

problem with reading and writing toa files

Sanny kumar
Ranch Hand

Joined: May 18, 2005
Posts: 53
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

Joined: Oct 29, 2001
Posts: 8927
    
    9

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Sanny kumar
Ranch Hand

Joined: May 18, 2005
Posts: 53
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

Joined: Jan 29, 2003
Posts: 8791
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

Joined: May 18, 2005
Posts: 53
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

Joined: Jan 29, 2003
Posts: 8791
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: problem with reading and writing toa files