फ़ाइल कैसे बनाएं, इसमें डेटा लिखें और एंड्रॉइड पर डेटा पढ़ें? यदि संभव हो तो एक कोड स्निपेट प्रदान करें।एंड्रॉइड में फ़ाइल कैसे बनाएं?
उत्तर
From here: http://www.anddev.org/working_with_files-t115.html
//Writing a file...
try {
// catches IOException below
final String TESTSTRING = new String("Hello Android");
/* We have to use the openFileOutput()-method
* the ActivityContext provides, to
* protect your file from others and
* This is done for security-reasons.
* We chose MODE_WORLD_READABLE, because
* we have nothing to hide in our file */
FileOutputStream fOut = openFileOutput("samplefile.txt",
MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
osw.write(TESTSTRING);
/* ensure that everything is
* really written out and close */
osw.flush();
osw.close();
//Reading the file back...
/* We have to use the openFileInput()-method
* the ActivityContext provides.
* Again for security reasons with
* openFileInput(...) */
FileInputStream fIn = openFileInput("samplefile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
/* Prepare a char-Array that will
* hold the chars we read back in. */
char[] inputBuffer = new char[TESTSTRING.length()];
// Fill the Buffer with data from the file
isr.read(inputBuffer);
// Transform the chars to a String
String readString = new String(inputBuffer);
// Check if we read back the same chars that we had written out
boolean isTheSame = TESTSTRING.equals(readString);
Log.i("File Reading stuff", "success = " + isTheSame);
} catch (IOException ioe)
{ioe.printStackTrace();}
फ़ाइल कहां लिखी जाएगी? –
"/data/data/your_project_package_structure/files/samplefile.txt" – Petar
क्या आपको 'बंद' से पहले 'फ्लश' कॉल करने की आवश्यकता है? –
मैं बाइट्स लिखने के लिए एक अस्थायी फ़ाइल बनाने के लिए निम्नलिखित कोड का इस्तेमाल किया। और यह ठीक काम कर रहा है।
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.txt");
file.createNewFile();
byte[] data1={1,1,0,0};
//write the bytes in file
if(file.exists())
{
OutputStream fo = new FileOutputStream(file);
fo.write(data1);
fo.close();
System.out.println("file created: "+file);
url = upload.upload(file);
}
//deleting the file
file.delete();
System.out.println("file deleted");
मैं इस सूत्र है कि दूसरों के लिए सहायक हो सकता है से एक वर्ग लिखने का फैसला किया। ध्यान दें कि वर्तमान में यह केवल "फाइलें" निर्देशिका में लिखना है (उदा। "Sdcard" पथों को नहीं लिखता है)।
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.content.Context;
public class AndroidFileFunctions {
public static String getFileValue(String fileName, Context context) {
try {
StringBuffer outStringBuf = new StringBuffer();
String inputLine = "";
/*
* We have to use the openFileInput()-method the ActivityContext
* provides. Again for security reasons with openFileInput(...)
*/
FileInputStream fIn = context.openFileInput(fileName);
InputStreamReader isr = new InputStreamReader(fIn);
BufferedReader inBuff = new BufferedReader(isr);
while ((inputLine = inBuff.readLine()) != null) {
outStringBuf.append(inputLine);
outStringBuf.append("\n");
}
inBuff.close();
return outStringBuf.toString();
} catch (IOException e) {
return null;
}
}
public static boolean appendFileValue(String fileName, String value,
Context context) {
return writeToFile(fileName, value, context, Context.MODE_APPEND);
}
public static boolean setFileValue(String fileName, String value,
Context context) {
return writeToFile(fileName, value, context,
Context.MODE_WORLD_READABLE);
}
public static boolean writeToFile(String fileName, String value,
Context context, int writeOrAppendMode) {
// just make sure it's one of the modes we support
if (writeOrAppendMode != Context.MODE_WORLD_READABLE
&& writeOrAppendMode != Context.MODE_WORLD_WRITEABLE
&& writeOrAppendMode != Context.MODE_APPEND) {
return false;
}
try {
/*
* We have to use the openFileOutput()-method the ActivityContext
* provides, to protect your file from others and This is done for
* security-reasons. We chose MODE_WORLD_READABLE, because we have
* nothing to hide in our file
*/
FileOutputStream fOut = context.openFileOutput(fileName,
writeOrAppendMode);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
osw.write(value);
// save and close
osw.flush();
osw.close();
} catch (IOException e) {
return false;
}
return true;
}
public static void deleteFile(String fileName, Context context) {
context.deleteFile(fileName);
}
}
मैंने आपके कोड की जांच की है लेकिन कुछ आदेश हैं जो नए एपीआई (17) के लिए बहिष्कृत हैं: Context.MODE_WORLD_READABLE और Context.MODE_WORLD_WRITEABLE को बदलने की जरूरत है। –
बहिष्कृत बिट्स के अलावा - आपको अंत में बंद होना चाहिए और आपको बंद होने से पहले फ्लश करने की आवश्यकता नहीं है। कृपया मैला कोड पोस्ट न करें –
एक फ़ाइल test.txt को लिखें:
String filepath ="/mnt/sdcard/test.txt";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filepath);
byte[] buffer = "This will be writtent in test.txt".getBytes();
fos.write(buffer, 0, buffer.length);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fos != null)
fos.close();
}
पढ़ें फ़ाइल test.txt से:
String filepath ="/mnt/sdcard/test.txt";
FileInputStream fis = null;
try {
fis = new FileInputStream(filepath);
int length = (int) new File(filepath).length();
byte[] buffer = new byte[length];
fis.read(buffer, 0, length);
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fis != null)
fis.close();
}
नोट: AndroidManifest में इन दो अनुमति जोड़ने के लिए मत भूलना। xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- 1. एंड्रॉइड निर्यात: .keystore फ़ाइल कैसे बनाएं?
- 2. एंड्रॉइड जेस्चर की कच्ची फ़ाइल कैसे बनाएं
- 3. एंड्रॉइड में बीप कैसे बनाएं?
- 4. एंड्रॉइड में टेक्स्टएरिया कैसे बनाएं
- 5. एंड्रॉइड पर openssl कैसे बनाएं
- 6. फ़ाइल स्पैस कैसे बनाएं?
- 7. .mbtiles फ़ाइल कैसे बनाएं?
- 8. एक्सएमएल फ़ाइल बनाएं और इसे आंतरिक स्टोरेज एंड्रॉइड में सहेजें
- 9. एंड्रॉइड एमुलेटर के लिए एसडी कार्ड छवि फ़ाइल कैसे बनाएं?
- 10. एंड्रॉइड में उलटी गिनती टाइमर कैसे बनाएं?
- 11. एंड्रॉइड में संपादन योग्य स्पिनर कैसे बनाएं?
- 12. एंड्रॉइड में लिंक बटन कैसे बनाएं
- 13. एंड्रॉइड एडिटटेक्स्ट में संकेत संकेत कैसे बनाएं?
- 14. एंड्रॉइड में ट्विटर शेयर बटन कैसे बनाएं?
- 15. एंड्रॉइड में यूट्यूब वीडियो थंबनेल कैसे बनाएं?
- 16. एंड्रॉइड में एक टक्कर ऐप कैसे बनाएं?
- 17. एंड्रॉइड में लाइव वॉलपेपर कैसे बनाएं?
- 18. एंड्रॉइड में पॉपअप विंडो कैसे बनाएं?
- 19. एंड्रॉइड में JSON प्रारूप डेटा कैसे बनाएं?
- 20. एंड्रॉइड में कस्टम वेबव्यू कैसे बनाएं?
- 21. एंड्रॉइड 4.0 में पूर्ण स्क्रीन कैसे बनाएं
- 22. एंड्रॉइड में पेड़ दृश्य कैसे बनाएं?
- 23. एंड्रॉइड में विस्तारणीय पैनल लेआउट कैसे बनाएं?
- 24. एंड्रॉइड में जावा थ्रेड डंप कैसे बनाएं?
- 25. एंड्रॉइड में बॉर्डरलेस बटन कैसे बनाएं
- 26. एंड्रॉइड में गोलाकार टेक्स्टव्यू कैसे बनाएं?
- 27. एंड्रॉइड - ग्रिडव्यू में अनुभाग कैसे बनाएं?
- 28. एंड्रॉइड में असुरक्षित आरएफसीओएमएम सॉकेट कैसे बनाएं?
- 29. एंड्रॉइड में डेटाग्रिड डिस्प्ले कैसे बनाएं?
- 30. एंड्रॉइड में वर्टिकल कैरोसेल कैसे बनाएं?
इस [लिंक] को देखें [http://www.anddev.org/worki ng_with_files-t115.html) इसमें फ़ाइलों से लिखना और पढ़ने के तरीके पर ट्यूटोरियल का पालन करना आसान है। – Prashast
http://developer.android.com/training/basics/data-storage/files.html –