使用FileWriter从文本文件中读取文本,然后将内容写入pdf并将其导出

时间:2019-06-15 18:01:42

标签: android filereader filewriter android-file

在我的项目中,我必须将一些数据写入文本文件。现在,在那之后的某些过程中,我必须从该文本文件中读取这些数据,并将其写入并导出为pdf文件,然后将其保存在本地存储中。我已经做完了,直到写入和读取文本数据,但无法解决pdf部分。在ExportData函数中,应进行阅读和pdf创建。任何帮助,将不胜感激。谢谢。

String Filename = "abc.txt";

public void createDataFile(HomeActivity c) {
    folder = new File(c.getFilesDir(), "folderABC");
    if (!folder.exists()) {
        folder.mkdir();
    }
    File file = new File(folder, FileName);
    try {
        FileWriter fw = new FileWriter(file);
        Toast.makeText(HomeActivity.this, file.getAbsolutePath(), Toast.LENGTH_LONG).show();
        fw.flush();
        fw.close();
        writeData();
    } catch (Exception e) {
        Toast.makeText(HomeActivity.this, "File not created !", Toast.LENGTH_LONG).show();
    }

}
public void writeData() throws IOException {
        File file = new File(folder, FileName);
        FileWriter fw = null;
        try {
            fw = new FileWriter(file);
            fw.append("hello !");
            fw.close();
        } catch (FileNotFoundException fe) {
            Toast.makeText(HomeActivity.this, "Can't Write Data !", Toast.LENGTH_LONG).show();

        }
    }
public void ExportData() throws IOException {
    PdfDocument document = new PdfDocument();
    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(500, 700, 1).create();
    PdfDocument.Page page = document.startPage(pageInfo);
    File file = new File(folder, FileName);
    FileReader fr = null;
    try {
        fr = new FileReader(file);

        fr.close();
    } catch (FileNotFoundException fe) {
        Toast.makeText(HomeActivity.this, "Can't Open File !", Toast.LENGTH_LONG).show();

    }
}

0 个答案:

没有答案