Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 14th, 2002, 02:13 PM
Marky_Mark Marky_Mark is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: On a screen near you
Posts: 498 Marky_Mark User rank is Private First Class (20 - 50 Reputation Level)Marky_Mark User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Reading Files

Java has so many streams to use im lost


I need to read a text file into a variable so far ive got this


Code:


InputStream fis = new FileInputStream(f); 

while ((x = fis.read())!= -1) {
            System.out.print(x); 
            }




This outputs binary data, how do i get acsii data into a Srting variable


Is this the best method for reading text files on windows


Mark
__________________
100 trillion calculations per nanosecond

Reply With Quote
  #2  
Old March 14th, 2002, 04:39 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,966 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 52 m 24 sec
Reputation Power: 189
binary data==ascii data.

Code:
InputStream fis = new FileInputStream(f); 

while ((x = fis.read())!= -1) {
            s=new String(x); 
                        if (s.equals("your-data-here")) {
                                 (do processing.....)
                        }
            }


does this help?

anyway, not sure 100% but there should be a StringStream() - is there? (i am not a java pro......) but i am sure there is a LineReader() class that you should have a look at
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old March 14th, 2002, 06:19 PM
Marky_Mark Marky_Mark is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: On a screen near you
Posts: 498 Marky_Mark User rank is Private First Class (20 - 50 Reputation Level)Marky_Mark User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Its not what i wanted


Code:


s = new String(x); 




This line causes an error because x is binary data


I need the binary data converted to acsii


Mark

Reply With Quote
  #4  
Old March 15th, 2002, 01:43 PM
tron's Avatar
tron tron is offline
SwollenMember
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: the master control
Posts: 264 tron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 14 m 57 sec
Reputation Power: 14
would using java.lang.Byte help?

then you could just say:

Byte.toString(x);

Reply With Quote
  #5  
Old March 30th, 2002, 01:24 AM
aabha aabha is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: India
Posts: 7 aabha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to aabha
InputStream fis = new FileInputStream(f);

String data = "";

do{
i = fis.read();
if (i != -1) data = data + (char)i;
}while(i != -1);

System.out.println(data);

this will append all values from the file to variable data...

Reply With Quote
  #6  
Old March 31st, 2002, 11:58 PM
Lord MJ Lord MJ is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 34 Lord MJ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 57 m 2 sec
Reputation Power: 12
Send a message via AIM to Lord MJ Send a message via Yahoo to Lord MJ
Why not do this,

BufferedReader buf = new BufferedReader(new FileReader(f));


System.out.println("buf.ReadLine);

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Reading Files

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap