Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please help...

 
Eric Large
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic