Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ThaUnknownProgga

[java] File Input into some sort of object

This topic is 6587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m fairly new to this Java stuff, so this problem really has me stumped even though I know it''s a fairly simple thing. I want to open a text file, read it''s contents, and display them on the screen. Preferably I''d like to read them in an initialization phase and store them, newlines and all, in some kind of object. Then I''d like to display them ( i guess in public void paint) with a font. Help?

Share this post


Link to post
Share on other sites
Advertisement
import java.io.*;
import java.util.*;

Vector vector = new Vector();
boolean read = true;
String temp;

public void initFile() {
File file = new File( "some/directory/textfile.txt" );
FileInputStream fis = new FileInputStream( file );
InputStreamReader isr = new InputStreamReader( fis );
BufferedReader br = new BufferedReader( isr );

while( true ) {
temp = br.readLine();
if( temp == null ) {
break;
}
vector.add( temp );
}

You include all of the try/catch blocks and error checking but that basically should do it. The just do a drawString() in your graphics routine.

I wanrned you! Didn''t I warn you?! That colored chalk was forged by Lucifer himself!

Opere Citato

Share this post


Link to post
Share on other sites
Alright first I had to use addElement instead of add, this is correct right? To display it I thought I could do a drawString(vector.toString()...) but that doesn''t print anything. Also, to set the font to say size 12 Arial, do I do this?:

Font font;
font = new Font("Arial", Font.PLAIN, 12);
setFont(font);

Share this post


Link to post
Share on other sites
AddElement is just fine. Are you trying to print out to console or to a graphics object? I am not too clear on that. If it is to console then a System.out.println( vector.remove().toString() ) will work. Also from the code you posted you were try to print the toString() of the vector not the contents of the vector, hence the remove(). Remove returns the object you want to remove along with deleting it from the vector.

I wanrned you! Didn''t I warn you?! That colored chalk was forged by Lucifer himself!

Opere Citato

Share this post


Link to post
Share on other sites
Well, I am trying to print to the graphics object, hence the use of drawString. I don''t want to remove from the Vector when I write it, because I may have to write again. I tried Vector.elementAt(1).toString() in my call to drawString, but still nothing is being written.

Also, font?

Share this post


Link to post
Share on other sites
I have never used fonts so can''t help there. First I would check to see if the objects in the vector actually hold strings. Printing them out to the console as you read them works well. Also if elementAt(1) is all white space you won''t see it so keep that in mind if it turns out that the vector is actually holding some strings.

I wanrned you! Didn''t I warn you?! That colored chalk was forged by Lucifer himself!

Opere Citato

Share this post


Link to post
Share on other sites
ah, Yes I tried prionting the strings to the console and got nothing. I don''t think anything is actually being retrieved from the file. I tried doing just File file = new File("junk.txt"); do I need to add a path in there even if this is the same location as my class file? I tried doing that to, "file:/C:/jdk1.2.2/bin/junk.txt" and it still didn''t print anything to either the console or the graphics object.

Share this post


Link to post
Share on other sites
Try this out:


import java.io.*;

public class TextRead {
private StringBuffer sb = new StringBuffer();

public TextRead() {
initialize();
//just to prove this worked
System.out.println(sb.toString());
}

public void initialize() {
char[] buffer = new char[1000];
int numRead = 0;
try {
FileReader fr = new FileReader("test.txt");

while (numRead != -1) {
numRead = fr.read(buffer,0,1000);
if (numRead != -1) {
sb.append(buffer);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
TextRead tr = new TextRead();
System.exit(0);
}
}

Share this post


Link to post
Share on other sites
If it is in the same directory it should be picked up. Try seeing if the file exists with file.exists(). I am not sure if this is already checked for otherwise some sort of exception would be thrown by one of the readers. I can''t say that I have ever had as much problems with loading a file as you are having.

I wanrned you! Didn''t I warn you?! That colored chalk was forged by Lucifer himself!

Opere Citato

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!