![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
New Coder ![]() Join Date: Sep 2009
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
![]() |
reading .txt file
I'm making a login applet and i can get it to output the information necessary to a file, and i can get it to input all the information from the file for me. However i can't seem to find out how to make it search the file for the information.
I don't wish to show my code, but here's basically the output input method i'm using Code:
import java.io.*;
public class OutputInput
{
// Main method
public static void main (String args[])
{
try
{
// Open an output stream
FileOutputStream fout = new FileOutputStream ("myfile.txt");
String myName = ("Hello World!!!\nLine 2\nLine 3");
// Print a line of text
new PrintStream(fout).println (myName);
// Close our output stream
fout.close();
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("myfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null)
{
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}
//Catch exception if any
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
}
|
|
|
|
|
|
PM User | #3 |
|
Regular Coder ![]() Join Date: Jun 2009
Location: Montreal, Canada
Posts: 941
Thanks: 2
Thanked 158 Times in 158 Posts
![]() |
inside this loop add the following search commands:
Code:
while ((strLine = br.readLine()) != null)
{
// Print the content on the console
System.out.println (strLine);
//search for a certain string
if(strLine.indexOf("yourString") != -1)
{
System.out.println("String found");
}
else
{
System.out.println("String not found");
}
}
__________________
Software and cathedrals are much the same - first we build them, then we pray. |
|
|
|
|
|
PM User | #4 |
|
New Coder ![]() Join Date: Sep 2009
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
![]() |
thanks ckeyrouz but the only problem i have with that is that so long as you don't misspell it, it finds the string.
ex. password is hello h -String found he -String found leave it blank -String found t -String not found |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|