Solved [Help] Read contents of text file

Discussion in 'Plugin Development' started by FisheyLP, Jun 2, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    FisheyLP

    im not using textfiles for saving / loading stuff from it (if i would not say it, many people would say "USE BUKKIT CONFIG API"...). How can i read a text file and send the player the contents of it?
     
  2. Offline

    Onlineids

  3. Offline

    FisheyLP

    doesnt work. it just shows me the filename and the path to it. heres my code:
    Code:java
    1. StringBuilder sb = new StringBuilder();
    2.  
    3. Scanner sc = new Scanner(currentpath
    4. + item.getItemMeta().getDisplayName());
    5. //currentpath is the path and the itemname is the filename. both of them is e.g plugins/Essentials/books.txt
    6. while (sc.hasNext()) {
    7. sb.append(sc.nextLine());
    8. }
    9. for (String zeile : sb.toString().split("\\n")) {
    10. p.sendMessage(zeile);
    11. }
     
  4. Offline

    Onlineids

    Its \n not \\n, I mention later in the post that was a typo :p
     
  5. Offline

    Garris0n

  6. Offline

    Onlineids

  7. Offline

    Garris0n

    -Half the threads here.
     
  8. Offline

    FisheyLP

    Garris0n
    does not work. can you give me a example?
     
  9. Offline

    Onlineids

    Sarcasm off
     
  10. Offline

    FisheyLP

    it shows me too just the path+file name :(

    if i use it on books.txt in the path plugins/essentials/books.txt, it doesnt show me the contents, it show me this path

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  11. Offline

    Garris0n

    Are you serious?
     
  12. Offline

    FisheyLP

    i searched on the website and found the scanner util. i tested it and it doesnt work
     
  13. Offline

    Garris0n

    What doesn't work?

    Post your code if you have issues.
     
  14. Offline

    FisheyLP

    i already posted my code and i said what doesnt work... but i can do it again xD
    it dont show me the contents of the file, it shows me just the filename :(
    here is code:
    Code:java
    1.  
    2. StringBuilder sb = new StringBuilder();
    3.  
    4. Scanner sc = new Scanner(path);
    5. while (sc.hasNext()) {
    6. sb.append(sc.nextLine());
    7. }
    8. for (String zeile : sb.toString().split("\n")) {
    9. p.sendMessage(zeile);
    10. }
    11. }
    12.  
     
  15. Offline

    Garris0n

    What is "path"?
     
  16. Offline

    aman207

    This might be of use to you

     
  17. Offline

    FisheyLP

    the path is this: plugins\essentials\books.txt
    (its just a example path)
     
  18. Offline

    Garris0n

    I mean what is the actual variable. This is why people need to post full code. Show where you initialize it, I can't see what it is.
     
  19. Offline

    Axe2760

    FisheyLP If your file is in a plugin data folder.. path = new File(plugin.getDataFolder(), relativeFilePath);

    relativeFilePath is path to the file you want, relative to the datafolder

    example: /plugins/MyPlugin/awesomeFile.swag => new File(myPlugin.getDataFolder(), "awesomeFile.swag");
     
  20. Offline

    FisheyLP

    too doesnt work... :( this time it doesnt show me anything

    That is how i get the path (My plugin is called Servergui...)
    Code:java
    1.  
    Code:java
    1.  
    2. @EventHandler
    3. public void onInvClick(InventoryClickEvent e) {
    4. Player p = (Player) e.getWhoClicked();
    5. ItemStack item = e.getCurrentItem();
    6. String invname = e.getInventory().getName();
    7. String currentpath = invname.substring(7);
    8. String newpath = "";
    9. try {
    10. if (invname.equals(inv.getName())) {
    11. if (item != null) {
    12.  
    13. e.setResult(Result.DENY);
    14.  
    15. if (item.getType() == Material.CHEST) {
    16. String itemname = item.getItemMeta().getDisplayName();
    17. newpath = currentpath + itemname;
    18. createGUI(newpath);
    19. }
    20. if (item.getType() == Material.SLIME_BALL) {
    21. if (!invname.equals("Server\\")) {
    22. File f = new File(currentpath.substring(0,
    23. currentpath.length() - 1));
    24. if (f.getParentFile() == null) {
    25. newpath = "";
    26. } else {
    27. newpath = f.getParentFile().getPath();
    28. }
    29. createGUI(newpath);
    30. }
    31. }
    32. if (item.getType() == Material.PAPER
    33. || item.getType() == Material.EMPTY_MAP) {
    34. try {
    35. Scanner sc = new Scanner(new File(currentpath
    36. + item.getItemMeta().getDisplayName()));
    37.  
    38. while (sc.hasNext()) {
    39. p.sendMessage(sc.next() + "\n");
    40. }
    41. sc.close();
    42. } catch (Exception ex) {
    43. }
    44.  
    45. }
    46. p.closeInventory();
    47. p.openInventory(inv);
    48. }
    49. }
    50. } catch (Exception exception) {
    51. }
    52. }
    53. [SIZE=2][/SIZE]


    (Slime ball is to go back one path, chests are folders, and emty maps and paper are .txt and .yml files. i want if the player clicks a txt or yml file (i defined it in the createGUI(String path); method which files are which items) it shows the player the contents of the file

    i know that. just read my code i posted before... its much complicated

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  21. Offline

    Axe2760

    FisheyLP Sorry, I'm of little help... I'm just fairly certain the problem isn't with the code to read the file, it's the code to get the path.
     
  22. Offline

    FisheyLP

    Axe2760
    do you know how to read the contents of the file?
     
  23. Offline

    teozfrank

    rofl

    read this article, it has examples too :D

    http://www.javapractices.com/topic/TopicAction.do?Id=42

    or watch this video:

    https://buckysroom.org/videos.php?cat=31&video=18046

    derp just seen this was posted already xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  24. Offline

    FisheyLP

    EDIT: WORKS YEEEEEEEEEEEAH THANK YOU ALL!!! IT WAS JUST THE §r COLORCODE FROM THE ITEMNAME THAT MESSED IT ALL UP :DDDD
     
Thread Status:
Not open for further replies.

Share This Page