This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Xenthera's profile picture

[Off Topic] [Java] bufferedReader

Started by Xenthera, 30 July 2012 - 07:52 PM
Xenthera #1
Posted 30 July 2012 - 09: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?
xuma202 #2
Posted 30 July 2012 - 10:04 PM
This seems wrong
[quote String bobby = a.toString();] it will not do what you want it to do. Look here http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html to find out about how to use a BufferedReader
Xenthera #3
Posted 30 July 2012 - 10:12 PM
This seems wrong
[quoteString 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.
xuma202 #4
Posted 30 July 2012 - 10:35 PM
Ok I have no enviorment at hand now but you can try this:
  1. Does it at least print something? Try assigning bobby to a static String (bobby = "TEST":ph34r:/>/>
  2. 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.
Xenthera #5
Posted 30 July 2012 - 10:48 PM
Ok I have no enviorment at hand now but you can try this:
  1. Does it at least print something? Try assigning bobby to a static String (bobby = "TEST" :ph34r:/>/>
  2. 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