Làm cách nào để tạo tệp.txt và chèn dữ liệu trên tệp có nội dung của một số biến trên mã của tôi ví dụ: population [] []; trên Android, do đó sẽ có các tệp thư mục trên gói của chúng tôi trong trình khám phá tệp (dữ liệu/dữ liệu/gói/tệp/ourfiles.txt) Cảm ơn bạnCách tạo tệp văn bản và chèn dữ liệu vào tệp đó trên Android
Trả lời
Sử dụng mã này bạn có thể viết tệp văn bản trong SDCard cùng với bạn cần phép thiết lập trong android manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
này là mã:
public void generateNoteOnSD(Context context, String sFileName, String sBody) {
try {
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, sFileName);
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
trước khi viết file cũng kiểm tra xem SDCard của bạn được Mounted & trạng thái lưu trữ bên ngoài của bạn là ghi
Environment.getExternalStorageState()
Kiểm tra android documentation. Đó là trong thực tế không có nhiều khác biệt so với tiêu chuẩn java io xử lý tập tin để bạn cũng có thể kiểm tra tài liệu đó.
Một ví dụ từ các tài liệu android:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
Tôi thử điều này: File myfile = new File ("genetic_algorithm.txt"); thử \t \t { \t \t Chuỗi chuỗi = "hello world!"; \t \t FileOutputStream fos = openFileOutput (myfile, Context.MODE_PRIVATE); \t \t fos.write (string.getBytes()); \t \t fos.close(); \t \t} \t \t catch (Throwable t) { \t \t \t Log.e (TAG, "lỗi"); \t \t} nó vẫn không thể hoạt động> _ < – Michelle
kiểm tra nhật ký của bạn về ngoại lệ bạn có. "nó vẫn không thể làm việc" không phải là một mô tả tốt về vấn đề này. Dựa trên nhật ký lỗi, chúng tôi có thể giúp bạn.Đây là cách xử lý Java IO chuẩn, do đó bạn có thể muốn đọc một số hướng dẫn về chủ đề đó. – hcpl
Nếu bạn muốn tạo một tập tin và viết và nối thêm dữ liệu vào nó nhiều lần, sau đó sử dụng mã dưới đây, nó sẽ tạo ra tập tin nếu không thoát và sẽ nối thêm dữ liệu nếu nó tồn tại.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd");
Date now = new Date();
String fileName = formatter.format(now) + ".txt";//like 2016_01_12.txt
try
{
File root = new File(Environment.getExternalStorageDirectory()+File.separator+"Music_Folder", "Report Files");
//File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists())
{
root.mkdirs();
}
File gpxfile = new File(root, fileName);
FileWriter writer = new FileWriter(gpxfile,true);
writer.append(sBody+"\n\n");
writer.flush();
writer.close();
Toast.makeText(this, "Data has been written to Report File", Toast.LENGTH_SHORT).show();
}
catch(IOException e)
{
e.printStackTrace();
}
First create a Project With PdfCreation in Android Studio
Then Follow below steps:
1.Download itextpdf-5.3.2.jar library from this link [https://sourceforge.net/projects/itext/files/iText/iText5.3.2/][1] and then
2.Add to app>libs>itextpdf-5.3.2.jar
3.Right click on jar file then click on add to library
4. Document document = new Document(PageSize.A4); // Create Directory in External Storage
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/PDF");
System.out.print(myDir.toString());
myDir.mkdirs(); // Create Pdf Writer for Writting into New Created Document
try {
PdfWriter.getInstance(document, new FileOutputStream(FILE));
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} // Open Document for Writting into document
document.open(); // User Define Method
addMetaData(document);
try {
addTitlePage(document);
} catch (DocumentException e) {
e.printStackTrace();
} // Close Document after writting all content
document.close();
5. public void addMetaData(Document document)
{
document.addTitle("RESUME");
document.addSubject("Person Info");
document.addKeywords("Personal, Education, Skills");
document.addAuthor("TAG");
document.addCreator("TAG");
}
public void addTitlePage(Document document) throws DocumentException
{ // Font Style for Document
Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD
| Font.UNDERLINE, BaseColor.GRAY);
Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL); // Start New Paragraph
Paragraph prHead = new Paragraph(); // Set Font in this Paragraph
prHead.setFont(titleFont); // Add item into Paragraph
prHead.add("RESUME – Name\n"); // Create Table into Document with 1 Row
PdfPTable myTable = new PdfPTable(1); // 100.0f mean width of table is same as Document size
myTable.setWidthPercentage(100.0f); // Create New Cell into Table
PdfPCell myCell = new PdfPCell(new Paragraph(""));
myCell.setBorder(Rectangle.BOTTOM); // Add Cell into Table
myTable.addCell(myCell);
prHead.setFont(catFont);
prHead.add("\nName1 Name2\n");
prHead.setAlignment(Element.ALIGN_CENTER); // Add all above details into Document
document.add(prHead);
document.add(myTable);
document.add(myTable); // Now Start another New Paragraph
Paragraph prPersinalInfo = new Paragraph();
prPersinalInfo.setFont(smallBold);
prPersinalInfo.add("Address 1\n");
prPersinalInfo.add("Address 2\n");
prPersinalInfo.add("City: SanFran. State: CA\n");
prPersinalInfo.add("Country: USA Zip Code: 000001\n");
prPersinalInfo.add("Mobile: 9999999999 Fax: 1111111 Email: [email protected] \n");
prPersinalInfo.setAlignment(Element.ALIGN_CENTER);
document.add(prPersinalInfo);
document.add(myTable);
document.add(myTable);
Paragraph prProfile = new Paragraph();
prProfile.setFont(smallBold);
prProfile.add("\n \n Profile : \n ");
prProfile.setFont(normal);
prProfile.add("\nI am Mr. XYZ. I am Android Application Developer at TAG.");
prProfile.setFont(smallBold);
document.add(prProfile); // Create new Page in PDF
document.newPage();
}
Ông hỏi làm thế nào để tạo một tập tin 'Văn bản' (' .txt') và bạn đang trả lời cho thấy làm thế nào để tạo một tập tin '.pdf'. Với thư viện ... – ClassA
- 1. tạo một tệp văn bản và viết trên đó
- 2. Cách đọc tệp và ghi vào tệp văn bản?
- 3. Nghe trên cổng mạng và lưu dữ liệu vào tệp văn bản
- 4. Tệp văn bản R và khai thác văn bản ... cách tải dữ liệu
- 5. Mở và ghi dữ liệu trên tệp văn bản bằng cách bash/shell scripting
- 6. Chèn dữ liệu vào tệp XML bằng PHP DOM
- 7. Dữ liệu meta của tệp văn bản
- 8. Chèn nội dung vào tệp văn bản trong Powershell
- 9. Tạo tệp văn bản và tải xuống
- 10. Nối dữ liệu tệp văn bản vào tệp nhật ký .bat
- 11. xóa dữ liệu bên trong tệp văn bản trong C++
- 12. Đọc tệp văn bản từ máy chủ trên Android
- 13. Tạo tệp XML mới và ghi dữ liệu vào nó?
- 14. Điền vào cơ sở dữ liệu Android Từ tệp CSV?
- 15. Cách tạo tệp dữ liệu cho gnuplot?
- 16. cách thêm dữ liệu được ghi vào một tệp?
- 17. Cách tạo tệp trong Android?
- 18. Nối văn bản vào tệp bằng Python
- 19. Django - cách tạo tệp và lưu tệp đó vào FileField của mô hình?
- 20. Cách ghi dữ liệu localStorage vào tệp văn bản trong Chrome
- 21. Lưu dữ liệu vào tệp qua Ajax POST và PHP
- 22. Đăng nhập bằng văn bản Android vào tệp
- 23. đính kèm tệp văn bản vào email
- 24. Ghi dữ liệu vào một tệp văn bản cục bộ với javascript
- 25. Ghi vào tệp CSV và xuất tệp đó?
- 26. cách tạo tệp tai và bao gồm tệp chiến tranh và tệp jar bên trong đó
- 27. Cách giải nén tệp gzip và ghi tệp đó ra tệp không nén, trên iPhone
- 28. Đăng nhập vào một tệp trên Android
- 29. So sánh hiệu suất để ghi vào MSMQ/tệp văn bản/cơ sở dữ liệu
- 30. Tải dữ liệu từ tệp văn bản vào bảng trong oracle
Tôi chạy nó trên giả lập đầu tiên trên thực tế, mã này có thể làm việc cho các giả lập? Thx u – Michelle
ya hoạt động tốt và bạn cũng có thể duyệt dữ liệu trong DDMS – Karthi
u có thể giải thích cho tôi String sFileName là gì, String sBody cho? Thx – Michelle