-1

Im new to Java and i did start by coding a IRC Bot (pircbot).

My Bot should be used as Twitch.tv Bot and til now everythign works fine.

Now i just encountered a Problem or i would like to change a thing;

Ive cleaned some Code up and wrote 3 Basic Strings;

public String OwnerChannel;
public String Coinname;
public String Owner;


OwnerChannel = "xyz";
Coinname = "xyz";
Owner = "xyz";

And Those are my Main inputs. i want to switch to a different channel on twitch i just need to edit those 3 parts.

NOW, i would liek to read this from a .txt file. is that possible?

for Example;

filename: BOTsettings.txt

and the inside should look like

Owner = "xyz"            // Edit "xyz" for changes
OwnerChannel = "xyz"     // Edit "xyz" for changes
Coinname = "xyz"         // Edit "xyz" for changes

I would be happy if some1 could tell me how i can manage this one here :D

greetings and thanks for your time!

5 Answers5

1

I would use java.util.Properties. It expects configuration file in following format by default:

key1=value1
key2=value2

Loading these configuration properties is really easy then:

private Properties loadProperties() throws IOException {
    Properties props = new Properties();
    props.load(new FileReader(new File("conf.txt")));
    return props;
}

Then you can read the properties by following way:

Properties props = loadProperties();
System.out.println(props.get("key1");
Michal
  • 550
  • 7
  • 14
  • Hey, this sounds quite easy! – user3043459 Dec 18 '13 at 16:07
  • I tried this now exactly the way u posted it. with the second code i got an error; Syntax error on tokens, AnnotationName expected instead any help or did i do soemthing wrong? – user3043459 Dec 18 '13 at 16:33
  • I am sorry, there is missing one closing bracket: System.out.println(props.get("key1")); and the last two lines must be in a method: i.e. main() ... Or where do you have exactly problem? – Michal Dec 19 '13 at 09:00
  • thats the problem i guess. im new to it :D i need to put it in a method? i just wrote it up in my bot :O – user3043459 Dec 19 '13 at 11:30
  • I do not know how your bot works and I'm not sure I can help you :(. Maybe try to read a basic Java Hello World tutorial and then try to use what I recommended above. – Michal Dec 19 '13 at 11:41
  • do you have an twitch.tv Account? i would liek to show you what ive doen so far – user3043459 Dec 19 '13 at 12:07
0

You dont have to use file. You can read this setings from command line or from system properties. You can run your code with option -D

 -DOwner=axyz

And read this value like this:

String value = System.getProperty("Owner");

But if you want read from file then I suggest to use properties. Let the java do the work for you. Look at this post about properties: How to use Java property files?

Community
  • 1
  • 1
bary
  • 1,699
  • 2
  • 15
  • 24
0

To preface, file reading is definitely a thing in Java.

ex)


InputStream in = new FileInputStream("inputFile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String currentLine;

try {
  while((currentLine = br.readLine()) != null)
  {
    // output the current line
    System.out.println(currentLine);
  }

} catch(Exception e) {
  System.out.println(e);
} finally {
  in.close();
}

To read the entire file in one go :


InputStream in = new FileInputStream("foo.txt");
String fileContents;

try {
  fileContents = IOUtils.toString(inputStream);
} finally {
  in.close();
}

Reading a file can be done using a Reader or InputStream. Each of these are interfaces, meaning they are a contract that describes how anything considered a Reader or InputStream must function. For the Reader or InputStream to function it must be implemented in some specific manner.

In the above cases, in is declared on the left as a InputStream, and the specific type of InputStream (how it is implemented) is declared on the right.

Note that the implementations of these interfaces are also listed in there doc.

Another key thing to note is the use of in.close();. A finally statement is used to ensure a piece of code is executed even if the application errors out. This is done to ensure the file handle is released.

Storing Configuration Properties

Configuration properties are generally simple key value pairs, such as :


key1=value1
key2=value2

Though in some circumstances it may be beneficial to store more complex data structures such as lists or dictionaries / maps. For this a better serialization format can be used. A format generally better for storing data like this is YAML, and an easy subset of YAML to begin using, JSON.

JSON is a structure for expressing Javascript objects, which are simple dictionaries / maps of keys to values. Value may be strings, numbers, dictionaries, and lists. A dictionary or list may contain values. This allows for data considered more complex than a simple key value store to be expressed.

Dawson
  • 4,391
  • 2
  • 24
  • 33
  • i will try the java.util.properties thing below first. if it doesnt work the way it should im definetly gonna try urs – user3043459 Dec 18 '13 at 16:10
  • I think it'll do the trick for you. Another option is to store your properties in JSON or YAML. This would allow for you to express list and dictionaries in your properties, rather than just key values. If you want I could put up an example of this as well. – Dawson Dec 18 '13 at 16:12
  • that would be awesome. examples are always a great way to learn better for a beginner like me ^_^ – user3043459 Dec 18 '13 at 16:14
0

first paste your txt file inside of assests then use this below piece of code .

AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");

Or place your txt fille inside of raw folder and use this

InputStream is = getResources().openRawResource(R.raw.test);
Amsheer
  • 7,046
  • 8
  • 47
  • 81
-1

Try this:

try
  {
  FileInputStream in = new FileInputStream("BOTsettings.txt");
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;

  while((strLine = br.readLine())!= null)
  {
   System.out.println(strLine);
  }

  }catch(Exception e){
   System.out.println(e);
  }

And then just store strLine in an array or something like this.

dehlen
  • 7,325
  • 4
  • 43
  • 71