Re: clerifying my previous question>hw i wld read a portion of file
Hi,
I sent a reply but it seems it did not make it... probably since I had an
attachement. Anyway, here goes...
A small program that scans a stream for a start token and optionally and end
token:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
/**
* Scans a stream for a start and optionally and end token
*/
public class StreamFind {
/**
* Main
* @param args the arguments
* @throws Exception if an error occurs
*/
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.err.println("usage: <file> <start token> [<end token>]");
System.exit(-1);
}
int argNbr = 0;
// assume platform default encoding is OK...
InputStream is = new FileInputStream(args[argNbr++]);
Reader br = new BufferedReader(new InputStreamReader(is));
//
String startToken = args[argNbr++];
String endToken = args.length == 3 ? args[argNbr++] : null;
//
StringBuilder b = null;
while (true) {
if (b == null && peekForToken(startToken, br)) {
b = new StringBuilder();
b.append(startToken);
} else if (endToken != null && b != null &&
peekForToken(endToken, br)) {
b.append(endToken);
break;
} else {
int c = br.read();
if (c == -1) {
break;
}
if (b != null) {
b.append((char) c);
}
}
}
if (b != null) {
System.out.println(b);
}
System.exit(b != null ? 0 : -1);
}
/**
* Peeks in the stream to check if the specified token is found
* @param token the token to look for
* @param br the reader
* @return <code>true</code> if the token is found, in this case the
stream is positioned
* after the token, otherwise false and the stream is unchanged
* @throws IOException
*/
private static boolean peekForToken(String token, Reader br) throws
IOException {
br.mark(token.length());
boolean matching = true;
for (int i = 0; matching && i < token.length(); i++) {
int c = br.read();
matching = c != -1 && c == token.charAt(i);
}
if (!matching) {
br.reset();
}
return matching;
}
}
If you disregard whitespace and only consider whitespace surrounded tokens a
java.util.Scanner can be used instead:
import java.io.File;
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.err.println("usage: <file> <start token> [<end token>]");
System.exit(-1);
}
int argNbr = 0;
// assume platform default encoding is OK...
Scanner scanner = new Scanner(new File(args[argNbr++]));
//
String startToken = args[argNbr++];
String endToken = args.length == 3 ? args[argNbr++] : null;
//
StringBuilder b = null;
while (scanner.hasNext()) {
String t = scanner.next();
if (b == null && t.equals(startToken)) {
b = new StringBuilder();
b.append(t);
} else if (b != null && t.equals(endToken)) {
b.append(t);
break;
} else if (b != null) {
b.append(t);
}
}
if (b != null) {
System.out.println(b);
}
System.exit(b != null ? 0 : -1);
}
}
Cheers,
Martin.
"vk" <seductive.vk@gmail.com> wrote in message
news:1153810008.316556.243720@h48g2000cwc.googlegroups.com...
I have a large file , I want to read the file from a location based on
a value till the end of the file.
eg :
hi im varun
how do you do
i m good..
if this is content of file in this case if i want to read and display it
from 'how --til good '
Is there a way we can do it in java.
Thanks in advance.