add ability to append to files

This commit is contained in:
Zsombor Gegesy 2010-08-15 03:05:18 +02:00 committed by Joel Dice
parent f0f35a920f
commit 1daa93d3c4
3 changed files with 32 additions and 4 deletions

View File

@ -593,11 +593,11 @@ Java_java_io_FileInputStream_close(JNIEnv* e, jclass, jint fd)
}
extern "C" JNIEXPORT jint JNICALL
Java_java_io_FileOutputStream_open(JNIEnv* e, jclass, jstring path)
Java_java_io_FileOutputStream_open(JNIEnv* e, jclass, jstring path, jboolean append)
{
string_t chars = getChars(e, path);
if (chars) {
int fd = doOpen(e, chars, O_WRONLY | O_CREAT | O_TRUNC);
int fd = doOpen(e, chars, append ? (O_WRONLY | O_APPEND) : (O_WRONLY | O_CREAT | O_TRUNC));
releaseChars(e, path, chars);
return fd;
} else {

View File

@ -22,14 +22,19 @@ public class FileOutputStream extends OutputStream {
}
public FileOutputStream(String path) throws IOException {
fd = open(path);
this(path, false);
}
public FileOutputStream(String path, boolean append) throws IOException {
fd = open(path, append);
}
public FileOutputStream(File file) throws IOException {
this(file.getPath());
}
private static native int open(String path) throws IOException;
private static native int open(String path, boolean append) throws IOException;
private static native void write(int fd, int c) throws IOException;

23
test/FileOutput.java Normal file
View File

@ -0,0 +1,23 @@
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutput {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileOutputStream f = new FileOutputStream("test.txt");
f.write("Hello world!\n".getBytes());
f.close();
FileOutputStream f2 = new FileOutputStream("test.txt", true);
f2.write("Hello world again!".getBytes());
f2.close();
}
}