aspose file tools*
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
Author

please help...

Eric Large
Greenhorn

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)
{
System.out.print(fileIn.getName());
}
}
Any guidence would be very appreciated! Thank you so much!
Eric
Steve Chernyak
Ranch Hand

Joined: Oct 19, 2000
Posts: 113
Check out the jdk api: http://java.sun.com/j2se/1.3/docs/api/index.html
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
author
Ranch Hand

Joined: Aug 03, 2001
Posts: 64
Here's a quick version:
<pre>import java.io.*;

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)
{
System.out.println(fileName);
return;
}
}
}
}</pre>
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: http://aspose.com/file-tools
 
subject: please help...