wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to read  part content of afile ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to read  part content of afile ?" Watch "How to read  part content of afile ?" New topic
Author

How to read part content of afile ?

John Smith
Greenhorn

Joined: Sep 21, 2005
Posts: 13
I have a file and the content of file is fllow,I need create three object from the file. the blank line seperate the file three parts.How to do I?


%CDM-ORS
SMS: 胜利油田
CODE: 05460002
DATE: 2006-08-29

30104 307
30100 196
30103 185
30108 1

TFCA 8054602122000251 30104
TFCA 8054602122000368 30100
TFCA 8054602122000426 30103 30104
TFCA 8054602122000616 30100 30104
.................................
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
You can go for BufferedReader class. readLine() method will return you a line of text.

Create instances of transfer object corresponding to each and very part of your file and populate each transfer objects by 4 lines of each part. This will return you three different transfer objects corresponding to three different parts.


- Naseem
[ August 31, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
As another alternative, why not investigate RandomAccessFile.

Jeremy


McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A Random Access File is really good when a file has fixed-length chunks. In this file it would be pretty hard to seek directly to the start of the second block without just reading the first block.

Years ago PJ Plauger suggested any file structure is a mini-language and any file reader is a mini-parser. For some reason I always got a lot of pleasure out of making little language files like this and parsers to read them.

See if you can make a method that looks like:

Use better names ... BlockOne is something about SMS header? Each readWhatever would read lines until it hits a blank one.


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
John Smith
Greenhorn

Joined: Sep 21, 2005
Posts: 13
thanks your help first,I think I don't describe the problem clearly, the first bolock of content is fixed-length chunk, the others bolck is unfiexd-length block ,how to do ? thanks

%CDM-ORS
SMS: 胜利油田
CODE: 05460002
DATE: 2006-08-29

30104 307
30100 196
30103 185
30108 1


TFCA 8054602122000251 30104
TFCA 8054602122000368 30100
TFCA 8054602122000426 30103 30104
TFCA 8054602122000616 30100 30104
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Give it a shot and show us what you come up with. We need to know just where you're stuck to give the right answer.

Can you set up a reader and read a file one line at a time?

Can you compare the line you got to the things you expect?
John Smith
Greenhorn

Joined: Sep 21, 2005
Posts: 13
Stan James thanks you first


the content of file is unzip form a zip package,the problem is how to seperte three part by blank line ,put them into different List .

the follow is my program, the problem is I don't know the number of blank line between two part.


public static void unzipFile(File file_input, File dir_output) {

ZipInputStream zip_in_stream = null;
ZipEntry zip_entry = null;
try {
FileInputStream in = new FileInputStream(file_input);
BufferedInputStream source = new BufferedInputStream(in);
zip_in_stream = new ZipInputStream(source);
}
catch (IOException e) {
e.printStackTrace();

}
try {
int blockCount = 0;

while ((zip_entry = zip_in_stream.getNextEntry()) != null) {
try {
zip_entry.getName();
BufferedReader reader = new BufferedReader(new InputStreamReader(zip_in_stream));
String line;
while ((line = reader.readLine()) != null) {
if (line.length() == 0) {
blockCount++;
System.out.println(line);
continue;
}

if (blockCount == 0) {
headList.add(line);

}

if (blockCount == 1) {
productStaticList.add(line);

}
if (blockCount == 2) {
detailList.add(line);
}
System.out.println(line);

}

}
catch (IOException e) {
e.printStackTrace();
}
zip_in_stream.closeEntry();
}

zip_in_stream.close();

}
catch (IOException e) {

e.printStackTrace();
}

}

%CDM-ORS
SMS: 胜利油田
CODE: 05460002
DATE: 2006-08-29

30104 307
30100 196
30103 185
30108 1


TFCA 8054602122000251 30104
TFCA 8054602122000368 30100
TFCA 8054602122000426 30103 30104
TFCA 8054602122000616 30100 30104
TFCA 8054602122000731 30100 30104
TFCA 8054602122000988 30103
TFCA 8054602122001085 30103 30104
TFCA 8054602122001192 30100
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Maybe add a flag to indicate we are in an area of blank lines:

This is very "procedural". That is, we could do it in COBOL or C. Once you get that working we could look into some more object oriented ideas.
John Smith
Greenhorn

Joined: Sep 21, 2005
Posts: 13
Stan James

thanks

I reslove the problem through flag
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read part content of afile ?
 
Similar Threads
Ho to modify a file through java?
Applet not shown in Eclipse internal browser.
executable
Html contents displayed by servlet
How to depart a file into two file?