Results 1 to 10 of 10
Thread: Append to file
- 06-16-2010, 05:46 PM #1
Append to file
Hi all,
Im trying to make a midlet for the S60, but im having trouble appending lines to a file.
I've tried an OutputStream and a printStream, but all I've managed to do is overwrite existing files.
Is there someway to append a line to a file?
I dont want to have to read a file, copy it to a new one and then add my line. There must be an easier way, surely?
Thanks,
Berkeleybross
Java Code:public void writeNewFile () { try { String url = System.getProperty("fileconn.dir.memorycard") + "test.txt"; String string = "this should be on the last line"; byte data[] = string.getBytes(); FileConnection file = (FileConnection)Connector.open(url, Connector.READ_WRITE); if (!file.exists()) { file.create(); } OutputStream output = file.openOutputStream(); output.write(data); output.close(); file.close(); } catch (IOException ioe) { System.out.println("IOException: "+ioe.getMessage()); } catch (SecurityException se) { System.out.println("Security exception:" + se.getMessage()); } }
- 06-17-2010, 05:35 AM #2
Member
- Join Date
- Mar 2010
- Location
- New Delhi,Vadodara
- Posts
- 50
- Rep Power
- 0
Hello,
If u want to append a file.
then follow below steps.
1.First u have to read that file and put all file in the StringBuffer Object
2.Then appent the content in it
3.then write content of that StringBuffer object in that file.
Like this.....
FileConnection fconn = (FileConnection)Connector.open(filepath,Connector. READ_WRITE);
InputStream is=fconn.openDataInputStream();
sb=new StringBuffer();
int chars,i=0;
while((chars=is.read())!=-1)
{
sb.append((char)chars);
}
sb.append("Hello world");
String fileContent=new String(sb.toString());
byte da[]=fileContent.getBytes();
OutputStream ops = fconn.openOutputStream();
ops.write(da);
ops.close();
fconn.close();
- 06-17-2010, 07:01 AM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Just use the append parameter on the FileWriter() constructor.
-Gary-
- 06-17-2010, 12:12 PM #4
Hi, thanks for the replies
@nitinverma: The code is a lot simpler than what i had thought I'd have to do, but isnt there anything like gcalvin said? A built in method to append a file?
@gcalvin: Im presuming you mean java.io.FileWriter? I tried putting it into my code but the class was not found. I can find it in a normal project, so i presume the S60 platform does not support it. Do you know of any similar classes that can be used on the mobile?
I've tried googling it and the best its come up with is
However all that does is make a file called "myfile.txt;append=true", and it overwrites it when writing.Java Code:Connector.open("file://c:/myfile.txt;append=true", Connector.WRITE );
Any more help much appreciated, failing that I guess Ill go with nitinverma's code to manually do it myself :(
Berkeleybross
- 06-17-2010, 12:55 PM #5
I've written a method based on nitinverma's code to append to a given file. I thought I'd share it with you all, incase anybody is in a similar situation.
If anyone spots any mistakes with it, let me know!Java Code:/** * Used to write output to a given file. Has the option to append, which will * add output to the end of the file. * If not appending, output will be put at start of file and overwrite any bytes * up to the length of output. * * @param output The string to be written to file * @param filePath The full URL of the file to be written to * @param append true if output should be put at end of file, false otherwise * @throws java.io.IOException */ public void appendToFile (String output, String filePath, boolean append) throws java.io.IOException { FileConnection file = (FileConnection)Connector.open(filePath, Connector.READ_WRITE); byte data[]; // Create the file if it doesnt exist if (!file.exists()) { file.create(); data = output.getBytes(); } else { // If append is needed, read the file and put it to the output data if (append) { InputStream is = file.openDataInputStream(); StringBuffer sb = new StringBuffer(); int chars = 0; while((chars = is.read()) != -1) { sb.append((char)chars); } is.close(); // Add output to the output data sb.append(output); String fileContent = sb.toString(); data = fileContent.getBytes(); } else { // Add output to the output data data = output.getBytes(); } } // Write out the data to the file OutputStream ops = file.openOutputStream(); ops.write(data); ops.close(); file.close(); }
Berkeleybross
- 06-17-2010, 01:30 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
If your CLDC confuration has a FileOutputStream class check if it supports the 'open in append mode' constructor. If so wrap that stream in a OutputStreamWriter and you have your writer (if you need one); otherwise just use the OutputStream itself.
kind regards,
Jos
- 06-17-2010, 01:56 PM #7
You can seek help from this tutorial by Java --- Files
Hope that helps.
Regardshttp://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)First, solve the problem. Then, write the code.
- 06-17-2010, 02:48 PM #8
Thanks for the tips.
@Jos unfortunately it doesnt seem to support the FileOutputStream.
Would it be possible to add it to the jar? If so, how? (in netbeans :P)
@tyrocer Im writing for a MIDlet not the standard java. Some of the java.io stuff is supported but the useful stuff (like append:P) doesnt seem to be.
Thanks,
Berkeleybross
- 06-18-2010, 05:19 AM #9
Member
- Join Date
- Mar 2010
- Location
- New Delhi,Vadodara
- Posts
- 50
- Rep Power
- 0
Hey Berkeleybross,
r u satisfy with my code or not.
- 06-18-2010, 11:42 AM #10
Similar Threads
-
How to append data to an already existing file?
By siteregsam in forum New To JavaReplies: 3Last Post: 05-03-2010, 08:06 PM -
How to Append in file ?
By Hippo in forum New To JavaReplies: 2Last Post: 03-19-2010, 01:50 PM -
Browse for and append
By looselispssinkships in forum New To JavaReplies: 0Last Post: 03-19-2010, 10:25 AM -
append variables to a text file
By ddatta8 in forum New To JavaReplies: 2Last Post: 01-02-2009, 10:17 AM -
JLabel append?
By Jononomous in forum New To JavaReplies: 0Last Post: 04-07-2008, 07:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks