The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Reading Files
Discuss Reading Files in the Java Help forum on Dev Shed. Reading Files Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 14th, 2002, 02:13 PM
|
|
Contributing User
|
|
Join Date: Sep 2001
Location: On a screen near you
Posts: 498

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
|

March 14th, 2002, 04:39 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
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 
|

March 14th, 2002, 06:19 PM
|
|
Contributing User
|
|
Join Date: Sep 2001
Location: On a screen near you
Posts: 498

Time spent in forums: < 1 sec
Reputation Power: 12
|
|
Its not what i wanted
This line causes an error because x is binary data
I need the binary data converted to acsii
Mark
|

March 15th, 2002, 01:43 PM
|
 |
SwollenMember
|
|
Join Date: Jun 2000
Location: the master control
Posts: 264
Time spent in forums: 13 h 14 m 57 sec
Reputation Power: 13
|
|
|
would using java.lang.Byte help?
then you could just say:
Byte.toString(x);
|

March 30th, 2002, 01:24 AM
|
|
Member
|
|
Join Date: Mar 2002
Location: India
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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...
|

March 31st, 2002, 11:58 PM
|
|
Contributing User
|
|
Join Date: Aug 2001
Posts: 34
Time spent in forums: 4 h 57 m 2 sec
Reputation Power: 12
|
|
|
Why not do this,
BufferedReader buf = new BufferedReader(new FileReader(f));
System.out.println("buf.ReadLine);
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|