File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes please help... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please help..." Watch "please help..." New topic

please help...

Eric Large

Joined: Sep 27, 2001
Posts: 1
Hello. I'm new to Java and am searching for help with something. I'm trying to search a file for a specific character ("@" I'm looking for email addresses). At this point, I just want to write out the name of the file if I find the character I'm searching for.
I have written this so far and have become pretty flustered!
File fileIn = new File("testFile.txt");
FileInputStream fis = new FileInputStream(fileIn);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
String myString = new String();
while ( (myString=dis.read_any()) != -1 )
location= myString.indexOf('@');
if (location != -1)
Any guidence would be very appreciated! Thank you so much!
Steve Chernyak
Ranch Hand

Joined: Oct 19, 2000
Posts: 113
Check out the jdk api:
It shows you how to use all of the classes that come with the jdk
DataInputStream doesn't have a read_any method declared. Also check out other objects that might be a better fit for reading from a file.
Ian Darwin
Ranch Hand

Joined: Aug 03, 2001
Posts: 64
Here's a quick version:

public class FindAt {
public static void main(String[] args) throws IOException {
String fileName = "testFile.txt";
BufferedReader dis = new BufferedReader(new FileReader(fileName));
String myString;
while ((myString=dis.readLine()) != null) {
int location= myString.indexOf('@');
if (location != -1)
We generally use a reader, not an InputStream, for text.
BufferedReader does buffering and line-at-a-time.
You want to return when you get the first match in case
the file has multiple addresses (according to what you said your problem was).
Ian Darwin,
Author of Java Cookbook: Solutions and Examples for Java Developers

Ian Darwin
Many questions are answered in my Java Cookbook, 2nd Edition
I agree. Here's the link:
subject: please help...
It's not a secret anymore!