Android的 - 创建文本文件 [英] Android - Creating Text Files

查看:155
本文介绍了Android的 - 创建文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,使Android的一个非常简单的应用程序,只需一个点击按钮创建文本文件。谁能帮我与编码?我已经取得的activity_main.xml中出发了,但现在我想知道我怎么能得到一个按钮来创建一个.txt或用户可以编写一个.doc文件! (有点像记事本)

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)

推荐答案

试试这个

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();
        }
    }

这篇关于Android的 - 创建文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆