Results 1 to 7 of 7
  1. #1
    Blacknight is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default Readling .txt file (Text Scripting)

    I need to read a text file over and over again, and also specify which line to read.

    Example TextScript:
    Java Code:
    Walk(400,400)
    Walk(400,401)
    Line:(1)
    So basically, what should happen if a text script looks like that is.

    Walk to Coords: 400,400 Then Walk to coords 400,401 next depending on what number is after 'Line:' - that's what line I need to switch to.

    This is what I have so far:

    Java Code:
    try{
    FileInputStream fstream = new FileInputStream("Scripts/" + RunningScript + ".txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
    				while ((strLine = br.readLine()) != null)   {					
    	  if (strLine.trim().toLowerCase().startsWith("walk") ) {
    	  int x = Integer.parseInt(strLine.substring(strLine.indexOf('(') + 1, strLine.indexOf(',')));
    	  int y = Integer.parseInt(strLine.substring(strLine.indexOf(',') + 1, strLine.indexOf(')')));
    	  while (x != PlayerX() && y!= PlayerY() && ScriptRunning != false) { 
    	  Walk(x , y);
    	  Wait(1500);
    	  }
    	  }  
    		 if (strLine.trim().toLowerCase().startsWith("line:") ) {
    		 int GotoLine = Integer.parseInt(strLine.substring(strLine.indexOf('(') + 1, strLine.indexOf(')'))); // line to goto
    		 }
    			
    }
        in.close();
        }catch (Exception e){
          System.err.println("Error: " + e.getMessage());
        }
    	}
    Thanks,

    Blacknight

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    You can use BufferedReader br=new BufferedReader(new FileReader("file.txt")); instead of all these statements you have now )
    It is all depends of the file data contains. Binary or text you need to get out.
    In the case you need reading text BufferedReader is well enough )

  3. #3
    Blacknight is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Webuser View Post
    You can use BufferedReader br=new BufferedReader(new FileReader("file.txt")); instead of all these statements you have now )
    It is all depends of the file data contains. Binary or text you need to get out.
    In the case you need reading text BufferedReader is well enough )
    How can I switch to the lines?
    Last edited by Blacknight; 03-11-2009 at 09:33 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I would keep things simpler, in that I'd read the whole text file into my app, possibly into an ArrayList of String, or perhpas better an ArrayList of ScriptCommand objects (or whatever you want to call an object that holds each statement). Then after I'm done reading in the file, I'd close it and be done with it, and I'd start working on following the commands.

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Yea, what Fubarable says.

    Read it all once, put it in an array, and work with that array, rather than the file.

    Much easier.
    I die a little on the inside...
    Every time I get shot.

  6. #6
    Blacknight is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default

    Thanks for reply,


    So like this?:
    Java Code:
    try{
    FileInputStream fstream = new FileInputStream("Scripts/" + RunningScript + ".txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        ArrayList<String> Lines = new ArrayList<String>();
    				while ((strLine = br.readLine()) != null)   {					
    
                                   Lines.add(strLine);
    
    		 
    			
    }
        in.close();
        }catch (Exception e){
          System.err.println("Error: " + e.getMessage());
        }
    	}
    but it's a String array - won't that give me an error everytime strLine == a number?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) If you look up the API for BufferedReader (or any "Reader"), what does readLine() return? Is it ever a number?
    2) When you try this out, are any errors thrown?

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 05:01 PM
  2. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 05:04 PM
  3. how to form a text file
    By tOpach in forum New To Java
    Replies: 6
    Last Post: 09-18-2008, 09:52 PM
  4. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM
  5. Converting text file(.txt) to JPG file(.jpg) in java
    By javadeveloper in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 05:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •