Create, read and write into .txt

Discussion in 'Plugin Development' started by TerraPlay, Jan 22, 2011.

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

    TerraPlay

    Hello,

    can somebody tell me how i can create a new xyz.txt if it's not already created? This should happen while the onEnable part. After that i want to write some strings into it, one per line. Then i want to read them and claim them as strings.

    Any help? Thanks!
     
  2. Offline

    blaatz0r

    Hope this helps, untested though :)

    Code:
    public void onEnable() {
             // the this.getDataFolder() gets the location of your MyPlugin.jar, the second argument is the relative path from the jar file
            File f = new File(this.getDataFolder(), "path/to/xyz.txt");
            String[] s = ...; // Make the array yourself
            this.createFile(f, s);
    }
    
    ...
    
    public void createFile(File file, String[] output) {
            BufferedWriter bwriter = null;
            FileWriter fwriter = null;
            try {
                file.createNewFile();
                fwriter = new FileWriter(file, true);
                bwriter = new BufferedWriter(fwriter);
    
                // Write some lines
                for (String s : output) {
    
                    bwriter.write(s);
                    bwriter.newLine();
                }
    
                bwriter.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bwriter != null) {
                        bwriter.close();
                    }
                    if (fwriter != null)
                        fwriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    ...
    
    public String[] readLines(File f) throws IOException {
            FileReader fileReader = new FileReader(f);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            List<String> lines = new ArrayList<String>();
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                lines.add(line);
            }
            bufferedReader.close();
            return lines.toArray(new String[lines.size()]);
        }
     
  3. Offline

    TerraPlay

    Thanks, but could you explain what i have to type instead of the ...?
    Code:
    ...
    String [] s = ...;
    ...
     
  4. Offline

    blaatz0r

    That's up to you. You said you wanted to write some strings to it, so I suppose you have a string array somewhere that you want to write to xyz.txt.

    - OnEnable() just specifies the xyz.txt file and calls createFile()
    - createFile() creates the file if it doesn't already exists and writes the string array to it
    - readlines() reads a file back to a string array
     
  5. Offline

    TerraPlay

    Thank you very much =)
    Please don't close this, i'll post here if I need help again.
    --- merged: Jan 22, 2011 9:10 PM ---
    Hm, the code gives me an error. (Maybe the error message is different, but i have german windows so it gives me the german error message)
    Code:
    java.io.IOException: the system can't locate the path
    That's my createfile code:
    Code:
    public void onEnable() {
    
            File f = new File(this.getDataFolder(), "messages.txt");
            String[] s = {"Line1","Line2","Line3","Line4","Line5"};
            this.createFile(f, s);
        }
    
        public void createFile(File file, String [] input) {
            BufferedWriter bwriter = null;
            FileWriter fwriter = null;
            try {
                file.createNewFile();
                fwriter = new FileWriter(file, true);
                bwriter = new BufferedWriter(fwriter);
    
                // Write some lines
                for (String s : input) {
    
                    bwriter.write(s);
                    bwriter.newLine();
                }
    
                bwriter.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bwriter != null) {
                        bwriter.close();
                    }
                    if (fwriter != null)
                        fwriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page