-5

I need help to make a very simple app for android that just creates text files on the click of a button. Can anyone help me with the coding? I already made the activity_main.xml set out already, but now I want to know how I can get a button to create a .txt or a .doc file which users can write in! (Something like a notepad)

KayKay
  • 1
  • 1
  • 2

1 Answers1

2

Try this

public void addTextToFile(String text) {
        File logFile = new File("sdcard/" + "MyFile.txt");
        if (!logFile.exists()) {
            try {
                logFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
            buf.append(text);
            buf.newLine();
            buf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77