Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 11-04-2009, 06:15 AM   PM User | #1
mmcnitt
New Coder

 
Join Date: Sep 2009
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
mmcnitt is an unknown quantity at this point
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());
		}
	}
}
so if i wanted it to just look and see if the file said "Line 3"?
mmcnitt is offline   Reply With Quote
Old 11-04-2009, 05:16 PM   PM User | #2
mmcnitt
New Coder

 
Join Date: Sep 2009
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
mmcnitt is an unknown quantity at this point
well i ended up making a work around where it just makes a seperate file for each login, but if anyone has any ideas for how i can so it in one file that'd be great
mmcnitt is offline   Reply With Quote
Old 11-04-2009, 05:38 PM   PM User | #3
ckeyrouz
Regular Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 941
Thanks: 2
Thanked 158 Times in 158 Posts
ckeyrouz is on a distinguished road
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.
ckeyrouz is offline   Reply With Quote
Old 11-07-2009, 05:09 PM   PM User | #4
mmcnitt
New Coder

 
Join Date: Sep 2009
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
mmcnitt is an unknown quantity at this point
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
mmcnitt is offline   Reply With Quote
Old 11-07-2009, 11:36 PM   PM User | #5
cs_student
New Coder

 
Join Date: Oct 2009
Location: ~/
Posts: 61
Thanks: 1
Thanked 8 Times in 8 Posts
cs_student is an unknown quantity at this point
I think that the Pattern class would help you better search for the items you want. This way you can determine what is and isn't acceptable as a match.
cs_student is offline   Reply With Quote
Old 11-08-2009, 09:17 PM   PM User | #6
mmcnitt
New Coder

 
Join Date: Sep 2009
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
mmcnitt is an unknown quantity at this point
i ended up using a Scanner, actually works quite well, thanks for y'alls help ckeyrouz and cs student
mmcnitt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:38 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.