Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to read a file block by block?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the title,
I don't know how to do that!
and I don't know how to write blocks (with the same block size)
into a file.
Please help me !!!
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here is the sample code for reading a file block by block..
This piece of code extracts the full content(html) from a url and store a particular block(means from <form> to </form>) in
a file called somefilename.txt
import java.net.*;
import java.io.*;
import java.util.*;
class strbuf
{
public static void main(String ar[])
throws IOException
{

URL url=new URL("http://www.usbullionexchange.com/pricesonline.cfm");
BufferedReader buf=new BufferedReader(new InputStreamReader(url.openStream()));
String in;
StringBuffer strbuffer=new StringBuffer();
String temp;
int startind=0;
int endind=0;


while ((in = buf.readLine()) != null)
{
if(in.length()>0)
strbuffer=strbuffer.append(" "+in);

}

temp=strbuffer.toString();
startind=temp.indexOf("<form ");>
endind=temp.indexOf("</form>");

byte[] b=temp.substring(startind,endind+7).getBytes();
try
{
FileOutputStream file=new FileOutputStream("somefilename.txt");
file.write(b);
}
catch(Exception e)
{
System.out.println("Error :"+e);
}
buf.close();
}
}
Hope this will help,
Best Regards,
Paramaguru.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, which you agreed to when you registered, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
hsmpaty
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks, Parama!
But, the program could only read ONE block.
And all I have to do is just reading a LOCAL file.
Do u know how to do that block by block with the same block size?
(ex. 128 bytes) not character by character.
Thanks anyway,
Morgan Huang
 
Paramagurusamy Balasubramanian
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Use FileInputStream's read(byte[] b) method to do this.
It reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.
Regards,
Paramaguru
 
hsmpaty
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Parama,
Here is my code for reading a file (partial):

This works, of course.
But not exactly the way what I want to.
U see, The file was read "byte by byte" and store in a byte array.
Is there something worng with my code? or
what kind of changes should I make?
Regards,
Morgan Huang
 
Paramagurusamy Balasubramanian
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The approach is correct.ie Reading from a file and store into a byte array.If u want to read block by block say 128,then u can navigate the byte array(buf)..
Best Regards,
Paramaguru
 
BWA HA HA HA HA HA HA! Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic