• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to read part content of afile ?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
.................................
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As another alternative, why not investigate RandomAccessFile.

Jeremy
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
John Smith
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stan James

thanks

I reslove the problem through flag
reply
    Bookmark Topic Watch Topic
  • New Topic