Hi,
I would like to know how to create buttons for my main menu, read/write files(for save data), and read online data for pruchase-able data later on. Sorry if this is nooby as i dont even know where to start.
Thanks,
Drok
Hi,
I would like to know how to create buttons for my main menu, read/write files(for save data), and read online data for pruchase-able data later on. Sorry if this is nooby as i dont even know where to start.
Thanks,
Drok
What are you using? Swing? LibGDX? Something else?
What have you tried? What has google told you? Which exact part of this are you stuck on?
Can you create a separate example program that just prints something to the console when you click a button?
Can you create a separate example program that just writes to a file?
Can you create a separate example program that just reads from a file?
Can you create a separate example program that just reads from online data (do you mean a URL or a database or something else)?
The point of those questions is to encourage you to break this down into smaller pieces. Focus on one step at a time, and post an MCVE when you get stuck.
Good luck!
Ok i got it to work but it doesnt work how i expected. I need it to read specific lines(such as line 2, 6, 10) and take the int/string value from that and apply it somewhere else(i think i said that right). Here is what i curently have:
package alfitaria.entity.player;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class LoadPlayerFiles {
public static void Load() throws IOException{
LoadPlayerFiles text = new LoadPlayerFiles();
//treat as a small file
List<String> lines = text.readSmallTextFile(FILE_NAME);
log(lines);
lines.add("This is a line added in CODE.");
text.writeSmallTextFile(lines, FILE_NAME);
//treat as a large file - use some buffering
text.readLargerTextFile(FILE_NAME);
lines = Arrays.asList("Down to the Waterline", "Water of Love");
text.writeLargerTextFile(OUTPUT_FILE_NAME, lines);
}
final static String FILE_NAME = "C:\\Temp\\input.txt";
final static String OUTPUT_FILE_NAME = "C:\\Temp\\output.txt";
final static Charset ENCODING = StandardCharsets.UTF_8;
//For SMALLER files
/**
Note: the javadoc of Files.readAllLines says it's intended for small
files. But its implementation uses buffering, so it's likely good
even for fairly large files.
*/
List<String> readSmallTextFile(String aFileName) throws IOException {
Path path = Paths.get(aFileName);
return Files.readAllLines(path, ENCODING);
}
void writeSmallTextFile(List<String> aLines, String aFileName) throws IOException {
Path path = Paths.get(aFileName);
Files.write(path, aLines, ENCODING);
}
//For larger files
void readLargerTextFile(String aFileName) throws IOException {
Path path = Paths.get(aFileName);
try (Scanner scanner = new Scanner(path, ENCODING.name())){
while (scanner.hasNextLine()){
//PROCESS each line in some way
log(scanner.nextLine());
}
}
}
void readLargerTextFileAlternate(String aFileName) throws IOException {
Path path = Paths.get(aFileName);
try (BufferedReader reader = Files.newBufferedReader(path, ENCODING)){
String line = null;
while ((line = reader.readLine()) != null) {
//process each line in some way
log(line);
}
}
}
void writeLargerTextFile(String aFileName, List<String> aLines) throws IOException {
Path path = Paths.get(aFileName);
try (BufferedWriter writer = Files.newBufferedWriter(path, ENCODING)){
for(String line : aLines){
writer.write(line);
writer.newLine();
}
}
}
private static void log(Object aMsg){
System.out.println(String.valueOf(aMsg));
}
}
P.S. this will be used to load worlds and player files(such as inventory, id, health, maxhealth ect) if it helps.
Then you need to just keep track of the line number you’re on, and either parse the line or don’t depending on the current line number.
You can also take a look at the Integer class for useful methods for parsing a String value.
Also the Scanner class you are already using has support for reading numbers via the nextInt(), nextFloat() (or something similar to these) etc. methods.
Ok i got it to read the file that is located on the pc but what if i wanted to read a pastebin.com file for example from here: http://pastebin.com/dpQWKeHU. i need to do this for donations/personal/nonhackable data later on(i hope this is the right way to do it). Thanks. Also how do i add buttons to my main menu?