Jump to content




[Off Topic] [Java] bufferedReader


  • You cannot reply to this topic
4 replies to this topic

#1 Xenthera

  • Members
  • 170 posts

Posted 30 July 2012 - 08:52 PM

Hi, i'm making a client for minecraft, and i'm not very good at Java at all.

What i'm trying to do is create a version.txt in the minecraft jar and print it on the main menu of the client, so when I update I don't have to change the code in eclipse, but all I have to do is update the txt.
Here's what I have so far:
BufferedReader a = new BufferedReader(new InputStreamReader((net.minecraft.src.GuiMainMenu.class).getResourceAsStream("/title/version.txt"), Charset.forName("UTF-8")));
	   
	    try {
   if(a.readLine() == null){
    drawString(fontRenderer, "Cannot Find version.txt", 2, height - 10, 0xffff);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
	   
  String bobby = a.toString();
 
  drawString(fontRenderer, bobby, 2, height - 10, 0xffff);
 


But that is basically failing at life at this point. Any help guys?

#2 xuma202

  • Members
  • 288 posts
  • LocationBonn Germany

Posted 30 July 2012 - 09:04 PM

This seems wrong

Quote

[/size]String bobby = a.toString();]
it will not do what you want it to do. Look here http://docs.oracle.c...eredReader.html to find out about how to use a BufferedReader

#3 Xenthera

  • Members
  • 170 posts

Posted 30 July 2012 - 09:12 PM

View Postxuma202, on 30 July 2012 - 09:04 PM, said:

This seems wrong

Quote

String[/size] bobby = a.toString();]
it will not do what you want it to do. Look here http://docs.oracle.c...eredReader.html to find out about how to use a BufferedReader

It helps a little, but I'm still not sure how to accomplish it reading one line of text out of the file, and printing it on the screen. May I remind you, Ive never messed around with java until now.

#4 xuma202

  • Members
  • 288 posts
  • LocationBonn Germany

Posted 30 July 2012 - 09:35 PM

Ok I have no enviorment at hand now but you can try this:
  • Does it at least print something? Try assigning bobby to a static String (bobby = "TEST":ph34r:/>
  • If 1. works try this
		    try {
  s = a.readLine();
   if(s == null){
    drawString(fontRenderer, "Cannot Find version.txt", 2, height - 10, 0xffff);
   } else {[color=#000000][size=2]drawString[/size][/color][color=#666600][size=2]([/size][/color][color=#000000][size=2]fontRenderer[/size][/color][color=#666600][size=2],[/size][/color][color=#000000][size=2] s[/size][/color][color=#666600][size=2],[/size][/color][color=#000000][size=2] [/size][/color][color=#006666][size=2]2[/size][/color][color=#666600][size=2],[/size][/color][color=#000000][size=2] height [/size][/color][color=#666600][size=2]-[/size][/color][color=#000000][size=2] [/size][/color][color=#006666][size=2]10[/size][/color][color=#666600][size=2],[/size][/color][color=#000000][size=2] [/size][/color][color=#006666][size=2]0xffff[/size][/color][color=#666600][size=2]);[/size][/color]}


  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

This should work if this line:
BufferedReader a = new BufferedReader(new InputStreamReader((net.minecraft.src.GuiMainMenu.class).getResourceAsStream("/title/version.txt"), Charset.forName("UTF-8")));
is correct.

#5 Xenthera

  • Members
  • 170 posts

Posted 30 July 2012 - 09:48 PM

View Postxuma202, on 30 July 2012 - 09:35 PM, said:

Ok I have no enviorment at hand now but you can try this:
  • Does it at least print something? Try assigning bobby to a static String (bobby = "TEST" :ph34r:/>
  • If 1. works try this
			try {
  s = a.readLine();
   if(s == null){
	drawString(fontRenderer, "Cannot Find version.txt", 2, height - 10, 0xffff);
   } else {[color=#000000][size=2]drawString[/size][/color][color=#666600][size=2]([/size][/color][color=#000000][size=2]fontRenderer[/size][/color][color=#666600][size=2],[/size][/color][color=#000000][size=2] s[/size][/color][color=#666600][size=2],[/size][/color][color=#000000][size=2] [/size][/color][color=#006666][size=2]2[/size][/color][color=#666600][size=2],[/size][/color][color=#000000][size=2] height [/size][/color][color=#666600][size=2]-[/size][/color][color=#000000][size=2] [/size][/color][color=#006666][size=2]10[/size][/color][color=#666600][size=2],[/size][/color][color=#000000][size=2] [/size][/color][color=#006666][size=2]0xffff[/size][/color][color=#666600][size=2]);[/size][/color]}


  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

This should work if this line:
BufferedReader a = new BufferedReader(new InputStreamReader((net.minecraft.src.GuiMainMenu.class).getResourceAsStream("/title/version.txt"), Charset.forName("UTF-8")));
is correct.
No worries, after a few more minutes of research and mucking about in eclipse I finally got it.
StringBuffer contents = new StringBuffer();
	    BufferedReader reader = null;
	    String bobby = "";
	    try {
		   
		 reader = new BufferedReader(new InputStreamReader((net.minecraft.src.GuiMainMenu.class).getResourceAsStream("/title/version.txt"), Charset.forName("UTF-8")));
		    String text = null;
		   
		   
		    while ((text = reader.readLine()) != null) {
			    contents.append(text)
					    .append(System.getProperty(
							    "line.separator"));
			    bobby = contents.toString();
		    }
		   
	    } catch (Exception exception) {
		   
	    }
	   
	    if (bobby == ""){
		 bobby = "Cannot Find Version.txt";
	    }
	    drawString(fontRenderer, bobby, 2, height - 10, 0xffff);
Seems to work fine, I'm going to try to compile it and add it to a real minecraft client now. Thanks for your help





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users