मैं इस कोड का उपयोग करके Android फ़ाइल पर कस्टम Log.txt फ़ाइल में लॉग लिखने की कोशिश कर रहा हूं, लेकिन फिर यह विधि फ़ाइल बनाती है, लेकिन इसमें कुछ भी नहीं है। मूल रूप से मैं फ़ाइल की पिछली सामग्री को पढ़ना चाहता हूं और फिर अपने डेटा को मौजूदा सामग्री के साथ जोड़ना चाहता हूं।
कोड इस प्रकार है:
public static void write(String str)
{
InputStream fileInputStream = null;
FileOutputStream fileOutpurStream = null;
try
{
fileInputStream = new FileInputStream(file);
fileOutpurStream = new FileOutputStream(file);
if(file.exists())
{
int ch = 0;
int current = 0;
StringBuffer buffer = new StringBuffer();
while((ch = fileInputStream.read()) != -1)
{
buffer.append((char) ch);
current++;
}
byte data[]=new byte[(int)file.length()];
fileInputStream.read(data);
fileOutpurStream.write(data);
fileOutpurStream.write(str.getBytes(),0,str.getBytes().length);
fileOutpurStream.flush();
}
else
{
file.createNewFile();
fileOutpurStream.write(str.getBytes(),0,str.getBytes().length);
fileOutpurStream.flush();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fileInputStream.close();
fileOutpurStream.flush();
fileOutpurStream.close();
fileOutpurStream = null;
fileInputStream = null;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}