The Artima Developer Community
Sponsored Link

Java Answers Forum
Help on read input file from Java

4 replies on 1 page. Most recent reply: Mar 27, 2002 5:01 AM by derrick

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
derrick

Posts: 3
Nickname: dragonboy
Registered: Mar, 2002

Help on read input file from Java Posted: Mar 26, 2002 3:27 AM
Reply to this message Reply
Advertisement
hi guys,
I am just wondering how do you read input from a text file? What class should i be using?
Thanks


Rajeshwari

Posts: 4
Nickname: rrb
Registered: Mar, 2002

Re: Help on read input file from Java Posted: Mar 26, 2002 3:38 AM
Reply to this message Reply
you can use FileReader class of java to read the txt file.you will get the output in a character array.

derrick

Posts: 3
Nickname: dragonboy
Registered: Mar, 2002

Re: Help on read input file from Java Posted: Mar 26, 2002 4:13 AM
Reply to this message Reply
Sorri i am really new to Java, let said i created a text file called Example.txt and inside there is "Hello,World".
Now how do u use the FileReader class to read input from this file? Can you give me an example as i said i am very new to this language!
Thanks

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Help on read input file from Java Posted: Mar 26, 2002 6:37 AM
Reply to this message Reply
To check how does it work, compile and run Test.java and don't forget to change the file name. Ideally, you should place your code in try-catch block. To keep it simple I didn't use try-catch here.

Thanks
Kishori

///////////////////Test.java//////////
class Test {	
 
	public static void main ( String[] args ) throws Exception {
		
		java.io.FileReader fr = new java.io.FileReader ( "c:\\kishori\\message.txt" ) ;
		
		// You can read only in chars and bytes using FileReader
		// To read a line at a time let us create a BufferedReader out of FileReader
		java.io.BufferedReader br = new java.io.BufferedReader ( fr ) ;
		
		String str = null ;
		
		// Read the text on eline at a time and display it on console
		while ( ( str = br.readLine()) != null ) {
			System.out.println ( str ) ;
		}		
	}
}

derrick

Posts: 3
Nickname: dragonboy
Registered: Mar, 2002

Re: Help on read input file from Java Posted: Mar 27, 2002 5:01 AM
Reply to this message Reply
Thanks for your help :)
I also got another probs i try to use the SimpleInput Class to ignore punctuation marks in the text file but it doesn't work.
This is what i used:
SimpleInput keyboard = new SimpleInput();
keyboard.setDelimiters("?\;:,.");
Can you help?
What have i done wrong?????

Flat View: This topic has 4 replies on 1 page
Topic: applet servlet communcation with SSL Previous Topic   Next Topic Topic: SSLava from phoas.com

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use